Optimization · PHP

Compress a PDF in PHP

Shrink PDF file size from PHP. rust-pdf drops unreferenced objects, Flate-compresses uncompressed streams, dedupes byte-identical objects, and can pack everything into object streams with a cross-reference stream.

Why PHP needs this

PHP's classic libraries (TCPDF, FPDF) predate modern PDF and have no real PDF/A, no PAdES signatures and no AES-256, while the commercial alternatives are costly.

Optimization is lossless structural cleanup: nothing in the rendered page changes, the file just carries less weight. Combine optimize (dedupe and compress) with compact (object and xref streams) for the smallest valid output, ideal before archiving, emailing or serving documents at scale.

  • Drop unreferenced objects, Flate-compress streams and dedupe identical objects.
  • Pack objects into object streams and emit a cross-reference stream for the smallest size.
  • Lossless: the rendered page is unchanged, the output still passes qpdf and mutool.

Compress a PDF in PHP with rust-pdf

Install the package, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real PHP code from the reference docs.

PHP
$ed = EditableDoc::loadFile('big.pdf');
$ed->optimize()->compact(true)->save('small.pdf');
Validated by: qpdfmutool

This is part of the free tier in PHP. No license required.

Full PHP reference in the documentation.

Compression in PHP: FAQ

How much smaller will my PDF be in PHP?

It depends on the input. Files with uncompressed streams, duplicated objects or no object streams shrink the most. Optimization is lossless, so the savings come from structure, not from degrading content.

Does compression reduce quality?

No. This is structural optimization, not image down-sampling. The rendered page is identical; only redundant or uncompressed structure is removed, so the output still validates.

Is optimization free?

Yes. Optimize and compact are part of the free tier in PHP. No license is needed.

Compress and Optimize a PDF in PHP

One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.