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.
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.
| Engine | Time (s) | Peak (MiB) | Verdict |
|---|---|---|---|
| WeasyPrint 70 | 1.09 | 80 | reference: the earlier plan |
| base engine 1.3.0 | 0.022 | 25 | picked, forked |
| ferri-pdf 0.3.0 | 0.069 | 30 | runner-up |
| sghtmltopdf | 0.041 | 16 | broke the layout; no RTL |
| fullbleed | 0.118 | 33 | Arabic letters not joined |
| Fulgur | 0.18 | 32 | RTL table not mirrored |
| PlutoPrint | 0.31 | 36 | no grid |
| Typst 0.15 | 0.028 | 26 | every template would need a rewrite |
A median hides the spread, so here are the ranges behind it:
| Engine | Time | Peak | Fidelity score |
|---|---|---|---|
| WeasyPrint | 0.8–1.25 s | 78–83 MiB | — |
| base engine | 20–24 ms | 24–26 MiB | 94–100 |
| ferri-pdf | 34–114 ms | 28–33 MiB | 93–100 |
| sghtmltopdf | 27–54 ms | 15–16 MiB | — |
| Fulgur | 0.15–0.21 s | 30–34 MiB | — |
| fullbleed | 94–148 ms | 31–36 MiB | — |
| PlutoPrint | 0.23–0.32 s | 36–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:
| Document | WeasyPrint | ferri-pdf | base engine |
|---|---|---|---|
| Normal quote, 1–2 pages | 0.69–1.23 s, 56–62 MiB | 0.05–0.13 s, 27–33 MiB | 20–24 ms, 24–26 MiB (Mac) |
| 500 rows, 31 pages | 6.7 s, 128 MiB | 0.75 s, 102 MiB | 0.1 s, 45 MiB (Mac) |
| 1,000 rows | 9.4 s, 198 MiB | 3.3 s, 172 MiB | not run |
| 2,000 rows, 119 pages | 16.2 s, 338 MiB | 2.5–3.6 s, 316 MiB | not 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.
| engine | 500 | 1,000 | 2,000 |
|---|---|---|---|
| pdfblaze (Mac) | 95 ms | 180 ms | 335 ms |
| ferri-pdf (arm64) | 0.75 s | 3.3 s | 2.5–3.6 s |
| WeasyPrint (arm64) | 6.7 s | 9.4 s | 16.2 s |
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:
| Module | Lines |
|---|---|
| HTML parsing | 4,641 |
| CSS parsing and selectors | 3,675 |
| Cascade | 7,056 |
| Box tree | 1,782 |
| Layout | 7,869 |
| Text | 1,773, plus C libraries |
| Drawing and PDF | 4,032 |
| SVG | 2,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.
- 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
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:
| Engine | Lines of Rust |
|---|---|
| sghtmltopdf | 92,513 |
| base engine | 221,976 |
| Fulgur | 139,941 |
| rustyprint | 78,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.