Skip to content

Commit

Permalink
BUG: Fix merge_page sometimes generating unknown operator 'QQ' (#2588)
Browse files Browse the repository at this point in the history
Fixes #2587
  • Loading branch information
rfotino committed Apr 7, 2024
1 parent ae0d27b commit 561b1b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pypdf/generic/_data_structures.py
Expand Up @@ -1256,7 +1256,7 @@ def isolate_graphics_state(self) -> None:
self._operations.insert(0, ([], "q"))
self._operations.append(([], "Q"))
elif self._data:
self._data = b"q\n" + b_(self._data) + b"Q\n"
self._data = b"q\n" + b_(self._data) + b"\nQ\n"

# This overrides the parent method:
def write_to_stream(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_page.py
Expand Up @@ -1268,6 +1268,19 @@ def test_pdf_pages_missing_type():
writer.pages[0]


@pytest.mark.enable_socket()
def test_merge_with_stream_wrapped_in_save_restore():
"""Test for issue #2587"""
url = "https://github.com/py-pdf/pypdf/files/14895914/blank_portrait.pdf"
name = "blank_portrait.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
page_one = reader.pages[0]
assert page_one.get_contents().get_data() == b"q Q"
page_two = reader.pages[0]
page_one.merge_page(page_two)
assert b"QQ" not in page_one.get_contents().get_data()


@pytest.mark.samples()
def test_compression():
"""Test for issue #1897"""
Expand Down

0 comments on commit 561b1b0

Please sign in to comment.