diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 29125b2cc3..f36381dbe9 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2194,6 +2194,7 @@ def clean(content: ContentStream) -> None: i += 1 else: i += 1 + content.get_data() # this ensures ._data is rebuilt try: d = cast(dict, cast(DictionaryObject, page["/Resources"])["/XObject"]) diff --git a/pypdf/generic/_data_structures.py b/pypdf/generic/_data_structures.py index 47eef04e1a..9b15350e36 100644 --- a/pypdf/generic/_data_structures.py +++ b/pypdf/generic/_data_structures.py @@ -854,7 +854,7 @@ def write_to_stream( deprecate_no_replacement( "the encryption_key parameter of write_to_stream", "5.0.0" ) - self[NameObject(SA.LENGTH)] = NumberObject(len(self.get_data())) + self[NameObject(SA.LENGTH)] = NumberObject(len(self._data)) DictionaryObject.write_to_stream(self, stream) del self[SA.LENGTH] stream.write(b"\nstream\n") diff --git a/tests/test_writer.py b/tests/test_writer.py index e71cf5b7f3..bc448fdec3 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -312,6 +312,8 @@ def test_remove_images(pdf_file_path, input_path): page = reader.pages[0] writer.insert_page(page, 0) writer.remove_images() + page_contents_stream = writer.pages[0]["/Contents"]._data + assert len(page_contents_stream.strip()) # finally, write "output" to pypdf-output.pdf with open(pdf_file_path, "wb") as output_stream: