Skip to content

We replaced Ghostscript with our own in-browser PDF engine

· 11 min read

Until the end of October 2026, five of our PDF tools — compress, grayscale, password-protect, unlock and repair — ran on Ghostscript compiled to WebAssembly. Ghostscript is a remarkable piece of software with nearly four decades of work behind it, and it did the job. We have now replaced it with a PDF engine of our own, written in TypeScript from the PDF specification. This is why, what it involved, and how we checked that it is actually better rather than merely ours.

Why replace something that works

Three reasons, in order of how much they annoyed us:

  • A 15 MB download before the first compression. Ghostscript is a complete PostScript and PDF interpreter with fonts, colour management and printer drivers. We used five of its operations. Every visitor paid for all of it.
  • Encryption topped out at 128-bit RC4. Ghostscript's PDF writer supports PDF encryption up to revision 3, so "password-protect" meant a cipher with known weaknesses. AES-256 has been part of the PDF standard for over a decade.
  • Licensing. Ghostscript is distributed under the AGPL, which attaches obligations to the whole application that loads it. Carrying those for a handful of operations was a poor trade.

The question was whether five operations could be done well without a general-purpose PDF interpreter. It turns out they can, because none of them needs to render a page. They need to read a PDF's structure, change parts of it, and write it back.

What we built

A parser that doesn't trust the file

A PDF ends with a cross-reference table listing where every object lives. Most parsers start there. Ours ignores it: it scans the file from start to finish for 12 0 obj headers, and when an object number appears twice, the later definition wins — which is exactly what PDF's incremental updates mean. Damaged cross-reference tables, wrong offsets, truncated downloads and files edited by careless tools all stop mattering. On output, the writer emits only objects reachable from the document's root, renumbered densely, so superseded revisions and orphaned data simply vanish.

That design is what makes repair work. When the page tree is lost, the engine rebuilds it from every page object it found. When a compressed stream is damaged, it keeps everything that decompressed before the damage and re-compresses that cleanly, so a viewer gets a valid, partial page instead of an error.

The standard security handler, both directions

Unlocking has to open whatever a user brings: 40- and 128-bit RC4 from old Acrobat versions, AES-128, and AES-256 in both its deprecated revision 5 and current revision 6 forms. Protecting writes revision 6 — AES-256 with the password run through the spec's deliberately slow, salted key derivation. Web Crypto offers SHA-2 but neither MD5 nor RC4, and calls into it are asynchronous, which is awkward when every string in a document needs decrypting. So MD5, RC4 and AES are implemented directly in TypeScript and checked against the official FIPS-197 and RFC test vectors and against Node's OpenSSL; only SHA-256/384/512 come from Web Crypto.

Compression that measures before it shrinks

Compression presets are expressed in dots per inch — 72, 150 or 300 — but an image's pixel count says nothing about its resolution until you know how large it is drawn. A 6,000-pixel photo filling a page is roughly 700 dpi; the same photo shown as a 2-inch thumbnail is 3,000. So the engine walks every page's content stream, tracking the transformation matrix through every q, Q and cm operator and into nested forms, and records the size at which each image is actually painted. Only images above 1.5× the preset's target are downsampled, using area averaging, then re-encoded with MozJPEG.

Whether an image becomes JPEG at all is decided by trying: lossless images are encoded both ways, and JPEG wins only when it is dramatically smaller — true for photos and scans, false for screenshots and line art, where JPEG would smear edges for little gain. At the high-quality preset, existing JPEGs that don't need downsampling are left untouched rather than recompressed. Then duplicate streams are merged, everything else is re-deflated at maximum compression, and small objects are packed into compressed object streams.

Grayscale without rasterising

Grayscale conversion rewrites colour wherever it can live: fill and stroke operators, colour spaces, images and their palettes, inline images, gradient shadings, tiling patterns, Type 3 font glyphs, form-field appearances and annotation colours. Spot colours and gradients are defined through PDF functions, so the engine includes an evaluator for all four function types — sampled, exponential, stitching, and a small PostScript calculator language — and replaces each gradient's colour function with a sampled gray one. Text stays text throughout.

How we tested it

A PDF engine that works on the files its author made is not much of a PDF engine, so most of the testing used other implementations as the reference.

  • Unlock: 112 files encrypted by qpdf in every scheme — RC4 40 and 128, AES-128 with and without encrypted metadata, AES-256, owner-only restrictions, Unicode passwords, with and without object streams — each unlocked with the user and with the owner password. All 224 results were byte-for-byte identical to the originals in every page and font stream.
  • Protect: every output opened in qpdf, in pdf.js (the viewer in Firefox and the engine of many web viewers) and in Apple's PDFKit, with the right password, refused the wrong one, and reported AES-256 revision 6.
  • Repair: 26 files damaged eight different ways — truncated at 60%, 90% and 95%, shifted offsets, missing cross-reference table and trailer, random bytes overwritten, mangled line endings, broken object streams. The engine recovered at least as many pages as qpdf in every case, and recovered pages from five files qpdf could not open at all.
  • Compress: eight documents at all three presets, against the Ghostscript build we used to ship, comparing file size and — by rendering every page with Apple's PDF renderer and measuring PSNR against the original — how faithful the result looks.

Some results at the default Balanced preset:

DocumentOriginalGhostscriptOur engineWorst-page fidelity (GS / ours)
Six full-page photos47 MB1.7 MB323 KB27.6 / 34.6 dB
Six-page colour scan19 MB1.2 MB558 KB39.2 / 38.9 dB
12-page report with photos6.6 MB75 KB53 KB43.5 / 47.2 dB
One large photo page3.4 MB682 KB206 KB46.0 / 43.6 dB
231-page text document1.2 MB978 KB880 KB41.5 dB / identical

Across all 24 file-and-preset combinations the output was smaller than Ghostscript's. Fidelity was equal or better in most, with honest exceptions: the single large photo above comes out a little softer, and one photo page at the high-quality preset scores 32.9 dB against Ghostscript's 35.0 — in a file under a third the size. On text documents the result is pixel-identical to the original, because nothing about the text changes; Ghostscript rewrites fonts, which shifts the rendering slightly.

Grayscale left no coloured pixels in any test file, including one built to contain every kind of colour PDF supports. Finally, all five tools were driven end to end in Chrome against the production build with its strict Content-Security-Policy, watching for any network request leaving the site. There were none.

Things that surprised us

  • TextDecoder("latin1") isn't Latin-1. Per the WHATWG encoding standard it is windows-1252, which remaps bytes 0x80–0x9F. Round-tripping binary PDF data through it quietly corrupts compressed streams. A test helper did exactly that before the engine ever saw the file.
  • Apple Preview normalises passwords differently. The PDF 2.0 spec says passwords are prepared with SASLprep, which composes accented characters. Apple's PDFKit decomposes them instead, so an AES-256 file protected with café — by us, by qpdf, by anything that follows the spec — won't open in Preview. Acrobat, Chrome and Firefox are fine. The protect tool now warns about accented passwords instead of breaking the spec to suit one viewer.
  • Chroma subsampling hurts graphics more than photos. MozJPEG halves colour resolution by default, which is invisible on photographs and very visible on coloured edges in UI screenshots. The high-quality preset keeps full colour resolution.
  • Progress messages can overtake results. A worker's last "100%" update, sent without waiting, arrived after the finished file and put the page back into its "working" state. Simple to fix once seen; invisible in unit tests.

What it doesn't do (yet)

  • Font subsetting. Ghostscript can strip unused glyphs from fully embedded fonts; we don't, so documents whose size is mostly fonts shrink less.
  • JPEG 2000 and CMYK JPEG images stay in colour in grayscale conversion, as do mesh gradients without a colour function. The tool tells you when a file contains any.
  • Very large photographs aren't faster. Six 36-megapixel photos take about 10 seconds at the high-quality preset, roughly what Ghostscript took; decoding, resampling and re-encoding that many pixels is simply work.

What it means if you use the tools

The PDF tools open faster, because there is no engine to download. Protected PDFs use AES-256. Compressed files are generally smaller. And the original promise is unchanged: everything runs in your browser and nothing is uploaded. You can confirm it the same way as always — open your browser's developer tools, watch the Network tab while a tool runs, or switch the connection off entirely and use it anyway. There's a short guide to that test in developer tools that work offline.