rustedf.cloud
Menu

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.

Terminal window
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.pdf

The 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.

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 as data: 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 :hover are 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.

← All posts