Skip to content

measured · · 10 min

Choosing a PDF engine by measurement: eight engines, our own quotes

Before replacing headless Chrome in a CRM, I printed the same quotes with eight HTML-to-PDF engines and measured time and peak memory. Why WeasyPrint lost on speed, why a port of it was weeks for our templates and years for the rest, and why I forked an open-source Rust engine instead of writing one.

This is part 2 of the pdfblaze series. A CRM printed its quotes with a long-lived headless Chrome. On 2026-12-04 the staging worker that ran it froze, and a cold Chrome took about 976 MiB per document. I needed something that would print the same HTML templates with a fraction of the memory, and I needed to pick it that day.

So I did not pick from READMEs. I printed our own quotes with eight engines and measured each one. The one I picked, an open-source Rust engine (MIT/Apache-2.0), printed a quote in a median 22 ms at 25 MiB; WeasyPrint, the earlier plan, took 1.09 s at 80 MiB. I forked it as pdfblaze. This post is how the choice was made, what each loser failed on, and why a fork beat both a port and a rewrite.

What the replacement had to do

The templates were not mine to change. They were HTML and CSS that Chrome already printed well, so the bar was "print these as Chrome does", not "print something". What I checked each engine against:

  • Right-to-left text. One template is an Arabic quote. Letters have to join, and a table in a right-to-left document has to be mirrored.
  • Layout the templates already use. Flexbox and CSS grid, tables with borders, page breaks inside long tables.
  • Memory per document. The point of the exercise. Chrome's cold process sat near a gigabyte; a warm Chrome grew from 1,252 to 9,404 MiB over 52 renders.
  • Something Rails can call. A binary over standard input and output, or a worker process, is enough. A library that only another language can load is not.
  • A licence I can ship. MIT, Apache-2.0 or BSD.

Typst is fast and small, but it reads its own markup, not HTML. Choosing it would have meant rewriting every template, which is a different project. Zerodha's move to Typst (1.5M contract notes in about 25 minutes, down from about 8 hours) shows it is a good one; it was not this one.

The method

Each engine printed the same single-page quotes on a Mac, in a fresh process each time, and I took the median of 4 quotes. A small script started the process, timed it from start to exit, and read its peak resident memory from wait4. A fresh process is the worst case for an engine with a slow start, and the honest case for a CLI that a web app spawns per document.

Speed and memory alone would have picked the wrong engine, so every first page was also scored for fidelity: rasterised, and compared with WeasyPrint's render of the same quote, the look that had already been approved. The score is the harmonic mean of "the reference's ink has ink near it in the test page" and the reverse, so 100 means the same ink in the same places. Where an engine broke a template, the verdict says how.

figure 1 · eight engines, one quote
030609010 ms100 ms1 stime per quote · log scalepeak MiBbase engine 1.3.0

base engine 1.3.0 · median 22 ms, 25 MiB · 20–24 ms, 24–26 MiB, fidelity 94–100

Picked, and forked as pdfblaze. The fastest HTML engine here, low memory, and a fidelity score of 94–100 on the templates.

Each engine printed the same single-page quotes in a fresh process; dots are medians. Pick an engine to see its spread and the reason it was kept or dropped. Down and to the left is better.
EngineTime (s)Peak (MiB)Verdict
WeasyPrint 701.0980reference: the earlier plan
base engine 1.3.00.02225picked, forked
ferri-pdf 0.3.00.06930runner-up
sghtmltopdf0.04116broke the layout; no RTL
fullbleed0.11833Arabic letters not joined
Fulgur0.1832RTL table not mirrored
PlutoPrint0.3136no grid
Typst 0.150.02826every template would need a rewrite

A median hides the spread, so here are the ranges behind it:

EngineTimePeakFidelity score
WeasyPrint0.8–1.25 s78–83 MiB—
base engine20–24 ms24–26 MiB94–100
ferri-pdf34–114 ms28–33 MiB93–100
sghtmltopdf27–54 ms15–16 MiB—
Fulgur0.15–0.21 s30–34 MiB—
fullbleed94–148 ms31–36 MiB—
PlutoPrint0.23–0.32 s36–37 MiB—

Reading the scatter

Three things stand out.

Memory was not the tie-breaker I expected. Every engine except WeasyPrint sat between 16 and 36 MiB. Against a 976 MiB Chrome, all of them won. The memory goal was met by the whole field, so fidelity decided it.

The fast losers lost on what a customer would see. sghtmltopdf had the lowest memory, broke the layout and had no right-to-left support. fullbleed printed Arabic letters unjoined. Fulgur did not mirror the right-to-left table. Each of those is a quote a customer would send back.

The runner-up was close. ferri-pdf scored 93–100 against the picked engine's 94–100, at 34–114 ms. What ruled it out was not a measurement: it was 5 weeks old and had 1 author. A young engine from a single person is a bet that the person stays. I made a similar bet with the base engine, but the next sections say why it was a smaller one.

Why not WeasyPrint

WeasyPrint was the plan going in. I built the quote on it, the output looked right, and the look was approved. Then I measured it next to the others, and dropped it.

At about a second a page in a fresh process, a one-page quote is acceptable. Long documents are where it fell over. The decision table, measured on an arm64 machine except where it says Mac:

DocumentWeasyPrintferri-pdfbase engine
Normal quote, 1–2 pages0.69–1.23 s, 56–62 MiB0.05–0.13 s, 27–33 MiB20–24 ms, 24–26 MiB (Mac)
500 rows, 31 pages6.7 s, 128 MiB0.75 s, 102 MiB0.1 s, 45 MiB (Mac)
1,000 rows9.4 s, 198 MiB3.3 s, 172 MiBnot run
2,000 rows, 119 pages16.2 s, 338 MiB2.5–3.6 s, 316 MiBnot run

WeasyPrint grew by about 150 KiB per row, and at 2,000 rows its runs ranged from 16 s to past a 20 s limit.

Two conflicts, kept visible rather than smoothed over. An earlier run of the survey put WeasyPrint at 141 MiB and ferri-pdf at 117 MiB for 500 rows, against 128 and 102 in the table; they come from different runs on different machines. And one of my own notes says a 2,000-row WeasyPrint quote takes about 2.3 s. Nothing I measured comes close to that, so I treat it as doubtful.

The figure below puts the forked engine's own scaling (pdfblaze, measured later on the final build, on a Mac) beside the other two. The machines differ, so compare the slopes.

figure 2 · long invoices
100 ms1 s10 s500 rows1,000 rows2,000 rowsseconds · log scalepdfblaze 335 msferri-pdf 2.5–3.6 sWeasyPrint 16.2 s
engine5001,0002,000
pdfblaze (Mac)95 ms180 ms335 ms
ferri-pdf (arm64)0.75 s3.3 s2.5–3.6 s
WeasyPrint (arm64)6.7 s9.4 s16.2 s
pdfblaze was measured on a Mac; ferri-pdf and WeasyPrint on an arm64 machine, so compare the slopes rather than the exact gaps. Time is on a log scale: each gridline is ten times the one below it.

Why not port WeasyPrint to Rust

The other road was to keep WeasyPrint's behaviour and lose its runtime: port it. I sized that before saying no.

WeasyPrint is 26,523 lines of Python code (33,360 with comments and blank lines), 15.6 years old, with 198 contributors. Counted by module, in lines of WeasyPrint:

ModuleLines
HTML parsing4,641
CSS parsing and selectors3,675
Cascade7,056
Box tree1,782
Layout7,869
Text1,773, plus C libraries
Drawing and PDF4,032
SVG2,174

The "plus C libraries" on the text row is the trap. WeasyPrint leaves line breaking, bidi and font fallback to Pango, HarfBuzz and fontconfig through cffi. A Rust port has to replace all of that, and right-to-left text is exactly where three of the eight engines failed.

figure 3 · the cost of a port
  • Layout7,869 lines
  • Cascade7,056 lines
  • HTML parsing4,641 lines
  • Drawing and PDF4,032 lines
  • CSS parsing and selectors3,675 lines
  • SVG2,174 lines
  • Box tree1,782 lines
  • Text1,773 lines, plus C libraries
WeasyPrint is 26,523 lines of Python code, 15.6 years old, with 198 contributors. Switch to time: covering only our templates is weeks of work; matching it is years.

The estimate: 12–20 engineer-weeks for a port that covers our templates, and 1.5–3 engineer-years for the full feature set. The first number is tempting until you notice what it buys: an engine that prints our templates and nothing else, with every future template a new port.

For scale, the Rust engines that already exist landed between 78,704 and 221,976 lines:

EngineLines of Rust
sghtmltopdf92,513
base engine221,976
Fulgur139,941
rustyprint78,704

Those are raw wc -l counts of every .rs file, tests included. My own count of the forked tree's crates later came to 221,891 with a slightly different method; both describe the same engine.

Why fork, rather than depend or write

Writing an engine was the port's cost without the port's head start. Depending on the base engine as it was would have worked on the day. Forking it was the choice, for three reasons.

It measured best where it mattered. 20–24 ms, 24–26 MiB and a fidelity score of 94–100 on our templates, including the Arabic one.

It already had what I would otherwise have to build. The engine already had a long-running worker protocol, a sandbox on Linux, PDF/A and PDF/UA output, a corpus compared page by page against Chrome, and a release workflow with attestations. None of that is my work, and the fork keeps its upstream copyright in LICENSE-MIT.

A fork lets me fix what I find, on my schedule. The engine was young: 1.0.0 on 2026-11-20, 1.3.0 on 2026-11-30. Young code has bugs, and a CRM in production cannot wait for someone else's release. Within a day of the fork I halved the heap of a long invoice (part 3), and after a security audit I shipped two releases in one day (part 4). Neither would have been possible as a downstream user filing issues.

The cost of a fork is that every bug is now mine, including the ones I did not write. The audit found one in a dependency (part 5), and carrying its fix is a maintenance cost I took on by forking.

What I'd do differently, and what's still open

  • One machine for everything. The single-page numbers are from a Mac; most of the long-document numbers are from an arm64 machine. The conclusion survives the gap, but a clean comparison would run every engine on one box.
  • More than 4 quotes. A median of 4 is enough to rank engines whose ranges do not overlap. It is not enough to rank ferri-pdf against the base engine on speed; their ranges do.
  • Score against Chrome, not WeasyPrint. The fidelity reference was WeasyPrint's render, because that look had been approved earlier the same day. The real target was Chrome's print of the same template. The later comparison of 13 templates against Chrome (the overview) is the better test; the survey should have used it.
  • What other companies do was part of the decision, but I could not re-verify every row of that table against a primary source, so it is not in this post.
  • The base engine at 1,000 and 2,000 rows was not run in the survey. The forked engine's numbers in figure 2 come from after the memory work, so they are not a fair "before".

Part 3 starts the day after this choice: what the forked engine held in memory on a 119-page invoice, and how four commits took half of it away.

Working through something like this? I help teams ship AI and cloud systems that hold up, and cost what they should.