Skip to content

fix(pdf): inflate compressed object and content streams before scanning - #300

Open
kadnaoui wants to merge 1 commit into
Community-Access:mainfrom
kadnaoui:fix/pdf-compressed-object-streams
Open

kadnaoui wants to merge 1 commit into
Community-Access:mainfrom
kadnaoui:fix/pdf-compressed-object-streams

Conversation

@kadnaoui

Copy link
Copy Markdown

What this changes and why

Fixes #297. parsePdfBasics searched the raw file bytes for /MarkInfo, /StructTreeRoot, /Title, /Lang, BT, /FontFile and /ToUnicode. WeasyPrint and most current writers put the catalog, structure tree and Info dictionary in a compressed object stream (/Type /ObjStm) and compress the page content, so a tagged PDF/UA file came back as untagged, with no title, language, text or fonts, and the user was told to OCR a document that already had real text.

The scanner now inflates FlateDecode object streams, content streams and form XObjects with node:zlib (no new dependency) and searches their contents along with the raw bytes. Images, embedded fonts, cross-reference streams and other binary streams are skipped so their bytes cannot match a marker by accident, a stream that does not inflate is skipped, and inflated output is capped at 50 MB per file. .github/scripts/pdf-a11y-scan.mjs had the same raw-bytes approach and gets the same helper.

Tested:

  • The reproduction from the issue (WeasyPrint 68.1, pdf_variant="pdf/ua-1"): scan_pdf_document went from 6 findings to none, and the expanded copy still has none. The CI script went from 6 errors to none on the same file.
  • New tests in mcp-server/server-core.test.js: a tagged PDF built with its objects in a compressed object stream and compressed page content (fails on main with the pdf-tagged, pdf-structure-tree, pdf-title, pdf-language, pdf-text-content and pdf-fonts findings, passes with the fix), and an untagged PDF with a damaged Flate stream that must still be reported.
  • npm run verify: 22 of 22 checks pass. npm run verify:release passes.

Accessibility checklist

Checks

  • npm run verify passes (it includes the Markdown accessibility linter and markdownlint)
  • No new Markdown accessibility warnings in the files I changed

Documentation and skill text (any change to Markdown, a skill or a report template)

The only Markdown change is one CHANGELOG entry.

  • One H1 per file, and headings do not skip levels
  • Link text is unique and descriptive (no "click here" or "read more")
  • N/A Images have meaningful alternative text or are marked decorative; diagrams have a text alternative nearby
  • N/A Each table has a one-sentence description before it and a header row
  • Real lists, not typed numbering; code in fenced blocks with a language
  • Meaning is not carried by color, position or styling alone
  • Plain language; abbreviations spelled out on first use
  • House style from AGENTS.md: no emoji, decorative Unicode or smart punctuation

Findings and guidance (skill changes)

N/A: no skill changes, and no finding wording changed.

Anything with a user interface (generated HTML, example/, a dashboard)

N/A

Terminal and MCP output (the installer, scripts, hooks, MCP tools, report wording)

  • Output is plain ASCII, in the order things happen, with status in words, not a symbol or color alone (no output wording changed; the scanner simply stops reporting findings that are not true)
  • N/A No prompt needs a pointer, and every prompt can be left with Ctrl+C or Escape
  • N/A Errors are a sentence that says what to do next; unknown options are refused, not acted on
  • N/A Counts match their nouns, and report headings, tables and findings follow the interface requirements

Dependencies

N/A: no dependency added.

Release

N/A: no version or release files changed.

Accessibility notes

Only Flate is inflated. A stream behind another filter (for example ASCII85 then Flate) is still searched only as raw bytes, as before.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FDKaHBvCwNUbDLqsswZqqD

parsePdfBasics searched the raw file bytes for /MarkInfo, /StructTreeRoot,
/Title, /Lang, BT, /FontFile and /ToUnicode. WeasyPrint and most current
writers put the catalog, structure tree and Info dictionary in a compressed
object stream (/Type /ObjStm) and compress the page content, so none of
those markers appear in the raw bytes and a tagged PDF/UA file was reported
as untagged, untitled, without language, text or fonts.

Inflate FlateDecode object streams, content streams and form XObjects with
node:zlib and search their contents along with the raw bytes. Images, fonts
and other binary streams are skipped, and inflated output is capped at
50 MB. The CI scanner in .github/scripts/pdf-a11y-scan.mjs gets the same
helper.

Fixes Community-Access#297

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FDKaHBvCwNUbDLqsswZqqD
@yucheng-du

Copy link
Copy Markdown

Thanks for this fix. I tested the branch and found one side effect: a bookmark title can now be read as the document title.

To reproduce: a one-page PDF with no title in the Info dictionary and one bookmark called "Bookmark", saved by pdf-lib with its default object streams.

const doc = await PDFDocument.create();
doc.addPage().drawRectangle({ width: 10, height: 10 });
const outlines = doc.context.obj({ Type: "Outlines" });
const outlinesRef = doc.context.register(outlines);
const item = doc.context.register(
  doc.context.obj({ Title: PDFString.of("Bookmark"), Parent: outlinesRef }));
outlines.set(PDFName.of("First"), item);
outlines.set(PDFName.of("Last"), item);
doc.catalog.set(PDFName.of("Outlines"), outlinesRef);
await writeFile("graphics.pdf", await doc.save());

On this branch (f0543cb):

  • scan_pdf_document no longer reports pdf-title.
  • extract_document_metadata shows Title: Bookmark as PASS.
  • .github/scripts/pdf-a11y-scan.mjs no longer reports PDFBP.META.TITLE_PRESENT.

On main: all three still report the missing title, because the bookmark is hidden inside the compressed object stream.

Expected: the title check passes only when the document itself has a title. This file has none.

Cause: the title regex takes the first /Title anywhere in the text it searches, so it can match a bookmark. main already does this for uncompressed files; inflating object streams makes it reachable for compressed ones too.

Possible fix: read /Title only from the dictionary that the trailer's /Info entry points to. If you want to count XMP as well, also accept dc:title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scan_pdf_document reports tagged PDFs as untagged when their objects are in compressed object streams

2 participants