HTML Template to PDF

The API enables you to add data to your HTML template and convert it to PDF. This feature streamlines the generation of invoices, reports, personalized marketing materials, and so on.

The data can be provided in JSON, XML, CSV or YAML and can be uploaded from a file or a string.

The supported template syntax is based on the Jinja rendering engine. The most common constructs are:

  • Data rendering: Invoice: {{ invoice.number }}
  • For loop: {% for item in invoice.items %} ... {% endfor %}
  • If statement: {% if invoice.total > 100 %} ... {% endif %}
  • Filter: {{ invoice.to.first_name|capitalize }}
  • Number formatting:
    • Thousands separator: {{ "{:,}".format(item.quantity) }}
    • Two decimal places: {{ "{:.2f}".format(item.price) }}

The following template filters are supported:
capitalize, center, default, escape, first, forceescape, format, indent, join, last, length, list, lower, replace, reverse, safe, slice, sort, string, striptags, title, trim, truncate, unique, upper, wordcount, wordwrap

Example

Below is a simple invoice template example along with sample JSON data. The code shows how to pass the template and the data to the API and convert it to a PDF file.

Template

<html>
  <body>
    <h1>Invoice #{{ invoice.number }}</h1>
    <p>Issued on: {{ invoice.date }}</p>

    <h2>To</h2>
    {{ invoice.to.company }} <br>
    {{ invoice.to.street }} <br>
    {{ invoice.to.state }} {{ invoice.to.zip }} <br>

    <table>
      <thead>
        <th>Item</th><th>Quantity</th><th>Price</th>
      </thead>
      {% for item in invoice.itemList %}
      <tr>
        <td>{{ item.name }}</td>
        <td>{{ "{:,}".format(item.quantity) }}</td>
        <td>${{ "{:.2f}".format(item.price) }}</td>
      </tr>
      {% endfor %}
    </table>

    <p>
      <b>Total:</b> ${{ invoice.total }} <br>
      {% if invoice.paid > 0 %}
      <b>Paid:</b> ${{ invoice.paid }} <br>
      {% endif %}
      <b>Amount Due:</b> ${{ invoice.amountDue }}
    </p>

  </body>
</html>

JSON Data

{
  "invoice": {
    "number": 122200,
    "date": "04/20/2023",
    "to": {
      "company": "Weyland-Yutani Corporation",
      "street": "333 Market St",
      "city": "San Francisco",
      "state": "CA",
      "zip": "94105"
    },
    "itemList": [
      {
        "name": "Bolts",
        "quantity": 1000,
        "price": 500
      },
      {
        "name": "Nuts",
        "quantity": 1000,
        "price": 350
      }
    ],
    "total": 850,
    "paid": 200,
    "amountDue": 650
  }
}

API Code