Skip to content

BUG: Use page reference for existing internal link targets - #4076

Merged
stefan6419846 merged 4 commits into
py-pdf:mainfrom
r-kamei:fix-internal-link-destination-reference
Sep 15, 2026
Merged

stefan6419846 merged 4 commits into
py-pdf:mainfrom
r-kamei:fix-internal-link-destination-reference

Conversation

@r-kamei

@r-kamei r-kamei commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Summary

This change resolves pypdf's intermediate internal-link destination to the target page's indirect reference when the target page is already present in the writer.

When the target page has not yet been added, the existing page-index representation is preserved.

Scope

Based on the current code paths, this change is limited to pypdf's intermediate representation of an internal direct destination:

to_add.get("/Subtype") == "/Link"
and isinstance(destination, DictionaryObject)
and "target_page_index" in destination

Remote and external links should not be affected. URI links and remote or embedded go-to actions are represented through an /A action dictionary, such as /URI, /GoToR, or /GoToE, rather than through the annotation-level intermediate /Dest dictionary handled here.

An already completed explicit destination is represented as an array rather than this intermediate dictionary, so it is also left unchanged.

Compatibility

The discussion in #2450 raised the question of whether allowing an internal link to reference a page that has not yet been added to the writer should be treated as supported behavior or as a bug.
This PR intentionally does not make that decision. If the target page is not yet present in the writer, the existing page-index representation is preserved. The change only resolves the destination to an IndirectObject when the target page is already available.
The remaining behavior for targets added later can therefore be discussed and addressed separately without blocking this limited improvement.

Tests

The added tests verify that:

  • an existing target page is represented by its IndirectObject;
  • a target page that has not yet been added preserves the existing page-index representation; and
  • an already completed destination array is preserved without being processed as pypdf's intermediate destination.

AI usage

I used Microsoft 365 Copilot, based on the GPT-5 reasoning model, to assist with reviewing the code paths related to direct /Dest entries and remote link actions. I reviewed and verified the resulting implementation and tests myself.

@r-kamei r-kamei mentioned this pull request Sep 12, 2026
@codecov

codecov Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.12%. Comparing base (d26df36) to head (c1e7624).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4076   +/-   ##
=======================================
  Coverage   98.11%   98.12%           
=======================================
  Files          59       59           
  Lines       11489    11494    +5     
  Branches     2150     2151    +1     
=======================================
+ Hits        11273    11278    +5     
  Misses        122      122           
  Partials       94       94           

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

@r-kamei

r-kamei commented Sep 12, 2026

Copy link
Copy Markdown
Contributor Author

The remaining test_text_extraction_fast failure appears unrelated to this PR. The test PDF ASurveyofImageClassificationBasedTechniques.pdf was missing from tests/pdf_cache, which appears to match the CI download/cache issue reported in #3728.

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me.

@stefan6419846
stefan6419846 merged commit cf5cec2 into py-pdf:main Sep 15, 2026
33 of 34 checks passed
@r-kamei
r-kamei deleted the fix-internal-link-destination-reference branch September 15, 2026 09:12
stefan6419846 added a commit that referenced this pull request Sep 16, 2026
## What's new

### Security (SEC)
- Limit size of alphabetical page labels (#4096) by @stefan6419846

### Deprecations (DEP)
- Replace PdfWriter method add_js (#3979) by @j-t-1

### Performance Improvements (PI)
- Move static value out of loop body for appearance stream data (#4087) by @stefan6419846
- Reduce number of full data lookups for attachment mapping API (#4081) by @stefan6419846

### Bug Fixes (BUG)
- Do not copy unrelated pages when appending pages with non-terminal fields (#4078) by @anandghegde
- Use page reference for existing internal link targets (#4076) by @r-kamei
- Arabic-Indic digits are reversed during text extraction (#4077) by @Syamjith-NK
- Parse a string rect for add_uri into a rectangle (#4074) by @RavSinghChandan

[Full Changelog](6.18.1...6.19.0)
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