Programmatic PDF generation with templates
The document that never changes is the one you should never rebuild. Define your document template once: layout rules, font pairings, and dynamic schemas. Then generate documents programmatically with a single HTTP call—receiving a verified, pixel-perfect PDF in milliseconds.
The anatomy of a programmatic template
A robust programmatic PDF workflow requires more than just raw HTML strings. A pdfs.build template unites three critical components into a guaranteed contract:
Typographic Layout
Native page composition compiled by Typst. Repeating table headers, page-number expressions (page 1 of 4), and blocks that never awkwardly split across pages.
JSON Schema Contract
Strict schema validation before compilation. Missing fields or malformed types trigger clear HTTP 422 JSON errors, ensuring broken PDFs never reach your customers.
Realistic Sample Data
Keeps development and preview environments honest. Non-technical teammates can design and test edge cases in the visual editor without touching production data.
Three architectural approaches compared
Most engineering teams start with imperative libraries or headless Chrome before switching to programmatic template engines. Here is how they compare in production:
| Architecture | Render Speed | Memory per Job | Pagination & Headers | Template Maintenance |
|---|---|---|---|---|
| Imperative Libraries PDFKit, jsPDF, ReportLab | Fast (<50ms) | Low (10–20MB) | Manual coordinate math; broken tables on page split | Hardcoded in backend; every label change requires deploy |
| Headless Chrome Puppeteer, Playwright, Gotenberg | Slower (~0.5s warm, ~3s cold) | Heavy (150–300MB per Chromium process) | CSS @page hacks; headers glitch across dynamic tables | HTML/CSS; unvalidated JSON strings passed into DOM |
| pdfs.build Programmatic Templates Typst engine + JSON Schema API | ~220ms median (full API call) | Low (under 50MB) | Deterministic typesetting; native repeating headers & footers | Decoupled template studio + typed schema contract |
Programmatic render recipes
One HTTP call sends your JSON data payload and streams the compiled binary PDF response immediately:
// TypeScript / Node.js
import fs from "node:fs/promises";
const response = await fetch("https://api.pdfs.build/v2/organizations/org_main/templates/invoice-standard/render", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.PDFS_BUILD_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
data: {
invoice_number: "INV-2026-0842",
date: "2026-09-21",
customer: {
name: "Acme Logistics Corp",
tax_id: "US-9482104",
},
items: [
{ description: "Priority Cargo Freight", quantity: 2, unit_price: 450.00 },
{ description: "Handling & Customs Clearance", quantity: 1, unit_price: 120.00 },
],
currency: "USD",
},
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Render failed: ${error.message}`);
}
// Raw PDF buffer ready to store in S3, email to customer, or stream to client
const pdfBuffer = Buffer.from(await response.arrayBuffer());
await fs.writeFile("invoice.pdf", pdfBuffer); Prefer Python, Go, or cURL? Check the Interactive API Documentation.
Three ways to create your template
- A
Pick from the 165+ Production Template Gallery
Every design in the template gallery is a compiled Typst document with schema and variables already wired. Pick an invoice, report, or certificate and adjust in minutes.
- B
Co-design with the AI Chat Agent
Describe your document requirements in natural language. The AI agent writes Typst layout code, generates the matching JSON schema, and checks compilation on every revision.
- C
Import Existing Word (.docx) Files
Upload a .docx file and convert it into a faithful Typst template, then define dynamic data slots. Learn how in our Word-to-API walkthrough.
Frequently asked questions
How does programmatic template generation differ from HTML-to-PDF?
HTML was designed for infinite vertical scrolling in browsers, not paginated printing. When tables break across pages in HTML-to-PDF, borders overlap, table headers vanish, and text orphans appear. Programmatic Typst templates are natively aware of margins, page sizes, running headers, and page counters, producing publication-grade documents deterministically.
Can I embed the template editor into my own web application?
Yes. With our embeddable React component (@pdfsbuild/react), you can embed the full studio canvas directly into your SaaS app. Your users can customize their own templates, colors, and variables while your backend calls the programmatic render API.
What happens if my backend sends invalid JSON data?
Every template is backed by a JSON Schema. If a required field is missing or a string is provided where a number is expected, the render request is rejected with an HTTP 422 error detailing the exact validation error. No half-rendered or corrupted PDFs are generated.
Start generating programmatic PDFs today
Free tier includes 2 templates and 50 renders every month. Integrate in under 5 minutes with any backend.