Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Add compression example #792

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ You can contribute to `PyPDF2 on Github <https://github.com/py-pdf/PyPDF2>`_.
user/adding-pdf-annotations
user/forms
user/streaming-data
user/file-size


.. toctree::
Expand Down
7 changes: 6 additions & 1 deletion docs/user/comparisons.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ a large community behind it.
And there are more:


* [`pyfpdf`](https://github.com/reingart/pyfpdf)
* [`pdfplumber`](https://pypi.org/project/pdfplumber/)

## Document Generation

There are (Python) [tools to generate PDF documents](https://github.com/py-pdf/awesome-pdf#generators).
PyPDF2 is not one of them.
38 changes: 38 additions & 0 deletions docs/user/file-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Reduce PDF Size

There are multiple ways to reduce the size of a given PDF file. The easiest
one is to remove content (e.g. images) or pages.

## Remove images


```python
import PyPDF2

reader = PyPDF2.PdfFileReader("example.pdf")
writer = PyPDF2.PdfFileWriter()

for page in reader.pages:
writer.addPage(page)

writer.removeImages()

with open("out.pdf", "wb") as f:
writer.write(f)
```

## Compression

```python
import PyPDF2

reader = PyPDF2.PdfFileReader("example.pdf")
writer = PyPDF2.PdfFileWriter()

for page in reader.pages:
page.compressContentStreams()
writer.addPage(page)

with open("out.pdf", "wb") as f:
writer.write(f)
```
3 changes: 2 additions & 1 deletion docs/user/robustness.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ Choosing `strict=True` means that PyPDF2 will raise an exception if a PDF does
not follow the specification.

Choosing `strict=False` means that PyPDF2 will try to be forgiving and do
something reasonable, but it will log a warning message.
something reasonable, but it will log a warning message. It is a best-effort
approach.