From 2f0956be871ef0db5894a9a01c08d8492965f0d5 Mon Sep 17 00:00:00 2001 From: j-t-1 <120829237+j-t-1@users.noreply.github.com> Date: Thu, 29 Feb 2024 10:49:55 +0000 Subject: [PATCH] Improve lossless compression example Simplify the previous version and remove the side effect of changing the PDF in ways not just related to lossless compression (e.g. links on content page did not work in the created PDF). --- docs/user/file-size.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/docs/user/file-size.md b/docs/user/file-size.md index 07b5614a9..c4dc687c9 100644 --- a/docs/user/file-size.md +++ b/docs/user/file-size.md @@ -81,16 +81,11 @@ Deflate compression can be applied to a page via [`page.compress_content_streams`](https://pypdf.readthedocs.io/en/latest/modules/PageObject.html#pypdf._page.PageObject.compress_content_streams): ```python -from pypdf import PdfReader, PdfWriter - -reader = PdfReader("example.pdf") -writer = PdfWriter() +from pypdf import PdfWriter -for page in reader.pages: - writer.add_page(page) +writer = PdfWriter(clone_from="example.pdf") for page in writer.pages: - # ⚠️ This has to be done on the writer, not the reader! page.compress_content_streams() # This is CPU intensive! with open("out.pdf", "wb") as f: