Datagrunt 4.6.38: Image Stream Size Checks in PDF Extraction
Datagrunt 4.6.38 extends the checks PDFReader and PDFWriter make on embedded images before decoding them. Upgrading is recommended for everyone who reads PDFs.
What changed
An image in a PDF declares its size (/Width, /Height, and its bits per component), and its data is usually compressed. Typical filters are FlateDecode, LZWDecode, RunLengthDecode, ASCII85Decode and ASCIIHexDecode, alone or chained.
From 4.6.38 (#441), the default pdfium engine checks that each such stream decodes to no more than its declared width and height can hold, before reading it. pdfium does not report an image’s colour depth without decoding it, so the allowance assumes the widest pixel format:
- Images: an image whose stream decodes to more than that is skipped, with a page warning naming the page and image (
its stream decodes to more than its declared size; skipped (not decoded)). It is never decoded. - Page renders: the same check runs before the
pdfiumengine renders a page (render_pages_as_images, and OCR of scanned pages). - Unreadable data: an image whose filter chain includes a filter Datagrunt can’t check, or whose data is corrupt, is skipped the same way. The
pymupdfengine is unchanged.
Text extraction is unchanged, and so is output on documents whose images match their declared sizes. No code changes are needed.
If a document you process is skipped this way, the page warnings in to_dicts() name the image and the reason.
Upgrading
uv pip install --upgrade datagrunt