Skip to content

ROB: Skip a font resource whose reference resolves to nothing - #4481

Open
Krushil-Dhankecha wants to merge 1 commit into
py-pdf:mainfrom
Krushil-Dhankecha:rob-layout-mode-dangling-font-reference
Open

Krushil-Dhankecha wants to merge 1 commit into
py-pdf:mainfrom
Krushil-Dhankecha:rob-layout-mode-dangling-font-reference

Conversation

@Krushil-Dhankecha

@Krushil-Dhankecha Krushil-Dhankecha commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4480.

_layout_mode_fonts() read each entry of the /Font resources as
font_resources[font_name].get_object(). DictionaryObject.__getitem__ resolves the
reference already, and PdfReader.get_object() answers None for an object number the
file does not hold, so the second call landed on None and
extract_text(extraction_mode="layout") raised AttributeError. The isinstance check on
the next line shows such an entry was meant to be skipped, and _extract_text() does skip
it, because it runs the same lookup inside except (AttributeError, TypeError). Plain and
layout extraction therefore disagreed on the same page.

The lookup now resolves only what it can resolve, and a reference to nothing falls through
to the existing isinstance check.

Two tests, both failing on the parent commit:

  • test_extract_text__none_objects gains the layout-mode call on page 8 of
    tika-957721.pdf. That test exists for this file's unresolvable objects and already
    covers plain extraction of the same page; 92 of the file's 104 pages raise without this
    change.
  • test_page_object__layout_mode_fonts__dangling_font_reference builds the same shape
    offline, pointing crazyones.pdf's font resources at an object number that is not in the
    file.

AI use, as asked for in CONTRIBUTING: I used Claude Code (Claude Opus 5) while writing this change. The diff and the reasoning behind it are mine and I have checked both.

PageObject._layout_mode_fonts() called get_object() on every entry of the
/Font resources. A reference to an object the file does not hold resolves
to None, so extract_text(extraction_mode="layout") raised AttributeError
instead of skipping the font, while the plain mode already tolerated it.
tika-957721.pdf fails this way on 92 of its 104 pages; its page 8 is the
one test_extract_text__none_objects already covers for plain extraction.
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4481   +/-   ##
=======================================
  Coverage   99.21%   99.21%           
=======================================
  Files          59       59           
  Lines       12425    12427    +2     
  Branches     2368     2369    +1     
=======================================
+ Hits        12328    12330    +2     
  Misses         42       42           
  Partials       55       55           

☔ 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.

Comment thread pypdf/_page.py
for font_name in font_resources:
font_object = font_resources[font_name].get_object()
# A dangling reference resolves to None, which has no get_object().
font_object: PdfObject | None = font_resources[font_name]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original call to get_object() was already redundant, because accessing a DictionaryObject by key will always return a resolved object. It will by definition return a PdfObject and never None.

Comment thread pypdf/_page.py
font_object = font_resources[font_name].get_object()
# A dangling reference resolves to None, which has no get_object().
font_object: PdfObject | None = font_resources[font_name]
if font_object is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... so this if branch can be removed.

@PJBrs

PJBrs commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR! I sort of was expecting this one :-)

I left two comments, but other than that the fix seems OK to me.

Two questions though.
One: Why did you add two tests for what really is a one-line fix? In fact, this line might already be covered by existing tests (I actually tried a patch like yours myself and I remember the new if-case already having been covered, might misremember though...) The maintainers might see this differently, by the way.

Two: I can't help wondering what would happen if we'd add a default font instead of skipping the font entirely, by calling Font.from_core_font_name(), which returns a Type1 Helvetica font. Anyway, that's probably not for this PR, but I am curious :-)

Hope this helps!

This branch has not been deployed

No deployments
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.

Layout-mode text extraction raises AttributeError for a dangling font reference

2 participants