#4127·pypdf

`PdfReader` cannot open a file whose `%%EOF` marker is not at the start of a line (`startxref <offset>%%EOF`)

Author: dexters1Created Sep 23, 2026Updated Sep 24, 2026
Labelsis-robustness-issue

PdfReader fails on a PDF whose last line has the %%EOF marker directly after the startxref offset, without an EOL in between:

works:  ...>>\nstartxref\n256\n%%EOF\n
fails:  ...>>\nstartxref 256%%EOF

The rest of the file is identical. Adding a single EOL before %%EOF makes the file readable again. It fails with both strict=True and strict=False.

We hit this with real-world files from a user of our product (2 of 5 files from one source). Those files open fine in Acrobat, Preview, MuPDF and pdfminer.six. The same happens when %%EOF follows the offset on its own line (startxref\n256%%EOF) and when a trailing newline follows the marker (startxref 256%%EOF\n). startxref 256\n%%EOF, with the offset on the startxref line, reads fine (#87).

What happens

_find_eof_marker walks backwards line by line until a line starts with %%EOF. The last line here is startxref 256%%EOF, so it never matches. The search logs CAUTION: startxref found while searching for %%EOF and keeps going back to the start of the file. Then:

  • strict=True raises PdfReadError: EOF marker not found.
  • strict=False logs EOF marker not found and then calls read_previous_line again at the start of the stream, which raises PdfStreamError: Stream has ended unexpectedly. So non-strict mode neither recovers nor gives a clear error.

I'm aware ISO 32000 says the last line shall contain only %%EOF, so these files are not strictly conformant. I'm raising this because pypdf already tolerates similar trailer irregularities: startxref on the offset's line (#87), a truncated %%EOF (#2853) and duplicate %%EOF markers (#4008).

Checked on 6.19.0 and on main (54d35184b9); 6.9.2 behaves the same.

Environment

bash
$ python -m platform
macOS-15.0.1-arm64-arm-64bit

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.19.0, crypt_provider=('cryptography', '50.0.1'), PIL=12.3.0

Code + PDF

This builds the file itself, so no attachment is needed:

python
import io
import re

from pypdf import PdfReader, PdfWriter

writer = PdfWriter()
writer.add_blank_page(200, 200)
buffer = io.BytesIO()
writer.write(buffer)
data = buffer.getvalue()  # ends with b">>\nstartxref\n<offset>\n%%EOF\n"

# Same bytes, only the whitespace in the last line changed: "%%EOF" follows the offset directly.
compact = re.sub(rb"startxref\s+(\d+)\s+%%EOF\s*$", rb"startxref \1%%EOF", data)
print(data[-25:], "->", compact[-25:])

for strict in (True, False):
    print(len(PdfReader(io.BytesIO(data), strict=strict).pages))  # 1
for strict in (True, False):
    try:
        PdfReader(io.BytesIO(compact), strict=strict)
    except Exception as exc:
        print(f"strict={strict}: {type(exc).__name__}: {exc}")

Output:

b'R\n>>\nstartxref\n256\n%%EOF\n' -> b'0 R\n>>\nstartxref 256%%EOF'
1
1
strict=True: PdfReadError: EOF marker not found
strict=False: PdfStreamError: Stream has ended unexpectedly

You're welcome to add the generated file to your tests.

Traceback

strict=True:

CAUTION: startxref found while searching for %%EOF. The file might be truncated and some data might not be read.
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File ".../site-packages/pypdf/_reader.py", line 151, in __init__
    self._initialize_stream(stream)
  File ".../site-packages/pypdf/_reader.py", line 175, in _initialize_stream
    self.read(stream)
  File ".../site-packages/pypdf/_reader.py", line 689, in read
    self._find_eof_marker(stream)
  File ".../site-packages/pypdf/_reader.py", line 800, in _find_eof_marker
    raise PdfReadError("EOF marker not found")
pypdf.errors.PdfReadError: EOF marker not found

strict=False:

CAUTION: startxref found while searching for %%EOF. The file might be truncated and some data might not be read.
EOF marker not found
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File ".../site-packages/pypdf/_reader.py", line 151, in __init__
    self._initialize_stream(stream)
  File ".../site-packages/pypdf/_reader.py", line 175, in _initialize_stream
    self.read(stream)
  File ".../site-packages/pypdf/_reader.py", line 689, in read
    self._find_eof_marker(stream)
  File ".../site-packages/pypdf/_reader.py", line 802, in _find_eof_marker
    line = read_previous_line(stream)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/pypdf/_utils.py", line 380, in read_previous_line
    raise PdfStreamError(STREAM_TRUNCATED_PREMATURELY)
pypdf.errors.PdfStreamError: Stream has ended unexpectedly