Skip to content

BUG: Fix AttributeError for /XRefStm in a cross-reference stream - #4451

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
prawalpokharel:fix/xrefstm-in-xref-stream
Nov 20, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
prawalpokharel:fix/xrefstm-in-xref-stream

Conversation

@prawalpokharel

Copy link
Copy Markdown
Contributor

PdfReader cannot open a PDF whose cross-reference stream dictionary has an /XRefStm entry:

AttributeError: 'PdfReader' object has no attribute 'stream'

_process_xref_stream reads self.stream, but _initialize_stream only sets that attribute after read() has returned, so it does not exist yet when the method runs. Before #2847 this code was inline and used the local stream. This PR passes the stream into the method again.

I found this while running pypdf over PDFs from the Common Crawl PDF corpus (CC-MAIN-2021-31-PDF-UNTRUNCATED). The file is 3472143.pdf in archive 3472.zip, SHA-256 700cb76ee7148438cb2860ed27be9f86935afa5ff9be8ca8bd7c8b7bd89058eb. It was written by Word 2010 as a hybrid-reference file and later updated by another tool, which copied /XRefStm into its cross-reference stream. poppler and PDFium open it. pypdf 5.0.0 opens it, 5.0.1 and later do not. With this change it opens again and has 2 pages.

The test builds a small PDF where the page object is only listed in the cross-reference stream that /XRefStm points to. It fails on main and passes with the change.

I can attach the file to an issue if you would like a test with the real file.

AI disclosure: I used Claude Code (Claude Fable 5.2) for the corpus run, the diagnosis, the fix and the test. I reviewed the diff and ran the tests myself.

PdfReader could not open a file whose cross-reference stream dictionary
contains /XRefStm. _process_xref_stream used self.stream, which is only
assigned after read() has returned. Use the stream that is being read,
as the code did before py-pdf#2847.

Co-authored-by: Claude <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.25%. Comparing base (d7f50bf) to head (c6528d6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4451      +/-   ##
==========================================
+ Coverage   99.21%   99.25%   +0.04%     
==========================================
  Files          59       59              
  Lines       12425    12425              
  Branches     2368     2368              
==========================================
+ Hits        12328    12333       +5     
+ Misses         42       38       -4     
+ Partials       55       54       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stefan6419846
stefan6419846 merged commit e2940a5 into py-pdf:main Nov 20, 2026
20 checks passed
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.

2 participants