This is part 7 of the pdfblaze series. Quotes were the easy part: they were already HTML, printed by Chrome, and pdfblaze prints HTML. Seven other documents in the CRM were drawn by Prawn, a Ruby PDF library, with code, inside the Rails process that also served web requests and ran jobs.
All seven now go through pdfblaze 1.4.3. Twelve of the 13 cases I measured got faster, from 1.44× to 20.54×. The biggest win was a 1,000-message chat transcript: 16,468.4 ms before, 801.7 ms after. Arabic prints right. One case got slower, and the memory budget I set turned out to be an estimate rather than a cap. This post is the measurements, the ceilings, and a rollout whose first production deploy failed.
What Prawn did, and what it cost
The seven were: invoices, contracts, signed contracts, receipts, payouts, chat transcripts, and the plain quote that a quote with no template fell back to. Each was a Ruby class that placed text, lines and tables on a page by hand. That is how Prawn works, and for a long time it was fine.
Three things were not:
- It ran inside the app. A PDF was drawn in the same Ruby process that answered web requests and ran jobs, so every big document grew that process.
- It was slow on long documents. The 1,000-message transcript took 16.5 s.
- It printed Arabic as "?" or with its letters reversed.
How much a big document grew the Rails process:
| Document | Rails process growth |
|---|---|
| Invoice, 500 lines | 49.6 MB |
| Invoice, 1,000 lines | 52.4 MB |
| Payout, 1,000 entries | 62.3 MB |
| Contract, 150 clauses | 76.5 MB |
| Quote, 1,000 lines | 133.2 MB |
| Chat transcript, 1,000 messages | 389.1 MB |
A long-running Ruby process seldom gives that memory back, and on a small server it is the same memory the web requests need.
The layouts also lived in Ruby code, a different system from the HTML quote templates. Two ways to draw a document is one too many.
How the new path works
Each document is now an HTML template that Rails renders, the same way it renders a page. pdfblaze prints it in a child process with two limits:
pdfblaze render invoice.html -o invoice.pdf --time-limit 20 --memory-budget 256
--time-limit 20 stops a document that takes longer than 20 seconds.
--memory-budget 256 stops one whose estimated memory passes 256 MiB. Either way
the document fails with a reason (limit.time or limit.memory) instead of
taking the app down with it. The CLI docs
describe both.
There is no fallback to Prawn. If pdfblaze cannot print a document, a web request is told to try again shortly and a background job retries. A fallback would have meant keeping two engines and two sets of layouts alive, and testing a path that almost never runs. Prawn and its table add-on are out of the Gemfile.
The benchmark
The setup: a laptop (M2 Pro), Ruby 3.4.13, 7 runs of each case, median. The laptop was busy, with a load average between 4.72 and 12.04 during the runs, so read differences of a few milliseconds as noise. "Old" is Prawn drawing the document inside a Rails process. "New" is pdfblaze 1.4.3 printing the same document in a process of its own, with the two limits above.
- old engine, in-process
- pdfblaze 1.4.3, own process
- slower than before
- Invoice · 6 lines1.64×67.8 ms41.3 ms
- Invoice · 100 lines4.19×228.3 ms54.5 ms
- Invoice · 500 lines8.13×1,102.8 ms135.6 ms
- Invoice · 1,000 lines8.46×1,921.4 ms227.1 ms
- Contract · 150 clauses7.78×1,060.4 ms136.3 ms
- Signed contract · 50 signers4.63×944.9 ms204.1 ms
- Receipt · 4 lines1.44×46.9 ms32.5 ms
- Receipt · 200 lines7.83×428.3 ms54.7 ms
- Payout · 1,000 entries5.23×2,500.5 ms478.3 ms
- Chat transcript · 1,000 messages20.54×16,468.4 ms801.7 ms
- Chat transcript · 3,000 messages10.53×19,178.1 ms1,821.1 ms
- Quote · 3 lines0.31×, slower18.4 ms58.6 ms
- Quote · 1,000 lines9.18×2,624.7 ms285.9 ms
The 3-line quote went from 18.4 ms to 58.6 ms: drawing it inside the app had no process to start, and pdfblaze does. Control: a templated quote, pdfblaze in both builds, measured 35.8 / 42.7 ms at 3 lines and 78.3 / 83.1 ms at 200, so differences of a few milliseconds here are noise.
| Document | Size | Old | New | Speed-up |
|---|---|---|---|---|
| Invoice | 6 lines | 67.8 ms | 41.3 ms | 1.64× |
| Invoice | 100 lines | 228.3 ms | 54.5 ms | 4.19× |
| Invoice | 500 lines | 1,102.8 ms | 135.6 ms | 8.13× |
| Invoice | 1,000 lines | 1,921.4 ms | 227.1 ms | 8.46× |
| Contract | 150 clauses | 1,060.4 ms | 136.3 ms | 7.78× |
| Signed contract | 50 signers | 944.9 ms | 204.1 ms | 4.63× |
| Receipt | 4 lines | 46.9 ms | 32.5 ms | 1.44× |
| Receipt | 200 lines | 428.3 ms | 54.7 ms | 7.83× |
| Payout | 1,000 entries | 2,500.5 ms | 478.3 ms | 5.23× |
| Chat transcript | 1,000 messages | 16,468.4 ms | 801.7 ms | 20.54× |
| Chat transcript | 3,000 messages | 19,178.1 ms | 1,821.1 ms | 10.53× |
| Quote | 3 lines | 18.4 ms | 58.6 ms | 0.31×, slower |
| Quote | 1,000 lines | 2,624.7 ms | 285.9 ms | 9.18× |
There is a control row. A quote with a template was already on pdfblaze in both builds, so it should not change: 35.8 ms against 42.7 ms at 3 lines, 78.3 ms against 83.1 ms at 200. That is the size of the noise on this machine.
pdfblaze's own peak memory, where it was recorded, stayed low: 26.7 MB for a 6-line invoice, 58.5 MB for 1,000 lines, 74.6 MB for the 1,000-message transcript and 142.2 MB for 3,000 messages. That memory belongs to a child process that exits when the document is done, not to the app.
The one that got slower
The 3-line quote went from 18.4 ms to 58.6 ms, about three times slower.
The reason is the shape of the work, not the engine's layout speed. Prawn drew the quote inside a Ruby process that was already running, so a tiny document cost only the drawing. pdfblaze starts a process for every document. For a long document that start is lost in the work; for a 3-line quote it is most of the work. It is the only one of the 13 cases that Prawn drew in under 40 ms, and the only one that got slower.
Two ways to win it back exist, and I took neither yet:
- A long-running worker. The engine already speaks a framed protocol to a worker that stays up between documents; a warm worker prints a one-page invoice in about 1.5 ms. But a pool of workers is shared state between requests, and part 6 is about how the documented pool handed one caller's PDF to the next. I would rather add that with care than in a hurry.
- The C library, which skips the process start (the base engine measured that at about 5 ms).
40 ms more on a quote nobody prints in a loop is a cost I chose to keep for now.
How big can a document get?
Speed at normal sizes is half the story. The other half is what happens at absurd sizes: a payout with 15,000 entries, a transcript with 20,000 messages. I pushed each document up until it failed.
- pdfblaze 1.4.3
- old engine
- Invoicepdfblaze first fails at 17,50015,000 lines20,000 lines
- Payoutpdfblaze first fails at 20,00015,000 entries30,000 entries
- Transcriptpdfblaze first fails at 25,00020,000 messages10,000 messages
- Quotepdfblaze first fails at 20,00010,000 lines10,000 lines
- Contractpdfblaze: no failure recorded4,500 clauses150 clauses
- Receiptpdfblaze: no failure recorded10,000 linessize not recorded
The old engine printed a bigger invoice and payout than pdfblaze lets through. pdfblaze printed a transcript twice as long and a contract with 4,500 clauses where the old engine's benchmark went to 150.
| Document | pdfblaze, largest that passed | pdfblaze, first failure | Old engine |
|---|---|---|---|
| Invoice | 15,000 lines, 3.67 s, 415 MB | 17,500 | 20,000 lines in 48.3 s |
| Payout | 15,000 entries, 5.06 s, 421 MB | 20,000 | 30,000 entries in 90 s |
| Transcript | 20,000 messages, 11.4 s, 583 MB | 25,000 | 10,000 messages in 47 s |
| Quote | 10,000 lines, 3.08 s | 20,000 | 10,000 lines in 33.7 s |
| Contract | 4,500 clauses, 4.5 s, 274 MB | — | 150 clauses in 1.49 s |
| Receipt | 10,000 lines, 1.63 s | — | 21.5 s |
pdfblaze fails first because it reaches its 256 MiB budget. That is the point of
the budget: a 17,500-line invoice is refused with limit.memory, and the caller
can say so. The old engine has no such stop. It printed a 30,000-entry payout, but
it spent 90 s of a Rails process doing it, with nothing to stop it at 20 s or at
any amount of memory.
So for invoices and payouts the old engine's ceiling is higher, and pdfblaze's is lower on purpose. If a real customer needs a 20,000-line invoice, the budget can go up for that document. I would rather raise a limit for one case than have no limit for any.
The budget is not a cap
Look at the peaks in that table again: 415 MB, 421 MB, 583 MB, all under
--memory-budget 256. Failures went up to 694 MB.
The budget is an estimate. pdfblaze counts what the large parts of a document hold
(the document tree, computed styles, layout fragments, decoded images, fonts) and stops the document when that
estimate passes the budget. It does not count everything the process holds, and it
does not cap the process. Measured today on 1.4.3, a document built to blow up
stops at 0.42 s under a 64 MiB budget, with a real peak of 358 MiB. The stop works.
The number on the flag is not the number in top.
The hard cap is a different flag:
pdfblaze render invoice.html -o invoice.pdf --time-limit 20 --memory-budget 256 \
--memory-limit 1024
--memory-limit (Linux only) ends the process if its address space would pass the
limit. The CRM does not set it yet. Adding --memory-limit 1024 is the planned
follow-up, and until then the budget plus the 20 s limit is what keeps a big
document from hurting anything else.
Staging, then production
The change merged at 08:47 UTC on 5 December. The staging deploy removed the Chrome container. At 09:07 UTC I printed the newest real record of each of the 8 document kinds on staging, the plain and the templated quote among them. Every one was a valid PDF, in 78–412 ms each, warm.
The first production deploy failed. The new container booted cleanly, but the deploy proxy checked its health only every 10 seconds, and it did not see the container healthy before the deploy gave up on it. The old version kept serving. Nothing about the failure was in pdfblaze or in the PDF code; it was a setting no one had needed to look at, because every earlier boot had happened to finish just before one of the checks.
The second attempt went live at 11:42 UTC. The Chrome container came out of production with it. A shorter interval is merged and runs on staging; production gets it with the next release.
Fork commit to production was under 20 hours. Most of it is in the timeline in part 1.
What's still open
- No production numbers yet. Staging printed every kind in 78–412 ms. I have not measured latency or memory in production since it went live.
- The hard memory cap is not set. The budget stops documents; real peaks reach
415–583 MB at the largest sizes.
--memory-limit 1024is the follow-up. - Copying Hindi or Arabic text out of a PDF gives the wrong characters. The page looks right; the text layer behind shaped letters is wrong.
- No veraPDF run. pdfblaze can write PDF/A, but nothing in this benchmark was validated with veraPDF.
- The 3-line quote is about three times slower, as above.
What I'd do differently
- Benchmark on a quiet machine. A load average of up to 12.04 is the likely reason the control row moves by 7 ms. The big speed-ups are far outside that; the small ones are not.
- Set
--memory-limitwith the budget, on the first day. I knew the budget was an estimate. I should have shipped the cap next to it rather than as a follow-up. - Read the deploy settings before the deploy that matters. A health check every 10 seconds gains nothing over a shorter one and costs a failed deploy when a boot runs long.
- Write the ceilings into the product. The CRM should tell a user who builds a 17,500-line invoice why it will not print, before they try.
The next part, part 8, is how the binary the CRM runs is built, signed and checked.