Optimization · Ruby
Compress a PDF in Ruby
Shrink PDF file size from Ruby. 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 Ruby needs this
Prawn generates beautiful PDFs but has no PDF/A, no digital signatures, no AES-256 and no PDF/UA, so the regulated features have simply been missing from Ruby.
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 Ruby with rust-pdf
Install the package, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real Ruby code from the reference docs.
ed = RustPdf::EditableDoc.load_file("big.pdf")
ed.optimize.compact(true)
ed.save("small.pdf")
ed.close
This is part of the free tier in Ruby. No license required.
Full Ruby reference in the documentation.
Compression in Ruby: FAQ
How much smaller will my PDF be in Ruby?
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 Ruby. No license is needed.
Compress and Optimize a PDF in Ruby
One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.