Examples
Five complete documents. Each one shows the HTML first, then how to render it with the API, the WebAssembly build and the native binary. The engine is the same in all three, so the output is too.
Invoice
A one-page A4 PDF. @page sets the paper and its margins; the pagination option makes the
engine honor it. The width and height, 794 × 1123, are A4 in CSS pixels: the height is required as soon as you
paginate.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
@page { size: A4; margin: 18mm; }
body { font: 10.5pt/1.5 Body, sans-serif; color: #1c2434; margin: 0; }
header { display: flex; justify-content: space-between; align-items: baseline;
border-bottom: 2px solid #2447b3; padding-bottom: 8pt; }
h1 { font-size: 20pt; margin: 0; color: #2447b3; }
table { width: 100%; border-collapse: collapse; margin-top: 24pt; }
th, td { padding: 6pt 0; border-bottom: 1px solid #d5dbe8; text-align: left; }
th:last-child, td:last-child { text-align: right; }
tfoot td { border: 0; font-weight: bold; }
</style>
</head>
<body>
<header><h1>Invoice</h1><span>No. 2026-0042 · 15 November 2026</span></header>
<p>Billed to <strong>Northwind Books</strong>, 12 Quay Street, Galway</p>
<table>
<thead><tr><th>Description</th><th>Qty</th><th>Amount</th></tr></thead>
<tbody>
<tr><td>Typesetting, 320 pages</td><td>1</td><td>€1,600.00</td></tr>
<tr><td>Cover design</td><td>1</td><td>€450.00</td></tr>
</tbody>
<tfoot><tr><td colspan="2">Total</td><td>€2,050.00</td></tr></tfoot>
</table>
</body>
</html> import { readFile, writeFile } from "node:fs/promises";
const response = await fetch("https://api.palatine.page/v1/render", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.PALATINE_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
html: await readFile("invoice.html", "utf8"),
format: "pdf",
width: 794,
height: 1123,
options: { pagination: true, pdfTitle: "Invoice 2026-0042" },
}),
});
if (!response.ok) throw new Error((await response.json()).error);
await writeFile("invoice.pdf", new Uint8Array(await response.arrayBuffer())); The API does not know a font called Body, so the text falls back to the built-in Noto Sans. To use
your own font, publish the file and declare it in the document's CSS:
@font-face {
font-family: Body;
src: url("https://example.com/fonts/Body.woff2");
}import { createRenderer } from "@palatine/renderer";
import { readFile, writeFile } from "node:fs/promises";
const renderer = await createRenderer({
license: await readFile("palatine-license.txt", "utf8"),
});
const pdf = await renderer.render({
html: await readFile("invoice.html", "utf8"),
format: "pdf",
width: 794,
height: 1123,
fonts: [{ family: "Body", data: await readFile("Body.ttf") }],
options: { pagination: true, pdfTitle: "Invoice 2026-0042" },
});
await writeFile("invoice.pdf", pdf); Body.ttf can be a TrueType, WOFF or WOFF2 file. A variable font covers regular and bold with one
file; with static fonts, add one entry per weight and style under the same family.
./palatine --license palatine-license.txt \
--input invoice.html \
--font Body.ttf --font-family Body \
--paginate --pdf-title "Invoice 2026-0042" \
--format pdf --width 794 --height 1123 \
--output invoice.pdf --font is the document's main font, registered under the name given by --font-family. See images, stylesheets and fonts to add more.
Paginated report
A small book: each chapter starts a page, its title runs in the page header, pages are numbered, a footnote sits at the bottom of its page, and a table of contents lists the page of each chapter. Paragraphs are justified with Knuth–Plass line breaking and automatic hyphenation.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
@page {
size: A5;
margin: 20mm 16mm 22mm;
@top-center { content: string(chapter); font: italic 9pt Body; }
@bottom-center { content: counter(page) " / " counter(pages); font: 9pt Body; }
@footnote { border-top: 0.5pt solid #777; padding-top: 4pt; }
}
@page :first { @top-center { content: none; } }
body { font: 10.5pt/1.45 Body, serif; }
h1 { string-set: chapter content(); break-before: page; font-size: 18pt; }
h1:first-of-type { break-before: auto; }
p { text-align: justify; text-indent: 1.2em; margin: 0;
hyphens: auto; widows: 2; orphans: 2; }
h1 + p { text-indent: 0; }
.note { float: footnote; font-size: 8.5pt; }
nav a { display: block; color: inherit; text-decoration: none; }
nav a::after { content: " · " target-counter(attr(href url), page); }
</style>
</head>
<body>
<nav data-palatine-toc="h1"></nav>
<h1 id="tides">Tides</h1>
<p>The harbour empties twice a day<span class="note">Measured at the
north pier.</span> and the boats settle on the sand.</p>
<p>…</p>
<h1 id="weather">Weather</h1>
<p>Wind from the south-west brings rain by evening.</p>
<p>…</p>
</body>
</html> string-setandstring()carry the chapter title into the@top-centermargin box.float: footnotemoves the note to the foot of the page and leaves a numbered call in the text.data-palatine-tocfills the element with links to the selected headings;target-counter()prints the page each link points to.hyphens: autofollows the document language, set bylang.
import { readFile, writeFile } from "node:fs/promises";
const response = await fetch("https://api.palatine.page/v1/render", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.PALATINE_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
html: await readFile("report.html", "utf8"),
format: "pdf",
width: 559,
height: 794,
options: {
pagination: true,
pdfTitle: "Field Notes",
typography: { justification: { algorithm: "knuth-plass" } },
},
}),
});
if (!response.ok) throw new Error((await response.json()).error);
console.log(response.headers.get("x-palatine-pages"), "pages");
await writeFile("report.pdf", new Uint8Array(await response.arrayBuffer())); A PDF of any length is billed as one document. The page count comes back in x-palatine-pages.
import { createRenderer } from "@palatine/renderer";
import { readFile, writeFile } from "node:fs/promises";
const renderer = await createRenderer({
license: await readFile("palatine-license.txt", "utf8"),
});
const pdf = await renderer.render({
html: await readFile("report.html", "utf8"),
format: "pdf",
width: 559,
height: 794,
fonts: [{ family: "Body", data: await readFile("Body.ttf") }],
options: {
pagination: true,
pdfTitle: "Field Notes",
typography: { justification: { algorithm: "knuth-plass" } },
},
onWarning: (warning) => console.warn(warning.code, warning.message),
});
await writeFile("report.pdf", pdf);./palatine --license palatine-license.txt \
--input report.html \
--font Body.ttf --font-family Body \
--paginate --knuth-plass --pdf-title "Field Notes" \
--format pdf --width 559 --height 794 \
--warnings-json warnings.json \
--output report.pdfThe size, 559 × 794, is A5 in CSS pixels. The articles on paged media and footnotes and references go further.
Images, stylesheets and fonts
A document that links to other files: a stylesheet, a logo, and a second font declared with @font-face. The engine never opens a file or a URL by itself. Each target has its own way to hand
these resources over.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="theme.css">
</head>
<body>
<img src="assets/logo.png" alt="Northwind Books">
<h1>Welcome aboard</h1>
<p>Your account is ready.</p>
</body>
</html> @font-face { font-family: Display; src: url("fonts/Display.ttf"); }
body { font: 11pt/1.5 Body, sans-serif; margin: 48px; }
h1 { font-family: Display, sans-serif; font-size: 24pt; }
img { width: 96px; } import { readFile, writeFile } from "node:fs/promises";
const response = await fetch("https://api.palatine.page/v1/render", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.PALATINE_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
html: await readFile("letter.html", "utf8"),
format: "pdf",
width: 794,
// theme.css, assets/logo.png and fonts/Display.ttf are fetched under this URL.
baseURL: "https://example.com/letters/",
}),
});
if (!response.ok) throw new Error((await response.json()).error);
await writeFile("letter.pdf", new Uint8Array(await response.arrayBuffer())); The API fetches resources over http(s), so they must be reachable from the internet. Without baseURL, a relative URL cannot be fetched and the render fails with 422. Private
network addresses are refused.
import { createRenderer } from "@palatine/renderer";
import { readFile, writeFile } from "node:fs/promises";
import { pathToFileURL } from "node:url";
const renderer = await createRenderer({
license: await readFile("palatine-license.txt", "utf8"),
});
const pdf = await renderer.render({
html: await readFile("letter.html", "utf8"),
format: "pdf",
width: 794,
fonts: [{ family: "Body", data: await readFile("fonts/Body.ttf") }],
// Relative URLs are resolved against baseURL, then handed to the loader.
baseURL: pathToFileURL(process.cwd() + "/").href,
resources: { load: (url) => readFile(new URL(url)) },
});
await writeFile("letter.pdf", pdf); The loader receives each URL the document asks for and returns its bytes. It can read files, query a database
or call fetch. When the resources are few and known, pass them as a map instead, keyed by the URL
as written in the document:
resources: {
"theme.css": await readFile("theme.css"),
"assets/logo.png": await readFile("assets/logo.png"),
"fonts/Display.ttf": await readFile("fonts/Display.ttf"),
},./palatine --license palatine-license.txt \
--input letter.html \
--font fonts/Body.ttf --font-family Body \
--image assets/logo.png=assets/logo.png \
--stylesheet theme.css=theme.css \
--font-resource fonts/Display.ttf=fonts/Display.ttf \
--format pdf --width 794 --height 1123 \
--output letter.pdf Each flag maps a URL, exactly as written in the document, to a local file. The flags are repeatable. A resource that is not mapped is skipped without an error: check the output when you add one.
Download in the browser
With the WebAssembly build, the PDF is made on your user's device: nothing is uploaded and no server renders. This example runs the engine in a Web Worker so the page stays responsive, shows the progress, then saves the file.
<article id="report">
<h1>Quarterly report</h1>
<p>Revenue grew by 12%.</p>
<img src="/logo.png" width="48" alt="Logo">
</article>
<button id="download">Download PDF</button>
<span id="status"></span> import { createWorkerRenderer } from "@palatine/renderer/browser";
const worker = new Worker(
new URL("@palatine/renderer/worker", import.meta.url),
{ type: "module" },
);
// A domain-bound license (PAL2-…): safe to ship, it only works on your domains.
const palatine = createWorkerRenderer(worker, { license: "PAL2-…" });
const font = new Uint8Array(await (await fetch("/fonts/Body.ttf")).arrayBuffer());
const status = document.querySelector("#status");
document.querySelector("#download").addEventListener("click", async () => {
await palatine.download(
{
html: document.querySelector("#report").outerHTML,
css: "@page { size: A4; margin: 20mm } body { font: 11pt/1.5 Body }",
format: "pdf",
width: 794,
height: 1123,
fonts: [{ family: "Body", data: font }],
resources: "fetch", // images and stylesheets are fetched from the worker
baseURL: location.href,
options: { pagination: true },
},
"report.pdf",
{
onProgress: (progress) => {
if (progress.phase === "pages")
status.textContent = progress.painted + " / " + progress.total + " pages";
},
onWarning: (warning) => console.warn(warning.code, warning.message),
},
);
status.textContent = "Done";
}); Vite bundles the worker from this new URL(…) form. A license shipped to a public page
can be read by anyone, which is why web apps use a license bound to their domains: see domain authorization. The browser notes cover rendering without a worker and returning bytes or a Blob instead of a download.
Social card image
The same engine draws images. Set the format to
pngorwebpand give the exact size in pixels. Flexbox, grid and gradients work as in a browser.<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> body { margin: 0; width: 1200px; height: 630px; box-sizing: border-box; display: flex; flex-direction: column; justify-content: center; padding: 80px; color: white; font-family: Body, sans-serif; background: linear-gradient(135deg, #14213d, #2447b3); } h1 { font-size: 72px; line-height: 1.1; margin: 0 0 24px; } p { font-size: 30px; margin: 0; opacity: .8; } </style> </head> <body> <h1>Paged media, without a browser</h1> <p>palatine.page/articles</p> </body> </html>curl --fail-with-body https://api.palatine.page/v1/render \ --header "Authorization: Bearer $PALATINE_API_KEY" \ --header "Content-Type: application/json" \ --data "$(jq -n --rawfile html card.html \ '{ html: $html, format: "png", width: 1200, height: 630 }')" \ --output card.pngjqputs the HTML file into the JSON body with the right escaping. An image counts as one document, like a PDF.import { createRenderer } from "@palatine/renderer"; import { readFile, writeFile } from "node:fs/promises"; const renderer = await createRenderer({ license: await readFile("palatine-license.txt", "utf8"), }); const png = await renderer.renderPNG({ html: await readFile("card.html", "utf8"), width: 1200, height: 630, fonts: [{ family: "Body", data: await readFile("Body.ttf") }], }); await writeFile("card.png", png);renderPNG,renderWebP,renderSVGandrenderPDFare shorthands forrenderwith a format../palatine --license palatine-license.txt \ --input card.html \ --font Body.ttf --font-family Body \ --format png --width 1200 --height 630 \ --output card.png