Skip to content

shipped · · 12 min

Printing a CRM's PDFs without Chrome: fork, harden, ship in under a day

A CRM printed its quotes with a headless Chrome that froze a small staging server, and seven other documents with a Ruby library inside the web process. I forked an open-source Rust engine as pdfblaze, cut its memory, had it audited, released it four times, and moved every PDF onto it. Fork commit to production: under 20 hours.

On 4 December the staging worker of a CRM froze. The CRM printed its quotes with a headless Chrome that ran next to the app on a small 2 GB cloud instance with no swap, and there was no memory left. Seven other documents were drawn by Prawn, a Ruby PDF library, inside the Rails process itself.

By 11:42 UTC the next day every PDF the CRM makes was printed by pdfblaze, a Rust program I had forked from an open-source engine the evening before. The Chrome container was gone, and so was Prawn. The CRM's 13 quote templates print in a median of 21 ms and about 26 MiB each, where a cold Chrome needed about 976 MiB for one document. A 1,000-message chat transcript went from 16.5 s to 0.8 s. Between the fork and production there were 16 commits, four releases and a security audit with 13 findings. Fork commit to production: under 20 hours.

This post is the overview: what printed the PDFs before, how I chose the engine, what I changed in the fork, and what the numbers are, including the ones that are not flattering. The series goes deep on each part:

  1. Printing a CRM's PDFs without Chrome (this post)
  2. Choosing a PDF engine by measurement
  3. Halving the heap of a 119-page invoice
  4. An audit and two releases in one day
  5. A ten-kilobyte logo that crashed the renderer
  6. The pool that handed one tenant's PDF to the next
  7. Replacing Prawn: seven documents, one engine
  8. Shipping a Rust binary you can verify

What was printing the PDFs

Two printers, with two different problems.

Quotes: a headless Chrome. A long-lived Chrome container with a 768 MiB limit ran beside the app and turned every quote's HTML into a PDF. Warm, it was quick enough: a median of 275 ms per template. But it only grew. Over 52 renders in a row in my comparison runs, the warm Chrome service went from 1,252 MiB to 9,404 MiB. Starting a fresh Chrome for each document stops the growth and costs more up front: a median of 976 MiB and 0.864 s for a one-page quote.

On staging that growth met a small machine. During the freeze the Chrome container went from 105 to 473 MiB while the jobs container sat at about 730 of its 800 MiB. A deploy starts the new container before it stops the old one, so for a moment both copies run. Getting the machine back took a stop and start of the instance.

Everything else: Prawn inside Rails. Invoices, contracts, signed contracts, receipts, payouts, chat transcripts and the plain quote were each drawn by Ruby code with Prawn, in the same process that served web requests and ran jobs. A big document grew that process by 49.6 to 389.1 MB. The 1,000-message transcript took 16,468.4 ms and grew it by 389.1 MB. Prawn also printed Arabic as "?" or with its letters reversed.

The constraint. A bigger server would have stopped the freeze. I chose not to buy one. A printer that needs a gigabyte for a one-page quote will need the next size up as well, and the one after that. The printer had to fit the machine.

Choosing an engine

Part 2 has the whole survey. The short version: WeasyPrint came first. I built a version of the quote with it, and its look was approved. Then I measured it: 1.09 s and 80 MiB for a one-page quote, median of four, a fresh process each, on a Mac. Fine for one quote, poor for a worker that prints them all day.

So I measured eight engines on the same quotes, the same way:

EngineTime per quotePeak memoryVerdict
WeasyPrint 701.09 s80 MiBtoo slow for the worker
The base engine, 1.3.00.022 s25 MiBpicked, and forked
ferri-pdf 0.3.00.069 s30 MiBrunner-up
Typst 0.150.028 s26 MiBevery template would need a rewrite
sghtmltopdf0.041 s16 MiBbroke the layout; no right to left
fullbleed0.118 s33 MiBArabic letters not joined
Fulgur0.18 s32 MiBright-to-left table not mirrored
PlutoPrint0.31 s36 MiBno grid

The engine I picked is an open-source Rust engine (MIT/Apache-2.0). It printed the quotes in 20–24 ms at 24–26 MiB, scored 94–100 on fidelity against Chrome, and was the fastest of the eight while getting Arabic and right-to-left tables right.

I forked it rather than depending on it. The CRM needed changes on its own schedule: the memory work, the audit fixes and a release pipeline it could verify all landed within a day, and the CRM pins the exact binary it runs. pdfblaze is that fork. The base engine's copyright stays in LICENSE-MIT.

The engine already did most of the hard parts before I touched it: page breaks inside layout rather than after it, PDF/A and PDF/UA output, PAdES signing, a framed worker protocol, a Linux sandbox, and a Web Platform Tests run in which 70.4% of the tests it runs pass. That is the base engine's work, not mine. Every version from 0.1.0 to 1.3.0 in the CHANGELOG is its history. My part starts at the fork commit and is the rest of this series.

From fork to production

figure 1 · fork to production, in order

1 · 12-04 · rollout

The staging worker freezes

Quotes were printed by a long-lived headless Chrome container on a small 2 GB instance with no swap. Chrome grew from 105 to 473 MiB, the jobs container sat near its cap, and getting the machine back took a stop and start. I chose not to buy a bigger server: the printer had to fit.

Fifteen steps from a frozen staging worker to production, in under 20 hours. Play it through, or filter by kind: the decisions, the memory work, the releases, the audit, the rollout. Times are UTC where they were recorded.

The fork commit is at 16:00 UTC on 4 December. Production went live at 11:42 UTC on 5 December. In between, roughly in order: memory work and a first release, a layout fix, a security audit of that release, two releases that fixed what the audit found, and the rollout.

What I changed in the fork

ChangeWhat it gavePart
Shrink and clear buffers, a smaller DOM node, shared styles for alike anonymous blocks, a web font held onceHeap on a 2,000-row, 119-page invoice 65.0 → 34.5 MB; peak RSS 114 → 86 MiB; each node 152 → 72 bytes3
Fat LTO for release builds4–5% faster, binary 28.0 instead of 30.5 MB8
Size flex image tiles from the right box (1.4.1)A 56 px logo tile with 6 px padding is 144 px wide, not 168below
Write to standard output on WindowsA pipe that failed with os error 87 works—
A depth limit for nested objects in PDFs used as images (1.4.2)A 10 KB file posing as a logo no longer crashes the process5
Stop a worker on any interrupt, check answer ids (1.4.2)The documented pool no longer hands one caller's PDF to the next6
CSS counters and multi-column layout obey the limits (1.4.2)A 47 KB input can no longer run for 29.3 s under a 1 s limit4
Release jobs split: build with read-only tokens, a separate job attestsSigned provenance and SBOMs per artifact; build jobs cannot write8
Drop the PHP and Laravel package147 files; 15,388 lines removed; one less thing to secure—

The memory work had one rule. Every change was checked against 668 test PDFs, which stayed byte-identical from the fork commit to the end. A counting allocator in the test suite fails if the peak heap of a 600-row table creeps back past its bound. Part 3 has the steps. The commit messages give the heap as 65.0 → 47.5 → 42.2 → 36.8 → 34.5 MB, and the CHANGELOG's end-to-end heap profile gives 63.5 → 33.1 MiB.

The logo fix came from the comparison below. Three templates differed from Chrome by about 3% of their ink, all for one reason: a logo with padding came out 24 px too wide. After 1.4.1 those three differ by 0.0%.

Against Chrome: 13 quote templates

The CRM ships 13 starter quote templates, from a one-line quote with no logo to a three-page Arabic and English one. I printed each with pdfblaze (three fresh processes, median) and with the warm Chrome service (median of three), on a busy laptop, and compared the pages at 80 dpi.

figure 2 · 13 quote templates, pdfblaze vs chrome
  • pdfblaze 1.4.0, fresh process
  • chrome 158, warm service
  • classic1 page
  • us-estimate1 page
  • au-gst1 page
  • de-angebot1 page
  • in-gst2 pages
  • ae-bilingual1 page
  • proforma-hindi1 page
  • boxed-quotation1 page
  • long-605 pages
  • one-line-no-logo1 page
  • long-names-fr2 pages
  • arabic-rtl3 pages
  • letterhead-letter2 pages

Median 21 ms for pdfblaze and 275 ms for a Chrome that was already running: about 13× faster, before counting Chrome's start-up.

Switch between time and memory. In time, each template has two bars: pdfblaze in a fresh process (median of 3) and the warm Chrome service (median of 3). In memory the axis is a log scale: pdfblaze's peak per template against one cold Chrome process (median of all 13) and the warm Chrome service, which grew with every render.
pdfblazeChrome
Time per template, median21 ms, a new process each275 ms warm; 0.864 s cold
Memory per template24–36 MiB, median 26976 MiB cold, median
PDF bytes, all 13523,7241,066,198
PDF bytes, median36,89656,662

The verdict across the 13 was 9 that match, 4 with small differences, 0 broken. The largest difference is not layout: long-names-fr differs by 67.2% of its ink because the test gave Chrome a Georgia font and gave pdfblaze none.

Two runs of the same comparison, on 1.4.0 and on 1.4.1, gave medians of 21 ms and 30 ms. Both ran on a laptop with other work going on, and nothing in the files explains the gap, so the honest range is 19–38 ms per template. Re-run today on 1.4.1 and 1.4.3, seven runs each, the 13 templates took 311.6 ms and 304.6 ms in total, with every page pixel-identical at 80 dpi between the two versions.

Here is what "small differences" looks like on two of the templates:

figure 3 · the same quote, chrome vs pdfblaze
50%
chromepdfblaze
Chrome's print. A one-page English quotation from a made-up company, Northwind Studio Ltd: a boxed header with logo, bill-to and prepared-by boxes, four line items, totals with 20% VAT, bank details and a signature.
pdfblaze's print of the same page. A one-page English quotation from a made-up company, Northwind Studio Ltd: a boxed header with logo, bill-to and prepared-by boxes, four line items, totals with 20% VAT, bank details and a signature.

boxed quotation: 0% of ink differs, rounded. Two pixels differ, at the corners of the logo tile. Everything else matches at this resolution.

Drag the split, or focus it and use the arrow keys: Chrome's print is on the left of the line, pdfblaze's on the right. Diff fades the ink both engines drew the same and marks in red the pixels that differ (page 1 at 80 dpi, 20% fuzz). The company, people and numbers are made up.

Against Prawn: seven documents

The seven Prawn documents moved in one change. On a laptop (M2 Pro), 7 runs each, median, the old engine inside Rails against pdfblaze 1.4.3 in its own process:

DocumentOldpdfblaze
Invoice, 1,000 lines1,921.4 ms227.1 ms8.46× faster
Payout, 1,000 entries2,500.5 ms478.3 ms5.23× faster
Chat transcript, 1,000 messages16,468.4 ms801.7 ms20.54× faster
Quote, 3 lines18.4 ms58.6 msslower

Twelve of the 13 measured cases got faster. The 3-line quote got slower, because a process start replaced a call inside the running app. Part 7 has all 13, how large each document can get before pdfblaze refuses it, and the production deploy that failed the first time.

What the audit changed, measured today

The audit of 1.4.1 found 2 High, 3 Medium, 4 Low and 4 Info findings. Two of the Mediums were the same kind of bug: a time limit or memory budget that a small input could walk straight past. Measured today on the released 1.4.3 (macOS arm64, median of 3):

CaseBefore, on 1.4.11.4.3
3,000 counters × 3,000 elements, --time-limit 129.3 s, 702 MiB; the limit ignoredstops at 1.35 s (limit.time), peak 607 MiB
the same, --memory-budget 64the budget ignoredstops at 0.42 s (limit.memory), peak 358 MiB
400,000 words in 1,000 columns, --time-limit 1still running at 120 sstops at 1.08 s, peak 314 MiB
terminal
pdfblaze render counters.html -o out.pdf --time-limit 1

A document that stops this way fails with a reason the caller can read, not a killed process. Part 4 is the audit. Its guard tests were checked by taking each fix out and watching the test fail.

What is still open

  • No production numbers yet. On staging the newest real record of each of the 8 document kinds printed in 78–412 ms, warm. I have no latency or memory figures from production since it went live.
  • The memory budget is not a hard cap. It estimates what a document holds and stops the document; it does not cap the process. Peaks still reach several times the budget before the stop: 358 MiB at a 64 MiB budget above, 415–583 MB at the largest CRM documents under 256 MiB. On Linux, --memory-limit is the hard cap, and the CRM does not set it yet.
  • Copying Hindi or Arabic text out of the PDF gives the wrong characters. The page looks right; the text layer behind shaped letters is wrong.
  • No veraPDF run. pdfblaze writes PDF/A, but I have not validated the CRM's output with veraPDF.
  • The 3-line quote is slower, 18.4 → 58.6 ms, as above.
  • Counters with no limits set are still expensive. At 3,000 × 3,000 they take 5.8 s and 1.7 GB on 1.4.3. The fix makes the limits work; it does not make the case cheap.

What I'd do differently

  • Measure on a quiet machine. Two runs of the same 13 templates gave medians of 21 and 30 ms, and I cannot say which is right. A laptop with a load average of 12 is not a benchmark machine.
  • Audit before the first release, not after it. Two releases of the fork were out before anyone had tried to break it on purpose, and the audit found two High findings.
  • Set the hard memory cap on day one. The budget did its job on every test I ran, and the real peak still went past it every time.
  • Read the deploy's health-check settings before the big deploy. The first production deploy failed on a check that ran every 10 seconds, and nothing in pdfblaze had to change to fix it. Part 7 has the details.

The code, the releases and the CHANGELOG are public: github.com/sundarshahi/pdfblaze, releases, CHANGELOG. If you read one more post, read part 3: it is the work that made the engine fit the machine.

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