Skip to content

Commit

Permalink
Improve lossless compression example (#2488)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
j-t-1 authored Feb 29, 2024
1 parent afbee38 commit f20c36e
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions docs/user/file-size.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f20c36e

Please sign in to comment.