HTML to PDF API: generate a PDF with one request
Published 2 min read
If your application already produces HTML, an invoice page, a report, a certificate, then
turning it into a PDF should be one call, not a project. This guide shows that call with
the rustedf.cloud API, in curl and in Node.js, and what to check before you rely on it.
The request
Send the HTML and the data in the same POST /v1/render. The HTML is treated as a
template: {{ name }} is replaced by the value of name in data.
curl -X POST https://api.rustedf.com/v1/render \ -H "Authorization: Bearer $RUSTEDF_KEY" \ -H "Content-Type: application/json" \ -d '{ "html": "<h1>Hello {{ name }}</h1>", "data": { "name": "World" } }' \ -o hello.pdfThe PDF is the body of the response. -o hello.pdf saves it. The response headers tell
you how many pages it has (X-Pages) and how many credits you have left
(X-Credits-Remaining).
Put values in data instead of building the HTML string yourself. Values are
HTML-escaped, so a customer called <b>Ana</b> shows up as text and cannot break the
layout.
The same call in Node.js
import { writeFile } from 'node:fs/promises';
const response = await fetch('https://api.rustedf.com/v1/render', { method: 'POST', headers: { Authorization: `Bearer ${process.env.RUSTEDF_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ html: '<h1>{{ title }}</h1><ul>{% for i in items %}<li>{{ i }}</li>{% endfor %}</ul>', data: { title: 'Shopping list', items: ['Bread', 'Milk'] }, }),});
if (!response.ok) throw new Error((await response.json()).error.message);await writeFile('list.pdf', Buffer.from(await response.arrayBuffer()));Keep the API key on your server. A request from a browser would expose it.
A link instead of the file
If you would rather hand your own customer a download link, ask for "output": "url".
The response is JSON with a signed link that works without an API key until it expires.
How long files are kept depends on your plan.
What to know before you ship
- Images. A document can use PNG and JPEG images you uploaded (
asset:<id>) or images embedded asdata:URLs. An<img>that points to a web address is skipped and reported, because the service never fetches an address from a template. - CSS. Documents are laid out for paper: pages, tables that continue across pages,
headers and footers. JavaScript, animations and
:hoverare not part of that. Anything unsupported is ignored and reported, not turned into a failure. - Credits. A successful render costs one credit. A failed render costs nothing.
- Limits. Input size, page count and time are limited per plan, and a request that exceeds them is refused with a clear error.
Check a document before it counts
POST /v1/validate takes the same body, builds the document and tells you what the
service did not fully support, without returning the PDF and without using a credit. Add
"strict": true to a render to make those same findings an error, which is useful in
tests.
When a saved template is better
Inline HTML suits one-off documents and HTML that comes from another system. If the same
layout is generated often, save it as a template in the portal, with a live preview, and
call it by id. The request is the same, with template_id instead of html.
Next, read Rendering a PDF for every option, and Generating with inline HTML for this exact flow.
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
HTML to PDF: library, headless browser or API? How to choose
Three ways to turn HTML into a PDF, what each asks you to run and maintain, what HTML and CSS you get, and a short list of questions that decide.
4 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
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