Options and limits

Tune conversion, cancel with a context, and cap memory for untrusted input.

Every entry point has a …Context form that adds a context.Context and an Options struct. The zero Options value gives 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 when the file is opened, so for an already-open document use OpenWithPassword. Conversion checks the context before each pass. Batches stop dispatching once it is cancelled and record ctx.Err() for the remaining files. Give user-facing calls a deadline, because a pathological file can take much longer than a typical one.

Resource limits

By default one stream decodes to at most 256 MB, which guards against decompression bombs, and an open document keeps at most 64 MB of caches. Raise the caps for large PDFs or lower them for untrusted input. Hitting a cap is reported, never silently truncated.

limits.go
// A single stream decodes to at most 256 MB by default, guarding against
// decompression bombs. Raise it for legitimately large PDFs, or lower it to
// harden against hostile input.
gopdfrab.SetLimits(gopdfrab.Limits{
	MaxDecodedStreamBytes: 512 << 20, // 512 MB
	MaxResidentBytes:      32 << 20,  // caches one open document keeps (default 64 MB)
})

gopdfrab.CurrentLimits()  // the effective caps
gopdfrab.DefaultLimits()  // the built-in caps

The caps are process-wide. Set them once at startup, before any concurrent work. A zero or negative value resets that cap to its default.