Automatic PDF Invoice Generation via API Endpoint - n8n Workflow

Generate professional, custom PDF invoices instantly using a powerful n8n workflow triggered by a webhook. Learn how this n8n template uses custom code and PDF tools for billing automation.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Ideal User Profile

Technical users needing an efficient method to generate documents on demand.
Developers building billing or e-commerce systems requiring headless invoice generation.
Businesses looking for reliable n8n templates to automate financial document creation.
System administrators seeking to implement custom PDF processing capabilities using n8n.

Overview

This solution provides a seamless, headless method for billing systems or e-commerce platforms to generate structured PDF invoices on demand. Instead of relying on expensive third-party PDF services, this n8n template uses a specialized PDF Toolkit n8n node (from CustomJS) for high-quality HTML-to-PDF conversion. The entire n8n workflow is initiated by an external Webhook n8n trigger, which receives the necessary transaction data. A crucial step involves the Code n8n node, which expertly preprocesses the raw JSON data, calculates line item totals and the grand total, and generates the necessary HTML structures for the table and addresses. This powerful and custom approach simplifies complex financial document generation tasks within your existing n8n automation infrastructure, ensuring fast, accurate, and professional output.

How it Works

The creation of the PDF invoice follows a clear, automated pipeline within the n8n workflow:


  1. Trigger Initiation: The process starts with the Webhook n8n trigger, which exposes an API endpoint waiting for an external POST request containing the invoice transaction data.

  2. Data Setup (Mock Data): The 'Set data' n8n node is configured here to simulate the rich input data (invoice items array, addresses, invoice number, etc.) that would typically come from the Webhook payload in a production n8n workflow.

  3. Data Preprocessing: The 'Preprocess' Code n8n node executes custom JavaScript logic. This script calculates the total invoice amount by summing up item prices and quantities. It also dynamically generates the necessary HTML table row () elements for all line items and wraps the address lines in

    tags, preparing the data for the final HTML template injection.


  4. PDF Generation: The 'HTML to PDF' n8n node uses the CustomJS PDF Toolkit to take a comprehensive, internally defined HTML template (which includes CSS styling) and substitutes the data outputs from the previous Code n8n node (totals, addresses, item details).

  5. Workflow Completion: The resulting binary PDF file is then passed to the 'Respond to Webhook' n8n node, which immediately returns the file directly as the response to the initial API call, completing the high-speed, on-demand n8n workflow execution.

Installation Guide

To deploy this n8n workflow template, follow these steps:


  1. Import: Copy the provided JSON code and paste it into your n8n instance using the 'New' menu -> 'Import from JSON'.

  2. Custom Node Installation: This n8n workflow requires the @custom-js/n8n-nodes-pdf-toolkit community n8n node. Install it via the n8n interface (Settings -> Community Nodes).

  3. Credential Setup: The 'HTML to PDF' n8n node requires credentials for the CustomJS API. Ensure you have set up these credentials in your n8n instance.

  4. Webhook Activation: Activate the 'Webhook' n8n trigger node to expose its URL. You will use this URL in your external system to trigger the invoice generation.

  5. Customization: Review the JavaScript in the 'Preprocess' n8n node and the HTML in the 'HTML to PDF' n8n node to match your specific data structure and branding requirements.

Node Details

Webhook (n8n trigger): Initiates the n8n workflow upon receiving an external POST request. It is configured to respond with the output of a subsequent node, ensuring the client receives the PDF directly.
Set data (n8n node): Acts as a placeholder for the structured invoice input data (Invoice No, Bill To, From, Details array). In a live n8n workflow, this would be replaced by the data coming directly from the Webhook.
Preprocess (Code n8n node): Essential for transforming raw data. It calculates the final total and formats the list of line items and addresses into correct HTML snippet strings, making the data ready for display in the PDF template.
HTML to PDF (@custom-js/n8n-nodes-pdf-toolkit n8n node): The core document generation tool. It renders a complex, styled HTML document using data passed from the Code node, generating a binary PDF file as output.


  • Respond to Webhook (n8n node): Takes the binary PDF output from the 'HTML to PDF' n8n node and returns it as the HTTP response to the original n8n trigger request.

Related n8n Workflows

Free

Nodes: 5 Nodes
Updated: December 26 2025
View all
Created by

Featured*