Skip to content

Commit

Permalink
BUG: Append pdf with named destination using numbers for pages
Browse files Browse the repository at this point in the history
closes py-pdf#471

the issue was with named destination using numbers instead of indirect object to point pages. This is normally not expected.
  • Loading branch information
pubpub-zz committed May 23, 2023
1 parent 0096c99 commit 043da66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2755,8 +2755,25 @@ def merge(
) # need for the outline processing below
for dest in reader._namedDests.values():
arr = dest.dest_array
if isinstance(dest["/Page"], NullObject):
pass # self.add_named_destination_array(dest["/Title"],arr)
if ( # noqa: SIM114
"/Names" in self._root_object
and dest["/Title"] in self._root_object["/Names"]["/Dests"]["/Names"]
):
# already exists : should not duplicate it
pass
elif isinstance(dest["/Page"], NullObject):
pass
elif isinstance(dest["/Page"], int):
# the page reference is a page number normally not iaw Pdf Reference
# page numbers as int are normally accepted only in external goto
p = reader.pages[dest["/Page"]]
try:
arr[NumberObject(0)] = NumberObject(
srcpages[p.indirect_reference.idnum].page_number
)
self.add_named_destination_array(dest["/Title"], arr)
except KeyError:
pass
elif dest["/Page"].indirect_reference.idnum in srcpages:
arr[NumberObject(0)] = srcpages[
dest["/Page"].indirect_reference.idnum
Expand Down
17 changes: 17 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1339,3 +1339,20 @@ def test_iss1767():
name = "iss1723.pdf"
in_pdf = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
PdfWriter(clone_from=in_pdf)


@pytest.mark.enable_socket()
def test_named_dest_page_number():
"""
Closes iss471
tests appending with named destinations as integers
"""
url = "https://github.com/py-pdf/pypdf/files/10704333/central.pdf"
name = "central.pdf"
w = PdfWriter()
w.add_blank_page(100, 100)
w.append(BytesIO(get_pdf_from_url(url, name=name)), pages=[0, 1, 2])
assert len(w._root_object["/Names"]["/Dests"]["/Names"]) == 2
assert w._root_object["/Names"]["/Dests"]["/Names"][-1][0] == (1 + 1)
w.append(BytesIO(get_pdf_from_url(url, name=name)))
assert len(w._root_object["/Names"]["/Dests"]["/Names"]) == 6

0 comments on commit 043da66

Please sign in to comment.