Generate Invoice PDFs in Zapier

Zapier is good at moving one record from A to B and weak at loops, which is exactly why a PDF is usually the awkward step. This template moves the repetition into the HTML template, so the Zap stays a straight line of three steps even when the invoice has twenty lines.

600 free requests / month, no credit card

Document
Invoice
Platform
Zapier
Steps
3
Output
PDF file

// live preview

The invoice this Zap produces

The HTML on the left is what the action receives. The JSON on the right is what the trigger step maps into it. Render it to see the file the Zap attaches.

// the workflow

Trigger, render, deliver

Trigger
new row, new payment, form submission
CustomJS
HTML to PDF
Gmail, Drive
attach or upload the file

Add the CustomJS app as an action step, pick the PDF action, and map the trigger fields into it. The tricky part on Zapier is the line items, and the next section is about exactly that.

Mapping trigger fields into the template data

{
  "invoiceNumber": "{{trigger__invoice_number}}",
  "customerName": "{{trigger__customer_name}}",
  "customerEmail": "{{trigger__customer_email}}",
  "total": "{{trigger__total}}",
  "currency": "USD",
  "items": {{trigger__line_items_json}}
}

// step by step

Build it yourself

  1. 1

    Connect CustomJS in Zapier

    Add a step, search for CustomJS, and create a connection with the API key from your workspace. The Zapier integration page shows the screens.

  2. 2

    Choose the PDF action

    Pick the HTML to PDF action. It takes the template and the data, and returns the finished file to the following steps.

  3. 3

    Get the line items in as JSON

    This is where most Zaps go wrong. If the trigger gives you Zapier line items, use a Formatter, Utilities, Line Itemizer step to turn them into a JSON array first. If the source already stores them as JSON text, map that field straight through.

  4. 4

    Attach the file

    The action returns a file object, so the Gmail, Outlook, Drive and Dropbox steps accept it directly in their attachment or file field. No extra download step is needed.

// html template

Why the loop lives in the template

Zapier has no repeat step in the ordinary plan, so a per line item render would mean one action run per row. Keeping the loop inside the template means one run per invoice, which is both faster and cheaper.

The loop in the template

{% for item in items %}
<tr>
  <td>{{ item.description }}</td>
  <td class="num">{{ item.quantity }}</td>
  <td class="num">{{ item.amount }}</td>
</tr>
{% endfor %}

What the items field must look like

[
  { "description": "Starter plan, November", "quantity": 1, "amount": "490.00" },
  { "description": "Additional seats", "quantity": 4, "amount": "400.00" }
]

A missing or malformed items array renders an invoice with an empty table rather than failing the Zap, so test with a real record before turning it on.

Questions about this template

Add this step to your Zap

Create a workspace, connect the API key in Zapier, and the action is available in the picker.