n8n Tutorial

Automate Invoice Generation with n8n

Use AI to extract billing data from emails and generate branded invoices automatically. Self-hosted, fully customizable.

Vincent Ventalon
Vincent Ventalon
·Updated
TemplateFox

About the TemplateFox Node

The official TemplateFox community node for n8n lets you generate PDFs from templates with dynamic data. Works synchronously — returns the PDF URL immediately. Compatible with n8n AI Agents.

Why automate invoices?

A client emails asking for an invoice. You open their message, copy details into a spreadsheet, format the line items, calculate totals, export to PDF, and email it back. For one client? Manageable. For dozens per week? That's hours gone.

The magic of n8n is you can add AI to the mix. An AI agent reads the email, extracts the billing details, structures them as JSON, and passes them straight to TemplateFox. No manual data entry. No copy-paste errors.

Here's what we're building: a workflow that triggers when you get an email request, uses AI to extract invoice data, generates a branded PDF, and sends it back to the client. Fully self-hosted if you want.

1

Create your invoice template

1.1 — Choose a template

Start with one of our pre-built invoice templates or create your own:

  1. Go to Templates
  2. Filter by Invoice category
  3. Choose a template that matches your style
  4. Click Use Template to add it to your dashboard
Professional Corporate Invoice template in the visual editor

The template includes a QR code for payment. Click it to configure the URL.

QR code selected showing properties panel

1.2 — Understand the template structure

The template uses dynamic fields that the AI agent will populate. Some you'll hardcode (your company info), others come from AI extraction (client details, line items).

Hardcode once (your company)

  • company_name
  • address_line1, address_line2
  • bank_name, account_number

AI extracts (per invoice)

  • client_company_name
  • client_address_line1
  • items[] (array)
  • payment_terms

Learn more: Expressions · Data Binding · Tables

1.3 — Test with sample data

Use the Preview tab to test your template with sample JSON before connecting n8n. This JSON structure is exactly what your AI agent will output.

Tip: Copy the JSON structure from the Preview tab. You'll use it as an example in your AI agent prompt.

2

Build the n8n workflow

The complete workflow: Email arrives → AI extracts data → TemplateFox generates PDF → Email sends it back.

Email TriggerAI AgentTemplateFoxSend Email

2.1 — Set up email trigger

Use the Email Trigger (IMAP) node to watch for incoming invoice requests:

  1. Add an Email Trigger (IMAP) node
  2. Configure your email credentials (Gmail, Outlook, or any IMAP server)
  3. Set the mailbox to watch (e.g., invoices@yourcompany.com)
  4. Optionally filter by subject line (e.g., contains "invoice request")

Alternative triggers: Webhook, Form submission, or schedule-based polling.

2.2 — Add AI Agent for data extraction

The AI agent reads the email and extracts structured invoice data. Add an AI Agent node and configure the prompt:

// System prompt for AI Agent
You are an invoice data extractor. From the email below, extract:
- Client name and address
- Line items (description, quantity, unit price)
- Payment terms (Net 30, Due on receipt, etc.)
Return as JSON:
{
"client_name": "...",
"client_address": "...",
"items": [
{ "description": "...", "qty": 1, "price": 100 }
],
"payment_terms": "Net 30"
}
1

Choose your LLM

OpenAI GPT-4, Anthropic Claude, or local models via Ollama.

2

Pass email content

Map {{ $json.text }} from the email trigger to the AI input.

3

Parse the output

The AI returns JSON that flows directly to the next node.

2.3 — Add TemplateFox node

Add the TemplateFox node to generate the PDF:

  1. Add a TemplateFox node
  2. Select Generate PDF operation
  3. Choose your invoice template
  4. Map fields from the AI output:
    • client_name{{ $json.client_name }}
    • items{{ $json.items }}
Invoice preview with sample data

Note: Hardcode your company details directly in the field mappings. They don't need to come from the AI.

2.4 — Send via email

Add a Send Email node to deliver the invoice:

  1. Add a Send Email node (Gmail, SMTP, etc.)
  2. Set recipient to the original sender's email
  3. Attach the PDF from TemplateFox output
  4. Activate your workflow

Done. Email comes in → AI extracts data → PDF generates → Invoice sent. All automatic.

Supported platforms

Any trigger that provides text content works. Common sources for invoice requests:

G
Gmail
O
Outlook
I
IMAP (any)
S
Slack
T
Typeform
W
Webhook
A
Airtable
N
Notion
C
Custom API

Frequently asked questions

What email providers work with this workflow?

Any email provider with IMAP support works: Gmail, Outlook, Yahoo, ProtonMail, or any custom email server. n8n's Email Trigger (IMAP) node connects to any mailbox.

How accurate is the AI data extraction?

Modern LLMs (GPT-4, Claude) are highly accurate at extracting structured data from emails. For best results, provide clear examples in your prompt. Always validate extracted amounts before sending invoices.

Can I use a local LLM instead of OpenAI?

Yes! n8n supports local LLMs via Ollama, LM Studio, or any OpenAI-compatible API. Perfect for keeping sensitive billing data on your own infrastructure.

How do I handle different email formats?

The AI agent adapts to different email formats automatically. Write your prompt to handle variations: formal vs informal, structured vs free-form, single vs multiple items.

Can I include line items and taxes?

Yes. Configure your AI prompt to extract arrays of line items. Use data binding to pass them to your template, which auto-generates rows and calculates totals.

What if the AI extraction fails?

Add an IF node after the AI agent to check for missing fields. Route failures to a manual review queue (Google Sheet, Slack notification, etc.) instead of generating incomplete invoices.

What does this automation cost?

n8n self-hosted is free. TemplateFox offers 60 free PDFs per month. You'll also pay for LLM API calls (OpenAI, Anthropic, or free with local models). View pricing.

Automate your PDF generation

Complete documentation, no-code integrations, and a powerful API to help you generate PDFs at scale. Let us handle the boring stuff.