Skip to content

Commit

Permalink
ROB: merge documents with named destinations with invalid page
Browse files Browse the repository at this point in the history
closes #2842
  • Loading branch information
pubpub-zz committed Sep 18, 2024
1 parent c4cdb5c commit ef587dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2702,7 +2702,10 @@ def merge(
reader._namedDests = (
reader.named_destinations
) # need for the outline processing below
for dest in reader._namedDests.values():

arr: Any

def _process_named_dests(dest: Any) -> None:
arr = dest.dest_array
if "/Names" in self._root_object and dest["/Title"] in cast(
List[Any],
Expand All @@ -2718,7 +2721,10 @@ def merge(
elif isinstance(dest["/Page"], int):
# the page reference is a page number normally not a PDF Reference
# page numbers as int are normally accepted only in external goto
p = reader.pages[dest["/Page"]]
try:
p = reader.pages[dest["/Page"]]
except IndexError:
return
assert p.indirect_reference is not None
try:
arr[NumberObject(0)] = NumberObject(
Expand All @@ -2733,6 +2739,9 @@ def merge(
].indirect_reference
self.add_named_destination_array(dest["/Title"], arr)

for dest in reader._namedDests.values():
_process_named_dests(dest)

outline_item_typ: TreeObject
if outline_item is not None:
outline_item_typ = cast(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2468,3 +2468,15 @@ def test_increment_writer(caplog):
assert writer.metadata is None
b = BytesIO()
writer.write(b)


@pytest.mark.enable_socket()
def test_append_pdf_with_dest_without_page(caplog):
"""Tests for #2842"""
url = "https://github.com/user-attachments/files/16990834/test.pdf"
name = "iss2842.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
writer = PdfWriter()
writer.append(reader)
assert "/__WKANCHOR_8" not in writer.named_destinations
assert len(writer.named_destinations) == 3

0 comments on commit ef587dc

Please sign in to comment.