How to Automate Invoice PDF Generation with a REST API
Still creating invoices manually in Excel? Copying data between spreadsheets and praying you didn't mistype an amount? There's a better way.
With a PDF generation API, you can turn invoice data into professionally formatted PDFs — and email them to customers — with a single API call. This guide walks you through the implementation with real code examples.
Why Automate with an API?
The Manual Process Problem
- Slow: Create in Excel → export to PDF → attach to email → send
- Error-prone: Manual data entry leads to typos in amounts and addresses
- Doesn't scale: 10 invoices per month is fine. 1,000 is a nightmare.
The API Approach
- Seconds, not hours: Generate from database values instantly
- Zero errors: Code handles calculations and formatting
- Scales infinitely: Same code handles 10 or 10,000 invoices
Architecture Overview
Order data → HTML template → PDF Generation API → Email delivery
You need three things:
- An HTML template — the invoice layout
- Data — customer name, line items, amounts
- One API call — generates the PDF and sends the email
Step 1: Create an HTML Template
Create an invoice template using HTML and CSS. With FUNBREW PDF's template engine, use {{variable_name}} placeholders for dynamic values.
<div style="font-family: -apple-system, sans-serif; max-width: 800px; margin: 0 auto; padding: 40px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 40px;">
<div>
<h1 style="font-size: 28px; margin: 0;">Invoice</h1>
<p style="color: #6b7280;">Invoice #: {{invoice_number}}</p>
<p style="color: #6b7280;">Date: {{issue_date}}</p>
</div>
<div style="text-align: right;">
<p style="font-weight: 700;">Your Company Inc.</p>
<p style="color: #6b7280; font-size: 13px;">123 Business St, Suite 100</p>
</div>
</div>
<div style="margin-bottom: 32px;">
<p style="font-size: 18px; font-weight: 700;">Bill to: {{customer_name}}</p>
</div>
<table style="width: 100%; border-collapse: collapse; margin-bottom: 32px;">
<thead>
<tr style="background: #f8fafc;">
<th style="text-align: left; padding: 12px; border-bottom: 2px solid #e5e7eb;">Item</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">Qty</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">Price</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">Total</th>
</tr>
</thead>
<tbody>{{line_items}}</tbody>
</table>
<div style="text-align: right; margin-bottom: 32px;">
<p>Subtotal: ${{subtotal}}</p>
<p>Tax: ${{tax}}</p>
<p style="font-size: 20px; font-weight: 700;">Total: ${{total}}</p>
</div>
<div style="border-top: 1px solid #e5e7eb; padding-top: 16px; color: #9ca3af; font-size: 12px;">
<p>Due date: {{due_date}}</p>
<p>Payment: Wire transfer to Account #1234567</p>
</div>
</div>
Register this template in the FUNBREW PDF dashboard.
Step 2: Generate an Invoice PDF via API
JavaScript (Node.js)
const response = await fetch("https://pdf.funbrew.cloud/api/pdf/generate-from-template", {
method: "POST",
headers: {
"Authorization": "Bearer sk-your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
template: "invoice",
variables: {
invoice_number: "INV-2026-0042",
issue_date: "March 26, 2026",
customer_name: "Acme Corp",
line_items: buildLineItemsHtml(items),
subtotal: "1,500.00",
tax: "150.00",
total: "1,650.00",
due_date: "April 30, 2026",
},
email: {
to: "billing@acme.com",
subject: "Invoice INV-2026-0042 from Your Company",
},
options: { engine: "quality" },
}),
});
const result = await response.json();
console.log("PDF URL:", result.data.download_url);
Python
import requests
response = requests.post(
"https://pdf.funbrew.cloud/api/pdf/generate-from-template",
headers={"Authorization": "Bearer sk-your-api-key"},
json={
"template": "invoice",
"variables": {
"invoice_number": "INV-2026-0042",
"issue_date": "March 26, 2026",
"customer_name": "Acme Corp",
"line_items": build_line_items_html(items),
"subtotal": "1,500.00",
"tax": "150.00",
"total": "1,650.00",
"due_date": "April 30, 2026",
},
"email": {
"to": "billing@acme.com",
"subject": "Invoice INV-2026-0042 from Your Company",
},
"options": {"engine": "quality"},
},
)
print("PDF URL:", response.json()["data"]["download_url"])
PHP (Laravel)
$response = Http::withToken('sk-your-api-key')
->post('https://pdf.funbrew.cloud/api/pdf/generate-from-template', [
'template' => 'invoice',
'variables' => [
'invoice_number' => 'INV-2026-0042',
'issue_date' => 'March 26, 2026',
'customer_name' => 'Acme Corp',
'line_items' => $this->buildLineItemsHtml($items),
'subtotal' => '1,500.00',
'tax' => '150.00',
'total' => '1,650.00',
'due_date' => 'April 30, 2026',
],
'email' => [
'to' => 'billing@acme.com',
'subject' => 'Invoice INV-2026-0042 from Your Company',
],
'options' => ['engine' => 'quality'],
]);
$downloadUrl = $response->json('data.download_url');
Step 3: Batch Processing for Monthly Billing
For end-of-month billing runs, use the batch API to generate all invoices at once.
const response = await fetch("https://pdf.funbrew.cloud/api/pdf/batch", {
method: "POST",
headers: {
"Authorization": "Bearer sk-your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
items: customers.map(c => ({
type: "template",
template: "invoice",
variables: buildInvoiceVars(c),
filename: `invoice-${c.id}-202603.pdf`,
email: {
to: c.email,
subject: `Invoice for ${c.name} - March 2026`,
},
})),
}),
});
const { data } = await response.json();
console.log(`Generated ${data.results.length} invoices`);
Get Notified with Webhooks
Want a Slack notification when invoices are generated? Set up a webhook URL in the dashboard. You'll receive a POST request like this:
{
"event": "pdf.generated",
"data": {
"filename": "invoice-42-202603.pdf",
"file_size": 48210,
"download_url": "https://pdf.funbrew.cloud/dl/abc123..."
}
}
Summary
Automating invoice PDF generation with an API eliminates manual work and errors:
- Create a template once, reuse forever
- One API call = PDF + email delivery
- Batch API for monthly billing runs
- Webhooks for completion notifications
Get started with the free plan (30 PDFs/month). Build your invoice template in the editor and test it in the Playground.