Skip to content

Commit

Permalink
DOC: Add compression example (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 22, 2022
1 parent f0f1fa3 commit 668869f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
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.

0 comments on commit 668869f

Please sign in to comment.