Skip to content
Back to blog
tutorial api developer guide

How to Generate PDFs from JSON Data with an API

pdfs.build Team

If you need to generate PDFs programmatically — invoices, reports, certificates, or any document that changes per request — you have a few options. This post covers the most common approaches and shows you how to build a clean, maintainable PDF generation pipeline using templates and JSON data.

The problem: dynamic PDFs at scale

Most apps need to generate documents that are mostly the same structure, but with different data each time. An invoice template stays constant, but customer names, line items, and totals change per render.

The approaches break down into three categories:

1. HTML-to-PDF (headless browser)

Tools like Puppeteer or Playwright render an HTML page and “print” it to PDF. This works for simple cases but has drawbacks:

2. Low-level PDF libraries

Libraries like pdfkit (Node), fpdf (Python), or iText (Java) give you full control but require pixel-level positioning:

// pdfkit example — verbose for anything complex
doc.fontSize(12).text('Invoice #2025-001', 50, 50);
doc.text('Acme Corp', 50, 70);
doc.moveTo(50, 90).lineTo(550, 90).stroke();
// ...hundreds of lines for a real invoice

3. Template-based rendering

Define a template with variables, pass data as JSON, get a PDF back. This is the most maintainable approach:

Template + Data → PDF

The template describes the layout and where dynamic values go. The rendering engine fills in the data and produces a consistent PDF every time.

Template-based PDF generation with pdfs.build

pdfs.build is a template engine purpose-built for document rendering. Templates are easy to read and maintain, with powerful layout capabilities for tables, typography, and multi-page documents.

Step 1: Create a template

The fastest way to create a template is through AI chat — describe what you want in plain English and the AI agent builds it for you. You can iterate on the design through conversation until it’s perfect.

Templates define dynamic variables (company name, line items, dates) that get filled in at render time. The template handles layout, typography, and formatting — your API call just provides the data.

Step 2: Define your data schema

Each template has a JSON schema that defines what data it expects. For our invoice:

{
  "company": "string",
  "invoice_nr": "string",
  "due_date": "string",
  "items": [
    {
      "name": "string",
      "qty": "number",
      "price": "number"
    }
  ]
}

Step 3: Render via API

Pass your template ID and data payload to the render endpoint:

const response = await fetch('https://api.pdfs.build/v1/render', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    templateId: 'tmpl_invoice_v3',
    data: {
      company: 'Acme Corp',
      invoice_nr: 'INV-2025-042',
      due_date: '2025-03-15',
      items: [
        { name: 'Consulting', qty: 8, price: 150 },
        { name: 'Development', qty: 24, price: 125 },
      ],
    },
  }),
});

const pdf = await response.arrayBuffer();
// Save to file, send as email attachment, return to user, etc.

The API returns a binary PDF in under 400ms on average.

Step 4: Integrate into your workflow

Common patterns:

Comparison: approaches at a glance

ApproachRender timeConsistencyMaintainabilitySetup complexity
HTML + Puppeteer500ms–2sLowMediumHigh (browser deps)
PDF libraries50–200msHighLow (verbose)Medium
Template engine<400msHighHighLow (API call)

Getting started

  1. Sign up for free — no credit card required
  2. Create a template (or pick from 50+ starters)
  3. Generate an API key
  4. Call the render endpoint with your data

The free tier includes 5 templates and 10 renders per month. The Pro plan ($29/month) gives you unlimited templates and 5,000 renders.


Have questions? Reach out at [email protected] or check out the API reference.

Back to blog