HTML to PDF: library, headless browser or API? How to choose
Published 4 min read
There are three common ways to turn HTML into a PDF: a library inside your application, a browser that you run without a screen, and an API that does the work elsewhere. None of them is the right answer for every project. This guide compares them on what you have to run, what HTML and CSS you get, and where the document goes, and ends with the questions that decide.
The three ways
A library in your application. You add a package and call it. It lays out HTML and CSS with its own engine, so it can be small and quick to start. It usually supports a subset of CSS, and what is outside the subset is yours to work around. It runs in your process, with the memory and the failures that come with that, and you keep it updated.
A headless browser. You run a real browser without a window and ask it to print a page. You get the HTML and CSS support of the browser, including JavaScript. In exchange you install the browser in every environment, give it memory and time for each document, and keep it patched. At volume you manage a pool of browsers, queues and restarts.
An API. You send the HTML and the data in a request and get a PDF back. There is nothing to install or to keep running, and the cost grows with the number of documents. The document and its data leave your own infrastructure, you depend on the service being available, and its limits (the size of a request, the pages of a document, the rate of calls) apply to you.
What each asks of you
| Aspect | Library | Headless browser | API |
|---|---|---|---|
| What you run | A package in your app | A browser in every environment | Nothing, a client call |
| HTML and CSS you get | Whatever the engine supports | Whatever the browser supports | Whatever the service supports |
| JavaScript while rendering | Usually not | Yes | Depends on the service |
| Where the data goes | Stays with you | Stays with you | To the service |
| Who updates it | You | You | The service |
| Cost grows with | Your servers | Your servers | The number of documents |
Where rustedf.cloud sits
It is the third way, and it has clear limits. Documents are laid out for paper: pages, tables that
continue across pages, headers and footers, page numbers. It supports a defined subset of HTML and
CSS, and it does not run JavaScript. Images must be uploaded to your account or embedded as
data: URLs, because the service never fetches an address from a template. Anything outside the
subset is ignored and reported, and a validation endpoint tells you what before you send it.
What you get in return is no browser to run, templates with live preview in a portal, formatting of numbers, currencies and dates for any locale, and a failed render that costs nothing.
Questions that decide
- Do your documents need JavaScript to render? If a chart or a layout is drawn by a script, you need a browser or something that can run it. A template-based API will not do.
- Must the data stay inside your infrastructure? If a contract or a regulation says so, use a library or your own browser.
- How many documents, and how steady? A few thousand a month in bursts suits an API well. A very large, steady volume may be cheaper to run yourself.
- Who will keep it running? If nobody on the team wants to own a browser pool, that is a cost that does not appear in the first week.
- How much of the web page must be reproduced? An invoice, a quote, a report or a certificate fits the paper-oriented subset. A page copied from a complex web application may not.
If most answers point to “documents built from data, laid out for paper, nothing to maintain”, try the API with one real document and validate it. The free plan is for exactly that.
Read Supported HTML and CSS and Rendering a PDF.
Keep reading
Getting started
HTML and CSS for PDF: what works and how to check it
A PDF is paged, not scrolled. See which HTML and CSS a PDF renderer supports, what to leave out, and how to validate a document before it ships.
3 min read
Getting started
How to generate an invoice PDF with an API
Build an invoice template with a loop over items, currency and date formatting for each country, and render it to PDF with one request.
2 min read
Getting started
HTML to PDF API: generate a PDF with one request
Send HTML and data in one POST and get a PDF back. A curl example, a Node.js example, and what to know about images, limits and credits.
2 min read
Code and integrations
Generate a PDF in ASP.NET Core: MVC, Razor Pages and minimal APIs
Turn a Razor view into a PDF in ASP.NET Core with one API call: a view renderer, a print-ready view, an MVC action, a Razor Pages handler and the traps to avoid.
3 min read