Skip to content

Commit f5c5747

Browse files
m32m32
andauthored
BUG: Unterminated object when using PdfWriter with incremental=True (#3345)
Closes #3118. --------- Co-authored-by: m32 <grzegorz.makarewicz@gmail.com>
1 parent af645a4 commit f5c5747

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pypdf/_writer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ def _write_increment(self, stream: StreamType) -> None:
14951495
)
14961496
)
14971497
xr.write_to_stream(stream)
1498-
stream.write(f"\nstartxref\n{xref_location}\n%%EOF\n".encode()) # eof
1498+
stream.write(f"\nendobj\nstartxref\n{xref_location}\n%%EOF\n".encode()) # eof
14991499

15001500
def _write_pdf_structure(self, stream: StreamType) -> Tuple[List[int], List[int]]:
15011501
object_positions = []

tests/test_writer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,3 +2769,16 @@ def test_insert_filtered_annotations__annotations_are_no_list(caplog):
27692769
"'/Normal'} of type DictionaryObject."
27702770
)
27712771
]
2772+
2773+
2774+
def test_unterminated_object__with_incremental_writer():
2775+
"""Test for #3118"""
2776+
reader = PdfReader(RESOURCE_ROOT / "bytes.pdf")
2777+
writer = PdfWriter(reader, incremental=True)
2778+
2779+
writer.add_blank_page(72, 72)
2780+
2781+
fi = BytesIO()
2782+
writer.write(fi)
2783+
b = fi.getvalue()
2784+
assert b[-39:] == b"\nendstream\nendobj\nstartxref\n1240\n%%EOF\n"

0 commit comments

Comments
 (0)