Template Generation

Generate PDF documents from Liquid HTML templates — perfect for invoices, certificates, reports, contracts, and dynamic documents. Templates can be supplied inline or fetched from a public URL, with full support for Liquid tags, filters, and variables.

Method Endpoint Purpose
POST /v1/pdf/from-template Generate a PDF from a Liquid template

Endpoint: Generate PDF from Template

Render a Liquid (Shopify-compatible) template with variables to produce HTML, then convert the HTML to a PDF document. Supports all standard Liquid tags ({% for %}, {% if %}, {% assign %}), filters (| upcase, | date, etc.), and objects ({{ variable }}).

# Inline template
curl -X POST "https://pdf.toolkitapi.io/v1/pdf/from-template" \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "template": "<h1>{{ title }}</h1><p>{{ body }}</p>",
    "variables": {"title": "Invoice #1234", "body": "Thank you for your business."},
    "page_size": "A4"
  }'

# Template from URL
curl -X POST "https://pdf.toolkitapi.io/v1/pdf/from-template" \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "template_url": "https://example.com/templates/invoice.html",
    "variables": {
      "company": "Acme Corp",
      "items": [
        {"desc": "Widget", "qty": 2, "price": 9.99},
        {"desc": "Gadget", "qty": 1, "price": 29.99}
      ],
      "total": 49.97
    },
    "page_size": "Letter",
    "strict": true
  }'
import requests, base64

resp = requests.post(
    "https://pdf.toolkitapi.io/v1/pdf/from-template",
    headers={"X-API-Key": "YOUR_KEY"},
    json={
        "template": "<h1>{{ title }}</h1><p>Dear {{ name }},</p><p>{{ message }}</p>",
        "variables": {
            "title": "Certificate of Completion",
            "name": "Alice Smith",
            "message": "You have completed the course.",
        },
        "page_size": "A4",
    },
)
data = resp.json()
print(f"Generated PDF: {data['page_count']} page(s), {data['file_size']} bytes")

# Save to file
with open("output.pdf", "wb") as f:
    f.write(base64.b64decode(data["pdf"]))
const resp = await fetch("https://pdf.toolkitapi.io/v1/pdf/from-template", {
  method: "POST",
  headers: { "X-API-Key": "YOUR_KEY", "Content-Type": "application/json" },
  body: JSON.stringify({
    template_url: "https://example.com/templates/report.html",
    variables: {
      report_name: "Q4 Financials",
      generated_at: new Date().toISOString(),
      sections: [
        { heading: "Revenue", content: "$1.2M" },
        { heading: "Expenses", content: "$0.8M" },
      ],
    },
    page_size: "A4",
  }),
});
const data = await resp.json();
console.log(`${data.page_count} page(s), ${data.file_size} bytes`);
// data.pdf is base64-encoded
Response
{
  "pdf": "JVBERi0xLjcN... (base64)",
  "upload_url": null,
  "page_count": 1,
  "file_size": 28473,
  "rendered_html_length": 412
}

Request Fields

Field Required Default Description
template Conditional* — Liquid template string (max 1 MB)
template_url Conditional* — Public HTTPS URL to a Liquid template (max 2048 chars)
variables No {} Template variables / context
strict No false Raise errors on undefined variables
page_size No "A4" Page size: A4, A3, A5, Letter, Legal

*Provide exactly one of template or template_url.

Page Size Dimensions

Size Width (pt) Height (pt)
A3 841.89 1190.55
A4 595.28 841.89
A5 419.53 595.28
Letter 612.0 792.0
Legal 612.0 1008.0

Template Tips

Liquid syntax — templates use the standard Liquid templating language:

<!-- Variables -->
<h1>{{ title }}</h1>

<!-- Conditionals -->
{% if discount > 0 %}
  <p>Discount: {{ discount | percent }}</p>
{% endif %}

<!-- Loops -->
<table>
{% for item in items %}
  <tr>
    <td>{{ item.desc }}</td>
    <td>{{ item.price | round: 2 }}</td>
  </tr>
{% endfor %}
</table>

<!-- Filters -->
<p>{{ "hello" | upcase }}</p>
<p>{{ published_date | date: "%B %d, %Y" }}</p>

CSS for print — use standard CSS for PDF styling. The HTML is converted via xhtml2pdf:

@media print {
  @page {
    size: A4;
    margin: 20mm 15mm;
  }
  body { font-family: Helvetica, Arial, sans-serif; }
  table { width: 100%; border-collapse: collapse; }
  th { background: #eee; }
}

Error Codes

Status Meaning
400 Invalid parameters — missing template source, empty rendered HTML, invalid page size, HTML conversion errors
500 Internal error — HTML-to-PDF engine unavailable

Tips

  • Use template_url for reusable templates stored on a CDN or static hosting — keeps your templates version-controlled.
  • Set strict: true during development to catch undefined variables early.
  • For multi-page documents, ensure your CSS respects page breaks (page-break-before, page-break-after).
  • Test with A4 first, then adjust for other sizes as needed.
  • The response includes rendered_html_length for debugging template output size.