From 752d2266a4620ec7a5b379f1b7b031c40558c03e Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sun, 20 Aug 2023 10:03:04 +0200 Subject: [PATCH] PI: Re-use content stream --- pypdf/_page.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pypdf/_page.py b/pypdf/_page.py index 6fb5feb92..e2c55a48a 100644 --- a/pypdf/_page.py +++ b/pypdf/_page.py @@ -882,12 +882,17 @@ def _content_stream_rename( @staticmethod def _push_pop_gs( - contents: Any, pdf: Union[None, PdfReaderProtocol, PdfWriterProtocol] + contents: Any, + pdf: Union[None, PdfReaderProtocol, PdfWriterProtocol], + use_original: bool = True, ) -> ContentStream: # adds a graphics state "push" and "pop" to the beginning and end # of a content stream. This isolates it from changes such as # transformation matricies. - stream = ContentStream(contents, pdf) + if use_original: + stream = contents + else: + stream = ContentStream(contents, pdf) stream.operations.insert(0, ([], "q")) stream.operations.append(([], "Q")) return stream @@ -1120,7 +1125,7 @@ def _merge_page( original_content = self.get_contents() if original_content is not None: new_content_array.append( - PageObject._push_pop_gs(original_content, self.pdf) + PageObject._push_pop_gs(original_content, self.pdf, use_original=True) ) page2content = page2.get_contents()