Invoice Data Extraction using Gemini AI and XML Transformation - n8n Workflow

Extract structured invoice data from uploaded PDFs using this powerful n8n workflow. Leverages Gemini AI for extraction, cleans the data, and converts structured XML to consumable JSON.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Accountants or Finance teams processing high volumes of invoices.
Developers needing reliable structured data extraction from PDFs.
Users looking for advanced examples of how to build sophisticated n8n templates.
Automation specialists integrating LLMs into data pipelines.

Overview

This advanced n8n workflow provides a robust and highly scalable solution for digitizing invoice data contained within PDFs. Unlike simple OCR, this approach uses a Large Language Model (LLM)—specifically the Google Gemini n8n node—to enforce a rigid output structure defined by an XML schema. This guarantees high accuracy and consistent data quality.

The challenge of extracting messy, varying invoice layouts is solved by first transcribing the PDF contents and then guiding the AI to adhere strictly to a predefined data model. The final step converts the AI-generated XML into clean JSON, ensuring the output of this n8n workflow is ready for immediate integration into any downstream system, such as an ERP or CRM.

How it Works

The entire process is initiated by the n8n trigger, which is the 'On form submission' n8n node, allowing users to upload a PDF file.


  1. PDF Transcription: The uploaded file immediately passes to the 'Extract from File' n8n node, which performs OCR on the PDF to generate a raw text transcription of the invoice content.

  2. Data Preparation: The 'Limpio data' Set n8n node then cleans the raw transcription (removing excessive newlines) and, most importantly, defines a highly detailed XML structure template for all required invoice fields (vendor, billed to, items, financials).

  3. AI Extraction: The Google Gemini n8n node receives the raw text and the XML structure. It is prompted to fit the unstructured invoice data into the specified XML format. This is the core data extraction step in the n8n workflow.

  4. XML Cleanup: Because LLMs sometimes include markdown formatting, the 'Limpio XML' Set n8n node removes wrappers like ```xml and strips unnecessary internal whitespace to ensure a perfectly valid XML string.

  5. Final Conversion: The 'XML to JSON' n8n node processes the cleaned XML string, transforming the structured data into a standard, accessible JSON format, completing the execution of this powerful n8n templates solution.

Installation Guide


  1. Import: Copy the provided JSON code and import it directly into your n8n instance.

  2. Credentials: You must configure credentials for the Google Gemini (PaLM) n8n node. Ensure you have a valid API key linked to a supported Google AI model.

  3. Webhook Setup: Activate the 'On form submission' n8n trigger. After activation, open the form URL (found in the n8n node configuration) in your browser to test the workflow by uploading a sample PDF invoice.

  4. Activation: Save and activate the n8n workflow. It is now ready to process files.

Node Details

On form submission (n8n trigger): The entry point of the n8n workflow. It is configured to accept a file upload named 'data'.
Extract from File (n8n node): Essential for PDF processing. It uses the pdf operation to convert the binary file into raw text data.
Limpio data (Set n8n node): Cleans the extracted text and defines the detailed estructuraXML variable, which serves as the instruction template for the Gemini model.
Message a model (Google Gemini n8n node): The core intelligence of this n8n workflow. It uses the defined XML schema to structure the messy invoice text data.
Limpio XML (Set n8n node): Performs crucial post-processing on the AI output, cleaning the XML string by stripping markdown and excess whitespace, ensuring compatibility with the next n8n node.
XML to JSON (XML n8n node): The final step, converting the validated XML string into a structured JSON object, making the extracted data easily usable.

Related n8n Workflows

Free

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

Automation consultant with over 10 years of experience specializing in AI, no-code, and workflow optimization. I’ve delivered tailored AI and NLP solutions across real estate, healthcare, and more, enhancing efficiency and customer experiences. Proficient in tools like Make, Airtable, and Zapier, I also integrate GPT models to create scalable, innovative automations. Contact me to discuss custom n8n workflows or advanced automations to streamline your processes.

Featured*