Optimization · Node.js and TypeScript
Compress a PDF in Node.js
Shrink PDF file size from Node.js. 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 Node.js and TypeScript needs this
Node typically reaches for heavy wrappers or headless Chrome for anything past basic output, which is slow, fragile and never archival-grade.
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 Node.js with rust-pdf
Install the package, then call the same idiomatic API every rust-pdf binding shares. The snippet below is real Node.js code from the reference docs.
const ed = EditableDoc.loadFile("big.pdf");
ed.optimize().compact(true);
ed.save("small.pdf");
ed.close();
This is part of the free tier in Node.js. No license required.
Full Node.js reference in the documentation.
Compression in Node.js: FAQ
How much smaller will my PDF be in Node.js?
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 Node.js. No license is needed.
Compress and Optimize a PDF in Node.js
One Rust core, the same output across every language. Prototype for free, license the corporate features when you ship.