PDF Compression: Two Libraries We Actually Ran, Byte Counts Verbatim

Two 10-page decks went through both libraries: deck A, text only at 6,394 bytes; deck B, the same pages carrying a full-page random-noise image at 1,929,577 bytes. On the text deck, PyMuPDF's save with garbage collection and deflate cut 38% (to 3,964 bytes) against pypdf's per-page stream compression at 11% (to 5,673 bytes). On the image deck, both tools' base compression moved the file by about a tenth of a percent — content-stream compression does not touch embedded images. The image savings came from a separate PyMuPDF pass: the lossless image rewrite took deck B from 1,929,577 down to 721,987 bytes, a 63% cut with no quality traded. One honest zero in the table: the lossy quality-50 pass produced a file byte-identical to the basic save on this deck — no change, reported as-run. And our first pypdf attempt failed outright with a real, quotable error (the page must belong to the writer before it can be compressed — verbatim below), which is an order lesson worth the read.

Day 12 of our 28-day series — next item on the gap list, and the newest page in the ran-it-ourselves line (PDF to Markdown, Markdown to PDF, split and merge, the metadata fix all precede it). Same rules as the rest of the series: tools we executed get rows with numbers from files in front of us; everything not run is listed with the reason; no prices anywhere, no invented precision.

The two test decks (reproduce in a minute)

Both built with PyMuPDF, 10 pages each, one heading and one body line per page. Deck A is text only: 6,394 bytes. Deck B is identical except a full-page 800×800 random-noise PNG sits on pages 3 and 8 — noise on purpose, the most incompressible content we could generate, the worst case for any compressor. Deck B came out at 1,929,577 bytes. Every size below is read back from the files on disk after the run; the only percentages are arithmetic on those counts.

Comparison table — both libraries ran, all numbers from the files

Pipeline (as run)Deck A: text-only, 6,394 BDeck B: noise image, 1,929,577 BWhat the run shows
pypdf: pages added to the writer first, then per-page stream compression5,673 B (11% smaller)1,928,783 B (0.04% smaller)content streams deflate; the noise image did not move
PyMuPDF: save with garbage collection at level 4 and deflate on3,964 B (38% smaller)1,927,481 B (0.1% smaller)same story on images, stronger base ratio on text
PyMuPDF: lossless image rewrite pass, then the same savenot run on this deck721,987 B (63% smaller)the only pass that moved the image bytes; lossless, no quality traded
PyMuPDF: lossy image rewrite at quality 50not run on this deck1,927,481 B (no change)byte-identical to the basic save on this deck; reported as-is, not explained away

Every output was verified after saving: 10 pages each, first-page text intact, the image object present on page 3, and the lossy output rendered to an image to confirm the picture survived the pass (it did — the render shows the same noise field, unchanged).

The runs, in order

Run 0 — the order lesson. Our first pypdf attempt called stream compression on pages taken straight from the reader, before they were added to a writer. The run failed with a verbatim error: ValueError: Page must be part of a PdfWriter. The compression method is a writer-side operation; pages taken from a reader cannot be compressed in place. Moving the call to the writer's pages, after adding them, is what made the run work. If your compression script dies with exactly that message, that is the reason.

Runs 1 and 2 — the text deck. pypdf: build a writer, add all ten pages, compress each writer page, write the file: 6,394 became 5,673. PyMuPDF: open, save with garbage collection and deflate: 6,394 became 3,964. Same content, same page count, first-page text intact in both outputs.

Runs 3 and 4 — the image deck. Both base routes produced essentially no savings: 1,928,783 bytes under pypdf and 1,927,481 under PyMuPDF. The 1.9 MB is the image, and content-stream compression never touches it. Run 5 — the lossless image rewrite: 721,987 bytes, cutting 63% with zero quality loss. Run 6 — the lossy pass at quality 50: 1,927,481 bytes, byte-identical to the basic save. We did not get a smaller file by trading quality, and we are not going to invent one: on this deck, in this run, the lossy pass changed nothing. Our deck's image was a PNG written by the library itself; whether the lossy pass behaves differently on JPEG-sourced files is listed below as not run.

The exact calls, from the runs

# pypdf — add pages to the writer FIRST, then compress the writer's pages
r = PdfReader("in.pdf")
w = PdfWriter()
for p in r.pages:
    w.add_page(p)
for p in w.pages:
    p.compress_content_streams()   # on reader pages this fails: "Page must be part of a PdfWriter"
with open("out.pdf", "wb") as f:
    w.write(f)

# PyMuPDF — base compression: garbage collection + stream deflate
d = pymupdf.open("in.pdf")
d.save("out.pdf", garbage=4, deflate=True)

# PyMuPDF — the image pass the save flags never touch
d2 = pymupdf.open("in.pdf")
d2.rewrite_images(lossy=False, lossless=True)
d2.save("out_images.pdf", garbage=4, deflate=True)

What we could NOT test — and refuse to guess

- Online PDF compressors — not run. Reason: upload-based services fail this site's privacy bar for confidential documents by design, and none were executed for this page.

- Ghostscript, qpdf and pdftk command-line compression — not run. Reason: this page's scope is the two Python libraries; the command-line tools were not executed, so no numbers for them appear here.

- Visual quality scoring of a successful lossy pass — not run. Reason: our lossy pass produced no size change on this deck, so there was nothing to score; no quality judgment is printed.

- The lossy pass on JPEG-sourced images — not run. Reason: our deck's image was a PNG; the no-change result is reported as-run rather than explained with internals we did not test.

Decision rule this run actually supports

- Text-heavy PDFs: PyMuPDF's save flags cut 38% in one call against 11% for pypdf's stream compression on our deck; either is a one-script job — pick whichever library is already in your pipeline.

- Image-heavy PDFs: base compression will not move them — about a tenth of a percent on our worst-case deck; the image rewrite pass is where the bytes are, and the lossless version of it cut 63% here without trading quality.

- Confidential documents: every run above executed locally with zero uploads; an upload-based compressor breaks that bar by design, ratings or not.

- If a lossy pass returns a byte-identical file on your input, as it did here: stop raising quality knobs, check what format your images are stored in, and try the lossless rewrite first.

Quick FAQ

Why did the lossy pass do nothing?

As-run: the output file was byte-identical to the basic save on our PNG-based deck. We report that observation alongside the 63% the lossless pass achieved on the same file; we did not chase the library's internal reasons and will not print guesses.

Is 38% versus 11% a ranking of the two libraries?

It is one text deck, one document class. Compression ratios depend heavily on the file; run your own file through both (the calls above, a minute of work) before deciding anything.

Can I reproduce your numbers?

Build the two decks the same way, run the code block above, and compare byte counts read back from disk. Exact numbers will differ with your content; the shape of the result — base flags ignore images, the image pass is a separate call — should hold.

This page continues the ran-it-ourselves line: split and merge, the metadata fix, and now compression. Bookmark pdf2mdtool.com and follow along.