Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix merge_page sometimes generating unknown operator 'QQ' #2588

Merged
merged 6 commits into from
Apr 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
@@ -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(
13 changes: 13 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -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"""