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.
Last updated: 2026-06-29
Why PHP needs this
Large PDFs are expensive to store, slow to email and painful to serve at scale. Much of that weight is structural: uncompressed streams, duplicated objects, a bloated cross-reference table, rather than the actual content on the page.
rust-pdf removes that overhead losslessly: it drops unreferenced objects, Flate-compresses uncompressed streams, dedupes byte-identical objects, and can pack everything into object streams behind a cross-reference stream. The rendered page is untouched, so the smaller file still passes qpdf and mutool.
Run as part of a queue worker, optimize() and compact() shrink generated invoices or user uploads before they hit storage. No Ghostscript or qpdf process to spawn from PHP. The binding arrives via composer require, ext-ffi links the native core, and the output is safe to re-serve byte for byte.
- 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 via Composer, then call the same idiomatic API every binding shares. The optimization snippet below is real PHP from the reference docs.
composer require rust-pdf/rustpdf
$ed = EditableDoc::loadFile('big.pdf');
$ed->optimize()->compact(true)->save('small.pdf');
Optimization is part of the free PHP tier — no license token required.
The complete optimization API is in the PHP 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.
Can I optimize PDFs in a worker without Ghostscript?
Yes. optimize() and compact() run entirely inside the native core, so a queue worker can shrink generated invoices or uploads before storing them. There is no Ghostscript or qpdf CLI to install on the box, and because the cleanup is lossless the result is byte-for-byte safe to re-serve.
Serve lighter PDFs from your PHP application
One Rust core, the same optimization in every language. Compression is free — you only need a license for corporate features such as PDF/A, signing and encryption.