Documentation

Options and context cancellation

The Context-taking entry points add a context.Context for cancellation and an Options struct for password, DPI, iteration and worker tuning.

The two-argument forms — Convert(path, profile), Verify(path, profile), and their Bytes/All variants — cover the common case. Each has a …Context counterpart that adds a context.Context for cancellation and an Options struct for tuning. The zero Options value is the default behaviour.

options.go
cr, err := gopdfrab.ConvertContext(ctx, path, gopdfrab.PDFA1B, gopdfrab.Options{
	Password:      []byte("secret"), // decrypt an encrypted input
	RasterDPI:     300,              // raster last-resort resolution (default 150)
	MaxIterations: 8,                // verify/fix loop bound (default 4)
	Workers:       4,                // batch concurrency (default runtime.NumCPU)
})
defer cr.Close()

// To set options without a deadline, pass context.Background()
res, err := gopdfrab.VerifyContext(context.Background(), path, gopdfrab.PDFA1B,
	gopdfrab.Options{Password: []byte("secret")})
Field Type Description Applies to
Password[]byteUser or owner password for an encrypted input. nil is the empty password.Verify + Convert
RasterDPIintResolution for the raster last resort and transparency flattening. 0 = 150.Convert
MaxIterationsintBounds the verify/fix loop. 0 = 4.Convert
CheckFidelityboolRender input and output and populate Fidelity with a per-page comparison. Roughly doubles the work.Convert
WorkersintBounds batch concurrency. 0 = runtime.NumCPU.ConvertAll + ConvertEach

Options.Password applies at the open step, so it works on ConvertContext/VerifyContext but not on the *Document methods, whose file is already open — use OpenWithPassword there. ConvertContext checks the context before each verify/fix iteration and each raster pass; the batch forms stop dispatching new files once it is cancelled and record ctx.Err() for the rest.

Cancellation is worth wiring up for anything user-facing. Conversion is iterative and rasterization is the expensive fallback, so a pathological document can take meaningfully longer than a typical one — a context with a deadline turns that from a hang into an error you can report.