PageObject member function compress_content_streams #2485
-
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf", "rb")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
for page in writer.pages:
page.compress_content_streams()
with open("output.pdf", "wb") as fd:
writer.write(fd)
I tried this but the content page links also do not work: from pypdf import PdfReader, PdfWriter
reader = PdfReader("big-old-file.pdf")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
writer.add_metadata(reader.metadata)
with open("smaller-new-file.pdf", "wb") as fp:
writer.write(fp) https://github.com/py-pdf/pypdf/blob/main/docs/user/file-size.md |
Beta Was this translation helpful? Give feedback.
Answered by
stefan6419846
Feb 28, 2024
Replies: 1 comment 1 reply
-
You probably want to use writer = PdfWriter(clone_from="input.pdf")
for page in writer.pages:
page.compress_content_streams()
writer.write("output.pdf") While there might be more Pythonic constructs to simplify the loop (for example using list comprehension or |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
stefan6419846
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
You probably want to use
clone_from
:While there might be more Pythonic constructs to simplify the loop (for example using list comprehension or
map
), I think that this just makes the action less obvious.