Skip to content

fix: accept PDF 2.0 headers and white space after the version - #99

Open
wleeaf wants to merge 1 commit into
ledongthuc:masterfrom
wleeaf:pdf-2.0-header
Open

wleeaf wants to merge 1 commit into
ledongthuc:masterfrom
wleeaf:pdf-2.0-header

Conversation

@wleeaf

@wleeaf wleeaf commented Oct 26, 2026

Copy link
Copy Markdown

NewReaderEncrypted accepts only %PDF-1.0 to %PDF-1.7 directly followed by CR or LF, so it rejects two kinds of files with "not a PDF file: invalid header":

  • Every PDF 2.0 file. Their header is %PDF-2.0 (ISO 32000-2, 7.5.2), and pdfTeX and LuaTeX write it, for example.
  • Files with white space between the version and the end of the line. tiff2pdf writes %PDF-1.1 \n (Handle space after header #22), and some writers put a NUL byte there.

The check now accepts %PDF-1.0 to %PDF-1.7 and %PDF-2.0, followed by any PDF white-space character. Anything else is still rejected.

Test plan:

  • Added TestNewReaderAcceptsHeaders. It opens a one-page file with each of several headers and checks NumPage, and checks that %PDF-1.8, %PDF-2.1, %PDF-1. and %PDF-1.45 are still rejected. On master it fails for %PDF-2.0\n, %PDF-2.0\r, %PDF-1.1 \n and %PDF-1.2\x00\n.
  • go test ./... and go build ./... pass.
  • I ran 1,637 PDFs from the qpdf and pdf.js test suites through the reader. 23 files that were rejected for their header now open; 17 of them are PDF 2.0, and qpdf reads 22 without a warning.
    • Their objects match qpdf --json, apart from one odd-length hex string that the reader fails on for a separate reason.
    • NumPage agrees with qpdf on all 23, and GetPlainText returns no error.

Fixes #22.

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.

Handle space after header

1 participant