Gmail PDF Invoice and Receipt Classifier & Google Drive Uploader - n8n Workflow

Automate document classification using this n8n workflow. Extract PDF invoices from Gmail, classify them with OpenAI, upload matched files to Google Drive, and optionally email them.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Small Business Owners/Freelancers: Needing automated expense tracking and filing of receipts and invoices.
Accountants/Bookkeepers: Requiring a streamlined method to collect and sort client documents based on defined criteria.
Automation Enthusiasts: Looking for powerful n8n templates that combine email automation, AI integration, and cloud storage operations.
Technical Users: Who need to leverage the power of the OpenAI n8n node for document intelligence.

Overview

Managing digital receipts and invoices received via email is a tedious task. This comprehensive n8n workflow solves this by introducing AI-powered classification directly into your document pipeline.

Starting with a simple webhook n8n trigger, the system fetches all relevant emails from Gmail within a user-defined date range, isolates PDF attachments, and utilizes the OpenAI n8n node (specifically GPT-4.1-mini in this configuration) to determine if the document matches a user-defined business cost criteria (e.g., 'software engineering business cost'). Documents that pass classification are automatically uploaded to a dedicated, dated folder in Google Drive. This powerful n8n template ensures that your filing is accurate, fast, and highly customizable.

How it Works

This automation is initiated by an external system calling the Webhook n8n trigger, providing startDate and endDate parameters.


  1. Initialization and Response: The Webhook is received, and a unique, dated folder is immediately created in Google Drive using the Create folder n8n node. Concurrently, the Respond to Webhook n8n node sends an immediate 202 status acceptance back to the calling service, ensuring fast execution.

  2. Configuration: The Configure n8n node sets critical variables, including the maximum token size for OpenAI input (maxTokenSize) and the classification term (Match on).

  3. Data Retrieval: The Get emails with attachments n8n node scans Gmail for all emails containing attachments within the defined date range. A subsequent filter further refines the email list.

  4. Attachment Iteration and Filtering: The Iterate over email attachments Code n8n node separates all binary attachments into individual items. The Is attachment a PDF? n8n node checks the file extension, discarding non-PDF files.

  5. AI Pre-check: The Read PDF email attachments n8n node extracts the textual content of the PDF. The Is text within token limit? n8n node acts as a safeguard, preventing large documents from exceeding OpenAI's context limits, thus saving costs.

  6. Classification: The OpenAI n8n node receives the PDF text and filename. It uses the custom prompt to classify the document against the 'Match on' criteria, returning a simple 'true' or 'false'.

  7. Decision and Storage: The classification result is merged back with the original PDF data. The Is matched n8n node checks if the result is 'true'. If confirmed, the original binary file is uploaded to Google Drive using the Upload file to folder n8n node.

  8. Optional Email Aggregation: The Send email with invoices? n8n node checks the initial sendEmail flag. If true, the Aggregate attachments Code n8n node compiles all successfully classified PDFs into one item, which is then sent via the Send to my accountant Gmail n8n node to the specified recipient.

Installation Guide

To use this n8n workflow, follow these steps:


  1. Import the Workflow: Copy the provided JSON data and import it into your n8n instance via the 'New' menu -> 'Import from JSON'.

  2. Set up Credentials: This n8n workflow requires three credentials:

Gmail OAuth2: Connect your Google account for reading emails and sending the final summary email.
Google Drive OAuth2: Connect your Google account for creating folders and uploading files.
OpenAI API Key: Configure this credential for the OpenAI n8n node to enable AI classification.

  1. Configure Parameters: Edit the Configure Set n8n node:

Update the Match on field to specify the exact document type you are searching for (e.g., "receipt or invoice that can be considered a software engineering business cost").
Set the Google Drive folder to upload matched PDFs ID (the parent folder where the dated subfolders will be created).
Update sendInvoicesTo with the recipient email (e.g., your accountant).

  1. Activate the Webhook: Save and activate the n8n workflow. Note the Webhook URL provided by the Webhook n8n trigger node to initiate the process externally.

Node Details

This powerful n8n workflow relies on a mix of core logic nodes and external service integrations:

Webhook (n8n trigger): The starting point. Listens for POST requests, expecting parameters like startDate, endDate, and sendEmail to define the search scope and final action.
Gmail (Get emails with attachments): Fetches emails. Uses dynamic expressions to set receivedAfter and receivedBefore based on the data provided by the n8n trigger.
Google Drive (Create folder & Upload file to folder): Manages file storage. It first creates a dynamically named folder (e.g., invoices_YYYY-MM-DD) and then uploads the classified PDF files to that location.
Read PDF email attachments (Read PDF n8n node): Essential for document intelligence. This node extracts raw text from the binary PDF attachments, making the content readable for the subsequent AI step.
OpenAI (n8n node): The core classification engine. It takes the PDF text and filename, uses a specific system prompt, and performs binary classification (true/false) based on the criteria set in the Configure node.
Configure (Set n8n node): Defines crucial variables such as the AI classification criteria (Match on) and token limits (maxTokenSize), demonstrating how to use dynamic configuration within an n8n template.


  • Iterate over email attachments (Code n8n node): A custom n8n node script used to transform the complex array of binary attachments into individual items, allowing linear processing of each file.

Related n8n Workflows

Free

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

Software Engineer and Architect for well over 10 years. Chromium project expert. Full Stack beast. Workflow automation enthusiast. Founder of BrowseWiz: in-browser AI assistant and AI agent platform with human in-the-loop.

Featured*