Skip to content

Commit 07f26f7

Browse files
ROB: Deal with annotations not being lists on merge (#3321)
Closes #3320.
1 parent a5845a5 commit 07f26f7

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pypdf/_writer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@ def _process_named_dests(dest: Any) -> None:
28532853
if "/Annots" not in excluded_fields:
28542854
for pag in srcpages.values():
28552855
lst = self._insert_filtered_annotations(
2856-
pag.original_page.get("/Annots", ()), pag, srcpages, reader
2856+
pag.original_page.get("/Annots", []), pag, srcpages, reader
28572857
)
28582858
if len(lst) > 0:
28592859
pag[NameObject("/Annots")] = lst
@@ -3020,6 +3020,9 @@ def _insert_filtered_annotations(
30203020
annots = cast("List[Any]", annots.get_object())
30213021
if annots is None:
30223022
return outlist
3023+
if not isinstance(annots, list):
3024+
logger_warning(f"Expected list of annotations, got {annots} of type {annots.__class__.__name__}.", __name__)
3025+
return outlist
30233026
for an in annots:
30243027
ano = cast("DictionaryObject", an.get_object())
30253028
if (

tests/test_writer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,3 +2748,24 @@ def test_insert_filtered_annotations__link_without_destination():
27482748
annots=annotations, page=writer.pages[0], pages={}, reader=reader
27492749
)
27502750
assert result == []
2751+
2752+
2753+
@pytest.mark.enable_socket
2754+
def test_insert_filtered_annotations__annotations_are_no_list(caplog):
2755+
"""Tests for #3320"""
2756+
url = "https://github.com/user-attachments/files/20818089/bugpdf.pdf"
2757+
name = "issue3320.pdf"
2758+
source_data = BytesIO(get_data_from_url(url, name=name))
2759+
reader = PdfReader(source_data)
2760+
writer = PdfWriter()
2761+
writer.append(reader)
2762+
font_file2 = reader.get_object(36).indirect_reference
2763+
assert caplog.messages == [
2764+
(
2765+
f"Expected list of annotations, got {{'/FontFile2': {font_file2!r}, "
2766+
"'/Descent': -269, '/CapHeight': 714, '/FontWeight': 300, '/FontName': '/JQJGLF+OpenSans-Light', "
2767+
"'/ItalicAngle': 0, '/StemV': 48, '/Type': '/FontDescriptor', '/FontBBox': [-521, -269, 1140, 1048], "
2768+
"'/FontFamily': 'Open Sans Light', '/Flags': 32, '/XHeight': 531, '/Ascent': 1048, '/FontStretch': "
2769+
"'/Normal'} of type DictionaryObject."
2770+
)
2771+
]

0 commit comments

Comments
 (0)