Skip to content

Skip trailing null bytes when searching for EOF marker#586

Open
55728 wants to merge 1 commit intoyob:mainfrom
55728:fix-trailing-null-after-eof
Open

Skip trailing null bytes when searching for EOF marker#586
55728 wants to merge 1 commit intoyob:mainfrom
55728:fix-trailing-null-after-eof

Conversation

@55728
Copy link
Copy Markdown

@55728 55728 commented Apr 3, 2026

Problem

PDFs generated by some tools (e.g. Atos/Fonet) append thousands of null bytes (\x00) after %%EOF. Since null bytes are not line terminators, they are not split by the line parser, preventing the EOF marker from being found within the trailing byte scan window (TRAILING_BYTECOUNT = 5000).

This causes PDF::Reader::MalformedPDFError: PDF does not contain EOF marker.

Solution

Scan backwards from the end of the file to skip any trailing null bytes before reading the final bytes to locate the %%EOF marker and xref offset. This handles any amount of trailing null bytes regardless of TRAILING_BYTECOUNT.

The existing NULL_BYTE constant defined in Buffer is reused.

Test

Added trailing_null_bytes.pdf (extended_eof.pdf + 6000 null bytes) and a spec that verifies the correct xref offset (145) is still found.

Fixes #585

Some PDF generators (e.g. Atos/Fonet) append thousands of null
bytes after %%EOF. Since null bytes are not line terminators,
they prevented the EOF marker from being found within the
trailing byte scan window.

This change scans backwards from the end of the file to skip
any trailing null bytes before reading the final bytes to locate
the %%EOF marker and xref offset.

Fixes yob#585
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.

PDF with trailing null characters after EOF raises MalformedPDFError

1 participant