Settings

Some of pikepdf’s global parameters can be tuned.

pikepdf.settings.get_decimal_precision()

Set the number of decimal digits to use when converting floats.

Return type:

int

pikepdf.settings.set_decimal_precision(prec, /)

Get the number of decimal digits to use when converting floats.

Parameters:

prec (int)

Return type:

int

pikepdf.settings.set_flate_compression_level(level, /)

Set compression level whenever Flate compression is used.

Parameters:

level (Literal[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) – -1 (default), 0 (no compression), 1 to 9 (increasing compression)

Return type:

int

qpdf limits

qpdf applies global limits that protect against malicious or badly damaged PDFs, such as the maximum nesting depth when parsing objects, or the maximum memory used to decode a stream. These limits apply to the whole process.

pikepdf.settings.get_qpdf_limits()

Return qpdf’s current global limits, keyed by name.

The keys are the keyword arguments accepted by set_qpdf_limits(), so the result can be saved and later restored with set_qpdf_limits(**saved).

Return type:

dict[str, int | bool]

pikepdf.settings.set_qpdf_limits(*, doc_max_warnings=None, parser_max_nesting=None, parser_max_errors=None, parser_max_container_size=None, parser_max_container_size_damaged=None, max_stream_filters=None, dct_max_memory=None, dct_max_progressive_scans=None, flate_max_memory=None, png_max_memory=None, run_length_max_memory=None, tiff_max_memory=None, dct_throw_on_corrupt_data=None)

Set qpdf’s global limits, which guard against malicious or damaged PDFs.

Arguments left as None are unchanged. All values are checked before any is applied, so an invalid argument leaves every limit as it was.

These limits are process-wide: they affect every pikepdf.Pdf in the process, including those used by other libraries, and qpdf does not synchronize access to them. Set them once at startup, before other threads use pikepdf. pikepdf.JobBuilder.limits() changes the same settings.

Parameters:
  • doc_max_warnings (int | None) – Maximum number of warnings a newly opened pikepdf.Pdf accepts before giving up; 0 (the default) means no limit.

  • parser_max_nesting (int | None) – Maximum nesting depth of arrays and dictionaries while parsing. Default 499.

  • parser_max_errors (int | None) – Maximum number of errors while parsing an object or content stream before qpdf gives up; 0 means no limit. Default 15. Content stream parsing stops silently, apart from a warning, when this limit is reached, so raise it or set it to 0 to recover as much of a badly damaged content stream as possible.

  • parser_max_container_size (int | None) – Maximum number of items in an array or dictionary while parsing. Default 4294967295.

  • parser_max_container_size_damaged (int | None) – As parser_max_container_size, while recovering a damaged file. Default 5000.

  • max_stream_filters (int | None) – Maximum number of filters a stream may have and still be decoded. Default 25.

  • dct_max_memory (int | None) – Maximum memory in bytes for decoding a DCT (JPEG) stream; 0 (the default) means no limit.

  • dct_max_progressive_scans (int | None) – Maximum number of progressive scans when decoding a DCT (JPEG) stream; 0 (the default) means no limit.

  • flate_max_memory (int | None) – Maximum memory in bytes for decoding a Flate stream; 0 (the default) means no limit.

  • png_max_memory (int | None) – Maximum memory in bytes for PNG predictors; 0 (the default) means no limit.

  • run_length_max_memory (int | None) – Maximum memory in bytes for decoding a RunLength stream; 0 (the default) means no limit.

  • tiff_max_memory (int | None) – Maximum memory in bytes for TIFF predictors; 0 (the default) means no limit.

  • dct_throw_on_corrupt_data (bool | None) – If True (the default), decoding corrupt DCT (JPEG) data raises an error; if False, qpdf decodes as much as it can.

Returns:

The previous values of the limits that were given, so that set_qpdf_limits(**previous) restores them.

Return type:

dict[str, int | bool]

pikepdf.settings.disable_qpdf_default_limits()

Disable qpdf’s optional default limits, for the rest of the process.

parser_max_errors becomes unlimited, and max_stream_filters and parser_max_container_size_damaged become effectively unlimited, unless they were already set explicitly. parser_max_nesting still applies, since it prevents stack overflows. Limits set explicitly afterward with set_qpdf_limits() still apply.

This cannot be undone. Like set_qpdf_limits(), it affects the whole process.

Return type:

None

pikepdf.settings.qpdf_limit_errors()

Return how many times any qpdf global limit has been exceeded.

The count covers the whole process since startup and never decreases, which makes it useful for noticing that a limit was hit when qpdf only issued a warning, such as content stream parsing stopping at parser_max_errors.

Return type:

int