Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Append pdf with named destination using numbers for pages #1858

Merged
merged 8 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2858,8 +2858,29 @@ 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 "/Names" in self._root_object and dest["/Title"] in cast( # noqa: SIM114
list,
cast(
DictionaryObject,
cast(DictionaryObject, 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"]]
assert p.indirect_reference is not None
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
27 changes: 27 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Fit,
IndirectObject,
NameObject,
NullObject,
NumberObject,
RectangleObject,
StreamObject,
Expand Down Expand Up @@ -1343,6 +1344,32 @@ def test_iss1767():
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
w2 = PdfWriter()
w2.add_blank_page(100, 100)
dest = w2.add_named_destination("toto", 0)
dest.get_object()[NameObject("/D")][0] = NullObject()
b = BytesIO()
w2.write(b)
b.seek(0)
w.append(b)
assert len(w._root_object["/Names"]["/Dests"]["/Names"]) == 6


@pytest.mark.parametrize(
("write_data_here", "needs_cleanup"),
[
Expand Down