Dynamic Invoice Generation, Encryption, and Email Delivery - n8n Workflow

Use this complete n8n workflow to automate invoice generation, encrypt PDFs using customer data, check for duplicate IDs in Google Sheets, and send the secure documents via Gmail. This powerful n8n template streamlines finance operations.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Businesses needing automated, secure invoicing solutions.
Users leveraging Google Sheets, Google Drive, and Gmail for financial records.
Developers looking for advanced n8n workflow examples utilizing code, APIs, and cloud storage.
Anyone searching for robust n8n templates for document processing.

Overview

This robust n8n workflow solves the complex challenge of secure, reliable, and automated invoice processing. Starting with a standard n8n trigger (Webhook), it ensures data integrity by first generating a unique invoice ID and checking Google Sheets to prevent duplicates—a critical core logic flow control feature of this n8n automation. Once the ID is confirmed unique, the n8n workflow leverages the PDF Generator API to dynamically create a professional PDF invoice. Crucially, it then uses the same API to encrypt the document, securing it with a customer-specific password before uploading the file to Google Drive and sending it directly to the client using a Gmail n8n node. This complete n8n workflow streamlines financial operations, enhancing security and efficiency through automated steps.

How it Works


  1. Trigger Reception: The n8n trigger (Webhook n8n node) waits for incoming JSON payloads containing all necessary invoice data (customer details, line items, customerid, email).

  2. ID Generation & Validation: The 'Generate Invoice ID' code n8n node creates a unique ID (e.g., INVOICE-XXXXX). The subsequent Google Sheets n8n node searches a specified spreadsheet for this ID.

  3. Duplication Check: The 'If Does not Exist' n8n node checks the Google Sheets output. If the row number is empty (ID is new), the flow continues. If the ID exists, the connection loops back to the 'Generate Invoice ID' node to create a new, non-duplicate ID, ensuring data integrity within the overall n8n workflow.

  4. PDF Generation: The first PDF Generator API n8n node dynamically renders the invoice based on a pre-defined template, incorporating data from the initial n8n trigger and the new invoice ID.

  5. Encryption: The second PDF Generator API n8n node takes the generated PDF file and encrypts it using the customerid from the webhook data as the owner password. This is a critical security step in this n8n template.

  6. Storage: The Google Drive n8n node uploads the newly encrypted PDF file (named using the unique invoice ID) for archival purposes.

  7. Delivery: Finally, the Gmail n8n node sends the encrypted file as an attachment to the customer's email address, including a body message explaining that the password is their customer_id. This completes the automated n8n workflow cycle.

Installation Guide


  1. Import the n8n template: Copy the provided JSON into your n8n instance using the 'New' -> 'Import from JSON' option.

  2. Webhook Setup: Activate the 'Webhook' n8n trigger node and copy the URL. This URL must be used by your external system to send invoice creation requests.

  3. Credential Setup: You must configure credentials for the following services:

Google Sheets: Required for the 'Check if ID Already Exists' n8n node.
PDF Generator API: Required for both document generation and encryption n8n nodes.
Google Drive: Required for the 'Upload file' n8n node.
Gmail: Required for the 'Send a message + file' n8n node.

  1. Google Sheets Configuration: Ensure the 'Check if ID Already Exists' n8n node points to the specific Spreadsheet and Sheet you use for tracking invoice numbers.

  2. PDF Template ID: Configure the 'Generate a PDF document' n8n node with your specific template ID from the PDF Generator API.

Node Details

Webhook (n8n trigger):
Function: Initiates the n8n workflow upon receiving an HTTP request containing customer and line-item data.
Key Configuration: Uses a unique path to receive JSON payload data.
Generate Invoice ID (Code n8n node):
Function: Executes JavaScript to create a pseudo-random, unique invoice ID starting with 'INVOICE-'.
Key Configuration: Custom JS code for randomized string generation.
Check if ID Already Exists (Google Sheets n8n node):
Function: Searches the designated Google Sheet for the newly generated invoice ID to prevent duplication.
Key Configuration: Set to search the specified Spreadsheet ID and Sheet name.
If Does not Exist (If n8n node):
Function: Implements core logic flow control. It checks if the Google Sheets lookup returned an empty result (rownumber is empty). If not empty (duplicate exists), it loops the n8n workflow back to generate a new ID.
Key Configuration: Condition ={{ $json.row
number }} is empty.
Generate a PDF document (PDF Generator API n8n node):
Function: Creates the invoice PDF dynamically using templated data from the n8n trigger and the generated ID.
Key Configuration: documentOutput is set to url; template ID must be configured.
Encrypt PDF document (PDF Generator API n8n node):
Function: Adds security to the generated PDF by encrypting it.
Key Configuration: Uses the input customer_id from the original n8n trigger data as the password (ownerPassword).
Upload file (Google Drive n8n node):
Function: Archives the final encrypted document in a specified Google Drive folder.
Key Configuration: File name is dynamically set to the unique invoice number.
Send a message + file (Gmail n8n node):
Function: Delivers the secure, encrypted invoice PDF to the customer.
Key Configuration: Sends to email from the n8n trigger payload; attaches the binary data received from the encryption n8n node; subject line uses the unique invoice ID.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by
Marián Današ
Marián Današ

Hi, I'm Marian Danas. With several years of experience at pdfgeneratorapi.com, my focus has been on robust document automation. I've recently discovered a new passion for n8n, which I believe is key to expanding our business by integrating powerful PDF generation into any automated process. I'm here to build and share workflows that connect these two worlds.

Featured*