Inkspan › Guides › How to compress a PDF in Node.js
PDFs grow for boring reasons: orphaned objects left behind by edits, duplicated resources, and uncompressed streams. The /v1/pdf/compress endpoint performs a lossless structural cleanup — garbage collection of unreferenced objects, stream deflation, and removal of redundant data — and returns a smaller file with identical visual output.
Because the optimisation is structural rather than lossy, savings depend entirely on the input. Files exported from design tools or edited repeatedly often shrink substantially; a lean, freshly-generated PDF may barely move.
import fs from 'node:fs';
const fd = new FormData();
fd.append('file', new Blob([fs.readFileSync('input.pdf')]), 'input.pdf');
const res = await fetch('https://api.inkspan.dev/v1/pdf/compress', {
method: 'POST',
headers: { 'X-API-Key': process.env.INKSPAN_KEY },
body: fd,
});
if (!res.ok) throw new Error(await res.text());
fs.writeFileSync('compressed.pdf', Buffer.from(await res.arrayBuffer()));
| Name | Type | Required | Description |
|---|---|---|---|
file | file | yes | The PDF to compress. |
Returns a PDF as a binary stream with Content-Type: application/pdf and Content-Disposition: attachment; filename=compressed.pdf. Write the response body straight to disk — do not parse it as JSON.
| Status | Meaning | Fix |
|---|---|---|
401 | Invalid or missing API key | Send your key in the X-API-Key header. |
402 | Monthly quota exceeded | The response reports used/quota. Upgrade at inkspan.dev/#pricing. |
422 | Missing or invalid parameter | Check required fields in the table above — the body names the offending field. |
Failed requests do not count against your quota; only successful ones are metered.
Yes. Compression is structural, so text, fonts, and metadata are preserved and the document remains searchable.
No. The endpoint applies one lossless profile — garbage collection, deflation, and cleanup — with no quality trade-off to tune.
It was probably already well-formed. Lossless optimisation can only remove waste, and some files have very little.
Get a free API key →Related Manipulate guides: merge PDF files · split a PDF into pages · add a watermark to a PDF · convert a PDF to images · fill a PDF form