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.
Last updated: 2026-06-29
Why Node.js and TypeScript needs this
Shrinking a PDF in Node typically means spawning Ghostscript or qpdf, which adds a heavyweight binary to the image and a child process to every request.
Optimization is lossless structural cleanup: nothing in the rendered page changes, the file 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.
Run optimize().compact(true) before you store or email a document to cut payload size where serverless response and attachment limits bite. The pure-FFI Koffi binding has no node-gyp and no headless-Chrome footprint, so it stays a small native dependency in a Lambda layer or container image, and optimization is lossless so output still passes qpdf.
- 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 from npm, then chain optimize and compact on a loaded document. The snippet below is real Node.js from the reference docs.
npm install rustpdf
const ed = EditableDoc.loadFile("big.pdf");
ed.optimize().compact(true);
ed.save("small.pdf");
ed.close();
Optimization and compaction are part of the free tier in Node.js — no license token needed.
The full Node.js optimization reference is 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.
Does the binding add much weight to a serverless bundle?
No. The Koffi binding is pure FFI with no node-gyp and no headless-Chrome dependency, so it stays a small prebuilt native library you drop into a Lambda layer or container image, handy when you are optimizing PDFs to fit upload and response size limits.
Send lighter PDFs without touching how they look
One Rust core, the same lossless optimization in every language. Compression is free; license the corporate features only when you ship them.