Skip to content

ROB: Fix merging documents if there are no Dests #3280

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

Merged
merged 1 commit into from
May 8, 2025
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
4 changes: 2 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2796,8 +2796,8 @@ def _process_named_dests(dest: Any) -> None:
List[Any],
cast(
DictionaryObject,
cast(DictionaryObject, self._root_object["/Names"])["/Dests"],
)["/Names"],
cast(DictionaryObject, self._root_object["/Names"]).get("/Dests", DictionaryObject()),
).get("/Names", DictionaryObject()),
):
# already exists: should not duplicate it
pass
Expand Down
21 changes: 21 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any
from unittest import mock

import pytest

Expand All @@ -23,6 +24,7 @@
from pypdf.generic import (
ArrayObject,
ContentStream,
Destination,
DictionaryObject,
Fit,
IndirectObject,
Expand Down Expand Up @@ -2693,3 +2695,22 @@ def test_compress_identical_objects__after_remove_images():
writer = PdfWriter(clone_from=RESOURCE_ROOT / "AutoCad_Diagram.pdf")
writer.remove_images()
writer.compress_identical_objects(remove_identicals=True, remove_orphans=True)


def test_merge__process_named_dests__no_dests_in_source_file():
"""Test for #3279"""
writer = PdfWriter(clone_from=RESOURCE_ROOT / "crazyones.pdf")

# Hacky solution to avoid attribute errors.
names = DictionaryObject()
names.indirect_reference = names
writer.root_object[NameObject("/Names")] = names

reader = PdfReader(RESOURCE_ROOT / "hello-world.pdf")
destination = Destination(title="test.pdf", page=reader.pages[0], fit=Fit("/Fit"))
with mock.patch.object(reader, "_get_named_destinations", return_value={"test.pdf": destination}):
writer.append(reader)
# The page now points to the appended one.
assert writer.named_destinations == {
"test.pdf": Destination(title="test.pdf", page=writer.pages[1].indirect_reference, fit=Fit("/Fit"))
}
Loading