Skip to content

Commit 561b1b0

Browse files
authored
BUG: Fix merge_page sometimes generating unknown operator 'QQ' (#2588)
Fixes #2587
1 parent ae0d27b commit 561b1b0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pypdf/generic/_data_structures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ def isolate_graphics_state(self) -> None:
12561256
self._operations.insert(0, ([], "q"))
12571257
self._operations.append(([], "Q"))
12581258
elif self._data:
1259-
self._data = b"q\n" + b_(self._data) + b"Q\n"
1259+
self._data = b"q\n" + b_(self._data) + b"\nQ\n"
12601260

12611261
# This overrides the parent method:
12621262
def write_to_stream(

tests/test_page.py

+13
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,19 @@ def test_pdf_pages_missing_type():
12681268
writer.pages[0]
12691269

12701270

1271+
@pytest.mark.enable_socket()
1272+
def test_merge_with_stream_wrapped_in_save_restore():
1273+
"""Test for issue #2587"""
1274+
url = "https://github.com/py-pdf/pypdf/files/14895914/blank_portrait.pdf"
1275+
name = "blank_portrait.pdf"
1276+
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
1277+
page_one = reader.pages[0]
1278+
assert page_one.get_contents().get_data() == b"q Q"
1279+
page_two = reader.pages[0]
1280+
page_one.merge_page(page_two)
1281+
assert b"QQ" not in page_one.get_contents().get_data()
1282+
1283+
12711284
@pytest.mark.samples()
12721285
def test_compression():
12731286
"""Test for issue #1897"""

0 commit comments

Comments
 (0)