Skip to content

Examples ​

Real document templates, rendered by vellora. Each picture is page 1 of a PDF made by the published vellora@0.3.0 package. The HTML and data for every one are in the repository's fixtures/ folder.

A full recipe: the receipt ​

Every example follows the same pattern: read the template and its data, pass any images as bytes, and call renderPdf. To run it, copy the repository's fixtures/ folder into a project that has vellora installed, and run the script from that project's root:

js
import { readFileSync, writeFileSync } from "node:fs";
import { renderPdf } from "vellora";

const dir = "fixtures/receipt";
const html = readFileSync(`${dir}/index.html`, "utf8");
const data = JSON.parse(readFileSync(`${dir}/data.json`, "utf8"));

const pdf = await renderPdf(html, data, {
  images: { "./logo.png": readFileSync(`${dir}/logo.png`) },
  metadata: { title: "Receipt" },
});

writeFileSync("receipt.pdf", pdf);

The template prints {{ merchant.name }}, loops over the items with {% for row in items %}, and its @page rule adds "Página 1 de 2" ("Page 1 of 2") at the bottom of each page. The images key is the src the template uses for the logo.

The other examples differ only in the folder and the images they pass.

Simple invoice ​

sparksuite/simple-html-invoice-template, an open-source HTML invoice template. Its sample text moved into data.json; the brand logo is replaced by a neutral placeholder.

js
const dir = "fixtures/oss-sparksuite-invoice";
const pdf = await renderPdf(html, data, {
  images: { "assets/logo.png": readFileSync(`${dir}/assets/logo.png`) },
});

Email receipt ​

The receipt from ActiveCampaign/postmark-templates, an email template printed to A4. Its Google Fonts @import is removed, because vellora loads nothing from the network; the text falls back to the bundled sans-serif font.

js
const dir = "fixtures/oss-postmark-receipt";
const pdf = await renderPdf(html, data);
Chrome: page 1 of the Postmark receipt template printed to PDFvellora: page 1 of the Postmark receipt template printed to PDF
Drag to compare. Left: Chrome’s print-to-PDF. Right: vellora 0.3.0. Same HTML file, with the template’s own [Product Name] placeholders.

Multi-page invoice ​

A Portuguese invoice with enough line items for three pages. The table's header row repeats at the top of each page, and the footer counts the pages. The Invoices guide walks through it.

js
const dir = "fixtures/invoice";
const pdf = await renderPdf(html, data, {
  images: { "assets/logo.png": readFileSync(`${dir}/assets/logo.png`) },
  metadata: { title: "Invoice" },
});

From a clone of the repository, after npm install && npm run build, npm run example renders it.

Boleto ​

A boleto is a Brazilian bank payment slip: a fixed grid of boxes, the typeable payment line, and the barcode digits printed as text.

js
const dir = "fixtures/boleto";
const pdf = await renderPdf(html, data, {
  images: { "./assets/banco-fake-logo.png": readFileSync(`${dir}/assets/banco-fake-logo.png`) },
  metadata: { title: "Boleto" },
});

Letter ​

A formal notification letter in Portuguese: long-form text, no images.

js
const dir = "fixtures/notification";
const pdf = await renderPdf(html, data, { metadata: { title: "Notification" } });

From a clone of the repository, after the same build, npm run render-notification renders it.

More from the repository ​

These scripts live in examples/:

fixtures/invoice-broken/ is a template with mistakes on purpose, to show the errors vellora gives.

Licences ​

The Simple invoice is adapted from sparksuite/simple-html-invoice-template, MIT licence, Copyright (c) 2021 Sparksuite. The Email receipt is adapted from ActiveCampaign/postmark-templates, MIT licence, Copyright (c) 2015 Wildbit. Each folder has the licence text and a NOTICE.md listing every change.

Next: Your first PDF in three steps →