🏗️

PDF from Template

Generate a PDF document from a Liquid template with variable substitution

POST 1 credit /v1/pdf/from-template
curl -X POST "https://pdf.toolkitapi.io/v1/pdf/from-template" \
  -H "Content-Type: application/json" \
  -d '{
    "template": "<h1>Invoice #{{ invoice_number }}</h1><p>Date: {{ date }}</p><table><tr><th>Item</th><th>Amount</th></tr>{% for item in items %}<tr><td>{{ item.name }}</td><td>{{ item.price }}</td></tr>{% endfor %}</table><p><strong>Total: {{ total }}</strong></p>",
    "variables": {
      "invoice_number": "INV-2024-0042",
      "date": "December 15, 2024",
      "items": [
        {"name": "Consulting", "price": "$2,500"},
        {"name": "Development", "price": "$5,000"}
      ],
      "total": "$7,500"
    },
    "page_size": "A4"
  }'
import httpx

resp = httpx.post(
    "https://pdf.toolkitapi.io/v1/pdf/from-template",
    json={
    "template": "<h1>Invoice #{{ invoice_number }}</h1><p>Date: {{ date }}</p><table><tr><th>Item</th><th>Amount</th></tr>{% for item in items %}<tr><td>{{ item.name }}</td><td>{{ item.price }}</td></tr>{% endfor %}</table><p><strong>Total: {{ total }}</strong></p>",
    "variables": {
      "invoice_number": "INV-2024-0042",
      "date": "December 15, 2024",
      "items": [
        {"name": "Consulting", "price": "$2,500"},
        {"name": "Development", "price": "$5,000"}
      ],
      "total": "$7,500"
    },
    "page_size": "A4"
  },
)
print(resp.json())
const resp = await fetch("https://pdf.toolkitapi.io/v1/pdf/from-template", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "template": "<h1>Invoice #{{ invoice_number }}</h1><p>Date: {{ date }}</p><table><tr><th>Item</th><th>Amount</th></tr>{% for item in items %}<tr><td>{{ item.name }}</td><td>{{ item.price }}</td></tr>{% endfor %}</table><p><strong>Total: {{ total }}</strong></p>",
    "variables": {
      "invoice_number": "INV-2024-0042",
      "date": "December 15, 2024",
      "items": [
        {"name": "Consulting", "price": "$2,500"},
        {"name": "Development", "price": "$5,000"}
      ],
      "total": "$7,500"
    },
    "page_size": "A4"
  }),
});
const data = await resp.json();
console.log(data);
# See curl example
Response 200 OK
{
  "pdf": "JVBERi0xLjcKMSAwIG9iago8...",
  "page_count": 1,
  "file_size": 45320,
  "rendered_html_length": 412
}

Try It Live

Live Demo

Description

Generate a PDF document from a Liquid template with variable substitution

How to Use

1

1. Provide your template via `template` (inline HTML+Liquid string) or `template_url` (public HTTPS URL).

2

2. Pass `variables` as a JSON object — these become available in the template as Liquid variables.

3

3. Optionally set `page_size` (`A4`, `A3`, `A5`, `Letter`, `Legal`) and `strict` mode (errors on undefined variables).

4

4. The response contains the generated PDF as base64, with page count and file size.

About This Tool

PDF from Template generates PDF documents by rendering a Liquid template (Shopify-compatible syntax) with your data, converting the resulting HTML to a polished PDF. This lets you create invoices, reports, certificates, letters, and any structured document from reusable templates.

Liquid templates support variables (`{{ name }}`), loops (`{% for item in items %}`), conditionals (`{% if paid %}`), and filters (`{{ name | upcase }}`). Write your template as HTML with Liquid tags, pass your data as variables, and get back a print-ready PDF.

Templates can be provided inline or fetched from a public HTTPS URL, making it easy to manage templates externally and update them without changing your code.

Why Use This Tool

Frequently Asked Questions

What Liquid features are supported?
All standard Liquid tags (`{% for %}`, `{% if %}`, `{% assign %}`, `{% capture %}`), filters (`| upcase`, `| date`, `| split`, etc.), and objects (`{{ variable }}`) are supported. This is Shopify-compatible Liquid syntax.
Can I include CSS styling?
Yes. Your template is HTML, so you can include `<style>` blocks or inline CSS. The HTML-to-PDF engine renders CSS for layout, fonts, colors, and basic formatting.
What happens with undefined variables?
By default, undefined variables render as empty strings. Set `strict` to `true` if you want the endpoint to return an error when a variable referenced in the template is not provided.

Start using PDF from Template now

Get your free API key and make your first request in under a minute.