Skip to content
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
13 changes: 7 additions & 6 deletions pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,10 @@ def _get_named_destinations(
names = cast(DictionaryObject, tree[CA.NAMES])
i = 0
while i < len(names):
original_key = names[i].get_object()
key = names[i].get_object()
i += 1
if not isinstance(original_key, (bytes, str)):
if not isinstance(key, (bytes, str)):
continue
key = str(original_key)
try:
value = names[i].get_object()
except IndexError:
Expand All @@ -502,7 +501,9 @@ def _get_named_destinations(
continue
dest = self._build_destination(key, value)
if dest is not None:
retval[key] = dest
retval[cast(str, dest["/Title"])] = dest
# Remain backwards-compatible.
retval[str(key)] = dest
else: # case where Dests is in root catalog (PDF 1.7 specs, §2 about PDF 1.1)
for k__, v__ in tree.items():
val = v__.get_object()
Expand Down Expand Up @@ -928,7 +929,7 @@ def get_destination_page_number(self, destination: Destination) -> Optional[int]

def _build_destination(
self,
title: str,
title: Union[str, bytes],
array: Optional[
list[
Union[NumberObject, IndirectObject, None, NullObject, DictionaryObject]
Expand All @@ -948,7 +949,7 @@ def _build_destination(
try:
return Destination(title, page, Fit(fit_type=typ, fit_args=array)) # type: ignore
except PdfReadError:
logger_warning(f"Unknown destination: {title} {array}", __name__)
logger_warning(f"Unknown destination: {title!r} {array}", __name__)
if self.strict:
raise
# create a link to first Page
Expand Down
2 changes: 1 addition & 1 deletion pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ class Destination(TreeObject):

def __init__(
self,
title: str,
title: Union[str, bytes],
page: Union[NumberObject, IndirectObject, NullObject, DictionaryObject],
fit: Fit,
) -> None:
Expand Down
278 changes: 275 additions & 3 deletions tests/test_doc_common.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""Test the pypdf._doc_common module."""
import itertools
import re
import shutil
import subprocess
from io import BytesIO
from operator import itemgetter
from pathlib import Path
from unittest import mock

import pytest

from pypdf import PdfReader, PdfWriter
from pypdf.generic import EmbeddedFile, NullObject, ViewerPreferences
from pypdf.generic import EmbeddedFile, NullObject, TextStringObject, ViewerPreferences
from tests import get_data_from_url

TESTS_ROOT = Path(__file__).parent.resolve()
Expand Down Expand Up @@ -121,6 +123,7 @@ def test_byte_encoded_named_destinations():
if action["/S"] == "/GoTo":
named_dest = action["/D"]
assert str(named_dest) in reader.named_destinations
assert TextStringObject(named_dest) in reader.named_destinations

assert reader.named_destinations == {
"Doc-Start": {
Expand All @@ -131,14 +134,23 @@ def test_byte_encoded_named_destinations():
"/Top": 667.198,
"/Zoom": NullObject()
},
"楣整搮捡귃㉫㈰爵捡牥汦杩瑨敷杩瑨瑳瑡捩慤慴": {
"/Title": "楣整搮捡귃㉫㈰爵捡牥汦杩瑨敷杩瑨瑳瑡捩慤慴",
"cite.dacÃ\xadk2025racerflightweightstaticdata": {
"/Title": "cite.dacÃ\xadk2025racerflightweightstaticdata",
"/Page": page.indirect_reference,
"/Type": "/XYZ",
"/Left": 133.768,
"/Top": 614.424,
"/Zoom": NullObject()
},
# This is the same as the previous entry, but with `str(name)` instead of the title.
"楣整搮捡귃㉫㈰爵捡牥汦杩瑨敷杩瑨瑳瑡捩慤慴": {
"/Left": 133.768,
"/Page": page.indirect_reference,
"/Title": "cite.dacÃ\xadk2025racerflightweightstaticdata",
"/Top": 614.424,
"/Type": "/XYZ",
"/Zoom": NullObject()
},
"page.1": {
"/Title": "page.1",
"/Page": page.indirect_reference,
Expand Down Expand Up @@ -177,3 +189,263 @@ def test_named_destinations__tree_is_null_object():
reader = PdfReader(BytesIO(get_data_from_url(url=url, name=name)))

assert reader.named_destinations == {}


@pytest.mark.enable_socket
def test_outline__issue3462():
url = "https://github.com/user-attachments/files/22293402/e371fffe0b_a7cccde95a.pdf"
name = "issue3462.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url=url, name=name)))

outline_flat = list(
itertools.chain.from_iterable(
entry if isinstance(entry, list) else [entry] for entry in reader.outline
)
)
assert list(map(itemgetter("/Title"), outline_flat)) == [
"AR 2021 - Daftar Isi",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"AR 2021 Book 001 (Highlights - Ikhtisar Saham)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"AR 2021 Book 002 (Laporan Manajemen)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"AR 2021 Book 003-1 (Profil Perusahaan)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"Page 13",
"Page 14",
"Page 15",
"Page 16",
"Page 17",
"Page 18",
"Page 19",
"Page 20",
"Page 21",
"Page 22",
"Page 23",
"Page 24",
"Page 25",
"Page 26",
"Page 27",
"Page 28",
"Page 29",
"Page 30",
"Page 31",
"Page 32",
"Page 33",
"Page 34",
"Page 35",
"Page 36",
"Page 37",
"Page 38",
"Page 39",
"Page 40",
"Page 41",
"Page 42",
"Page 43",
"Page 44",
"Page 45",
"Page 46",
"Page 47",
"AR 2021 Book 003-2 (Sumber Daya Manusia)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"AR 2021 Book 003-3 (Komposisi pemegang saham)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"AR 2021 Book 003-4 (Kronologis Pencatatan Saham)",
"Page 1",
"Page 2",
"AR 2021 Book 003-5 (Akuntan Publik Independen)",
"Page 1",
"Page 2",
"Page 3",
"AR 2021 Book 004 (Analisa dan Pembahasan Manajemen)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"Page 13",
"Page 14",
"Page 15",
"Page 16",
"Page 17",
"Page 18",
"Page 19",
"Page 20",
"Page 21",
"AR 2021 Book 005-1 (Tata Kelola Perusahaan)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"AR 2021 Book 005-2 (Direksi-Komisaris)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"Page 13",
"Page 14",
"Page 15",
"Page 16",
"Page 17",
"Page 18",
"Page 19",
"Page 20",
"Page 21",
"Page 22",
"Page 23",
"Page 24",
"Page 25",
"Page 26",
"Page 27",
"Page 28",
"Page 29",
"Page 30",
"Page 31",
"Page 32",
"Page 33",
"Page 34",
"Page 35",
"Page 36",
"Page 37",
"Page 38",
"AR 2021 Book 005-3 (Komite Audit)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"AR 2021 Book 005-4 (Sekretaris Perusahaan)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"AR 2021 Book 005-5 (Unit Audit Internal)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"AR 2021 Book 005-6 (Sistem Pengendalian Internal)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"AR 2021 Book 005-7 (Program Saham)",
"Page 1",
"AR 2021 Book 005-8 ( Whistleblowing)",
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5",
"Page 6",
"Page 7",
"Page 8",
"Page 9",
"Page 10",
"Page 11",
"Page 12",
"Page 13",
"Page 14",
"Page 15",
"Page 16",
"Page 17",
"Page 18",
"Page 19",
"Page 20",
"Page 21",
"Page 22",
"Page 23",
"Page 24",
"Page 25",
"AR 2021 Book 006 (Tanggung Jawab Sosial - CSR)",
"Page 1",
"Page 2",
"AR 2021 Book 007-1 (LAPORAN KEUANGAN KONSOLIDASIAN)",
"Page 1",
"AR 2021 Book 007-2 (Isi Laporan Keuangan)",
"AR 2021 Book 008 (Tanggung Jawab Atas Laporan Tahunan)",
"Page 1",
"Page 2"
]
2 changes: 1 addition & 1 deletion tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ def test_issue604(caplog, strict):
pdf = PdfReader(f, strict=strict)
outline = pdf.outline
msg = [
"Unknown destination: ms_Thyroid_2_2020_071520_watermarked.pdf [0, 1]"
"Unknown destination: 'ms_Thyroid_2_2020_071520_watermarked.pdf' [0, 1]"
]
assert normalize_warnings(caplog.text) == msg

Expand Down
Loading