Skip to content

Commit 56cf557

Browse files
BUG: PdfWriter.append fails when there are articles being None (#3509)
Closes #3508
1 parent 248e741 commit 56cf557

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ history and [GitHub's 'Contributors' feature](https://github.com/py-pdf/pypdf/gr
3939
* [Mérino, Antoine](https://github.com/Merinorus)
4040
* [Murphy, Kevin](https://github.com/kmurphy4)
4141
* [nalin-udhaar](https://github.com/nalin-udhaar)
42+
* [Noah-Houghton](https://github.com/Noah-Houghton) | [LinkedIn](https://www.linkedin.com/in/noah-h-554992a0/)
4243
* [Paramonov, Alexey](https://github.com/alexey-v-paramonov)
4344
* [Paternault, Louis](https://framagit.org/spalax)
4445
* [Perrensen, Olsen](https://github.com/olsonperrensen)

pypdf/_writer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3003,7 +3003,10 @@ def add_filtered_articles(
30033003
for p in pages.values():
30043004
pp = p.original_page
30053005
for a in pp.get("/B", ()):
3006-
thr = a.get_object().get("/T")
3006+
a_obj = a.get_object()
3007+
if is_null_or_none(a_obj):
3008+
continue
3009+
thr = a_obj.get("/T")
30073010
if thr is None:
30083011
continue
30093012
thr = thr.get_object()

tests/example_files.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,5 @@
120120
url: https://github.com/user-attachments/files/21578875/layout-parser-paper-with-empty-pages.pdf
121121
- local_filename: issue-3429.pdf
122122
url: https://github.com/user-attachments/files/21711469/bomb.pdf
123+
- local_filename: issue-3508.pdf
124+
url: https://github.com/user-attachments/files/23211824/repair-manual-thermo-230-300-350-2012-en.pdf

tests/test_merger.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pypdf.generic import Destination, Fit
1111

1212
from . import get_data_from_url
13+
from .test_encryption import HAS_AES
1314

1415
TESTS_ROOT = Path(__file__).parent.resolve()
1516
PROJECT_ROOT = TESTS_ROOT.parent
@@ -399,6 +400,15 @@ def test_articles_with_writer(caplog):
399400
assert r.threads[0].get_object()["/F"]["/P"] == r.pages[0]
400401

401402

403+
@pytest.mark.skipif(not HAS_AES, reason="No AES implementation")
404+
@pytest.mark.enable_socket
405+
def test_null_articles_with_writer():
406+
data = get_data_from_url(name="issue-3508.pdf")
407+
merger = PdfWriter()
408+
merger.append(BytesIO(data))
409+
assert len(merger.pages) == 98
410+
411+
402412
def test_get_reference():
403413
writer = PdfWriter(RESOURCE_ROOT / "crazyones.pdf")
404414
assert writer.get_reference(writer.pages[0]) == writer.pages[0].indirect_reference

0 commit comments

Comments
 (0)