Skip to content

🤖 n8n workflow that uses Google Gemini AI to automatically extract data from PDF receipts sent via email and log them to Google Sheets

Notifications You must be signed in to change notification settings

HeartThanakorn/n8n-pdf-receipt-ai-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

n8n PDF Receipt AI Processor

Workflow Diagram

🤖 An intelligent n8n workflow that automatically processes PDF receipts/invoices from Gmail, extracts key data using Google Gemini AI, and logs the information to Google Sheets.

✨ Features

  • 📧 Auto Email Monitoring: Watches Gmail for PDF attachments
  • 🔍 AI-Powered Extraction: Uses Google Gemini to intelligently extract receipt data
  • 📊 Google Sheets Integration: Automatically logs data to spreadsheet
  • 🎯 Smart Field Detection: Extracts supplier name, order date, amount, currency, etc.
  • 📱 Status Notifications: Sends confirmation emails after processing
  • 🔄 Real-time Processing: Processes receipts as they arrive

🏗️ Architecture

The workflow follows this intelligent process:

  1. Gmail Trigger - Monitors for PDF attachments from specific senders
  2. File Handler - Saves PDF to temporary processing directory
  3. PDF Text Extraction - Converts PDF to text using pdftotext command
  4. AI Agent - Orchestrates the data extraction and logging process
  5. Google Gemini - Intelligently extracts structured data from receipt text
  6. Google Sheets Tool - Appends extracted data to tracking spreadsheet
  7. Gmail Notification - Sends status confirmation email

📋 Extracted Data Fields

The AI extracts these 6 key fields from each receipt:

Field Description Example
Supplier name Company/vendor name "Amazon", "Starbucks"
Order date Transaction date "2024-01-15"
Order number Invoice/receipt number "ORD-12345"
Supplier email Vendor contact email "noreply@vendor.com"
Total amount Final amount paid "25.99"
Currency 3-letter ISO code "USD", "EUR", "THB"

🚀 Setup Instructions

1. Prerequisites

System Requirements

  • n8n instance (self-hosted or cloud)
  • Linux/Mac system with command line access
  • pdftotext utility installed

Install pdftotext

# Ubuntu/Debian
sudo apt-get install poppler-utils

# macOS
brew install poppler

# CentOS/RHEL
sudo yum install poppler-utils

2. API Keys & Credentials

Google Gemini API

  1. Visit Google Cloud Console
  2. Enable Generative AI API
  3. Create API key for Gemini
  4. ⚠️ Important: Monitor billing - this API has usage costs!

Gmail OAuth2

  1. Create OAuth2 credentials in Google Cloud Console
  2. Add authorized redirect URIs for your n8n instance
  3. Enable Gmail API

Google Sheets API

  1. Enable Google Sheets API in Google Cloud Console
  2. Use the same OAuth2 credentials or create separate ones

3. Google Sheets Setup

  1. Create a new Google Spreadsheet
  2. Set up columns with these exact headers:
    | Supplier name | Order date | Order number | Supplier email | Total amount | Currency |
    
  3. Copy the spreadsheet ID from the URL
  4. Share the sheet with your Google account used for n8n

4. Import & Configure Workflow

Import Workflow

  1. Download pdf-receipt-processor-workflow.json
  2. In n8n: Workflows > Import from File
  3. Upload the JSON file

Configure Credentials

  1. Gmail OAuth2: Create and link in Gmail Trigger and Gmail Tool nodes
  2. Google Gemini API: Create and link in Google Gemini Chat Model node
  3. Google Sheets OAuth2: Create and link in Google Sheets Tool node

Update Configuration

  1. Gmail Filter: Change your-email@example.com to desired sender email
  2. File Path: Ensure /tmp/n8n_pdf_processing/ directory exists and is writable
  3. Google Sheets ID: Replace YOUR_GOOGLE_SHEETS_DOCUMENT_ID with your sheet ID
  4. Notification Email: Update recipient email in Gmail Tool node

📖 Usage

Automatic Processing

  1. Send PDF Receipt: Email a PDF receipt to the monitored Gmail account
  2. Auto Processing: Workflow detects the email and processes the PDF
  3. Data Extraction: AI extracts key information from the receipt
  4. Sheet Update: Data is automatically added to your Google Sheet
  5. Confirmation: You receive an email confirming the processing

Manual Testing

  1. Open the workflow in n8n
  2. Click "Execute Workflow"
  3. Send a test PDF to trigger processing

🛠️ Troubleshooting

Common Issues

pdftotext Command Not Found

# Verify installation
which pdftotext
pdftotext -v

# If missing, install poppler-utils

Permission Denied on File Path

# Create directory and set permissions
sudo mkdir -p /tmp/n8n_pdf_processing
sudo chmod 755 /tmp/n8n_pdf_processing

AI Extraction Accuracy Issues

  • Ensure PDF text is clear and readable
  • Complex receipt layouts may need prompt tuning
  • Check if PDF is image-based (requires OCR)

Google Sheets Connection Failed

  • Verify sheet ID is correct
  • Check if sheet is shared with the service account
  • Confirm column headers match exactly

High API Costs

  • Review Gemini API usage in Google Cloud Console
  • Optimize prompts to reduce token usage
  • Set up billing alerts and quotas

🎨 Customization Ideas

Enhanced Data Extraction

  • 🏷️ Extract product categories and line items
  • 🧾 Support for different receipt formats
  • 💳 Add payment method detection
  • 🏪 Store merchant category codes

Workflow Improvements

  • 📊 Add data validation and error handling
  • 🔄 Implement retry logic for failed extractions
  • 📈 Generate monthly expense reports
  • 🎯 Smart categorization of expenses

Integration Extensions

  • 💼 Connect to accounting software (QuickBooks, Xero)
  • 📱 Send notifications to Slack/Teams
  • 🗄️ Archive processed PDFs to cloud storage
  • 📊 Create expense analytics dashboard

🔄 Alternative Approaches

Without AI (Cost-Free)

  • Use regex patterns for simple receipt formats
  • Create template-based extraction rules
  • Manual field mapping for consistent suppliers

Different AI Providers

  • Replace Gemini with OpenAI GPT
  • Use local LLM models (Ollama)
  • Try Claude API for different extraction styles

OCR Integration

For image-based PDFs:

  • Add Tesseract OCR step
  • Use Google Vision API
  • Integrate with Azure Cognitive Services

🤝 Contributing

We welcome contributions! Areas for improvement:

  • 📈 Accuracy: Better extraction prompts and validation
  • 🔧 Error Handling: Robust error recovery and logging
  • 🎨 UI/UX: Better status reporting and user feedback
  • 📊 Analytics: Usage statistics and processing insights
  • 🧪 Testing: Automated testing with sample receipts

📄 License

This workflow is provided as-is for educational and personal use. Please comply with:

  • Google Cloud APIs terms of service
  • Gmail API usage policies
  • Google Sheets API guidelines
  • Respect data privacy regulations (GDPR, etc.)

⚠️ Security & Privacy

Data Handling

  • PDFs are temporarily stored during processing
  • Extracted data contains financial information
  • Consider data retention policies

Security Best Practices

  • Never commit real credentials to version control
  • Use environment variables for sensitive data
  • Regularly rotate API keys
  • Monitor access logs and usage
  • Implement proper access controls

Privacy Considerations

  • This workflow processes financial documents
  • Ensure compliance with local privacy laws
  • Consider encryption for sensitive data
  • Implement data deletion policies

About

🤖 n8n workflow that uses Google Gemini AI to automatically extract data from PDF receipts sent via email and log them to Google Sheets

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published