Browser printing vs server-side PDF generation
Compare browser printing and server-side PDF generation for HTML documents: requirements, control, consistency, fonts, security and cost, with DocStudio examples.
Once you can render a document as HTML, there are two ways to get a PDF: let the user's browser print it, or print it yourself in a headless browser on a server. DocStudio supports both with the same template. The choice is about requirements, not quality.
What each one is
Browser printing. printHtml(html) from docstudio/pdf renders the document in a hidden frame and opens the print dialog for that document only. Save as PDF is one of the choices in the dialog.
Server-side PDF. createPdfService from docstudio/server loads the document in Chromium through Playwright and returns the PDF bytes and the page count.
Comparison
| Concern | Browser printing | Server-side PDF |
|---|---|---|
| Infrastructure | None | Node, Playwright, a Chromium install |
| Who triggers it | A user, in a browser | Your code |
| You receive a file | No | Yes |
| Batch and background jobs | No | Yes |
| Consistency across users | Each browser paginates for itself | One engine and one set of fonts |
| Fonts | Must reach the page, or be registered with fonts |
Registered with the service |
| Controls | The user's dialog settings | Timeouts, concurrency, queue limits |
| Cost | The user's machine | Your compute |
| Attack surface | Your page | A browser that renders untrusted content |
Control
With browser printing the user can change margins, scale and background graphics in the dialog, and you cannot name the file. With a server you decide everything, and you can attach the result to an email or store it.
Consistency
Browsers paginate independently, so page breaks and totals can differ slightly between them. A server uses one Chromium and one set of fonts that you choose. If a document is legal or archival, or two people must get identical files, prefer the server.
Security of the server path
A server that renders documents is a browser that opens content built from templates and data. The DocStudio service restricts the document with a content security policy, allows only data: URLs and resolved assets through request interception, enforces a per-job timeout and concurrency and queue limits, writes nothing to disk and keeps document data out of logs. You still own authentication, authorization and rate limiting on the endpoint in front of it.
Fonts
Fonts are the usual source of surprises. A browser print uses the fonts it can find, so register the ones you rely on. The server needs the same fonts, supplied as a registry. Arabic and Urdu need a font that covers the script in either path.
A practical rule
Start with browser printing if a person is looking at the document and a print dialog is acceptable. It needs no infrastructure. Add the server when you need a file produced without a user, identical output for everyone, or a document attached to something. Because the template does not change between the two, you can add the server later.
Related
Related: the matching feature page and the quick start.