Extract PDF invoice data to Google Sheets with Gemini API and n8n | Alpha | PandaiTech

Extract PDF invoice data to Google Sheets with Gemini API and n8n

Learn how to automate the extraction of structured data (such as totals, dates, and senders) from PDF invoices in Google Drive using the Gemini API and save it directly to Google Sheets.

Learning Timeline
Key Insights

Why Use Gemini API Instead of Standard Nodes?

The standard 'Analyze Document' node in n8n has limited options. By using the Gemini API directly, you gain access to 'hidden settings' and more granular prompt control for higher data output accuracy.

The Advantage of Multimodal AI

Gemini is highly effective for this task because it is multimodal—it can 'see' the visual layout of an invoice. Even if invoice formats vary (different layouts), the AI can still locate the 'Total Amount' without needing to set specific coordinates like traditional extraction software.
Prompts

Structured Invoice Data Extraction

Target: Gemini API
Extract the following fields from the attached invoice PDF: invoice_sender, sender_address, invoice_number, invoice_date, total_amount, and currency. Requirements: - Return the data in a clean JSON format. - Ensure the date is in YYYY-MM-DD format. - If a field is missing, return 'N/A'. - Identify the currency symbol and convert it to the 3-letter ISO code (e.g., EUR, USD).
Step by Step

How to Set Up an n8n Invoice Automation Workflow

  1. Create a dedicated Google Drive folder to store invoice PDF files.
  2. Set up a Google Sheets file with these column headers: Invoice Sender, Address, Invoice Number, Date, Total Amount, and Currency.
  3. In n8n, add a 'Google Drive' node and set it to 'Watch for New Files' to trigger whenever a new PDF is uploaded.
  4. Use a second 'Google Drive' node with the 'Download File' action to retrieve the PDF's binary data.
  5. Add a 'Google Gemini' node and select a model that supports document processing (such as Gemini 1.5 Pro/Flash).
  6. Connect the 'Gemini API' using a valid API Key from Google AI Studio.
  7. In the Gemini node configuration, upload the PDF file as input context.
  8. Write a structured prompt to instruct Gemini to extract data in JSON format (refer to the Prompt Card).
  9. Add an 'Edit Fields' (Set) node to map the JSON output from Gemini into the appropriate data format.
  10. Add a 'Google Sheets' node with the 'Append Row' action to insert the extracted data into a new row.

More from Build & Deploy Autonomous AI Agents

View All