HomeAbout MeBook a Call

Automate Supply Chain PDF Invoices with Gemini API and Apps Script

By Vo Tu Duc
March 29, 2026
Automate Supply Chain PDF Invoices with Gemini API and Apps Script

Manual invoice processing is a massive hidden tax on your supply chain, leading to costly errors, delayed payments, and wasted talent. Discover the true cost of relying on static PDFs and learn how freeing your team from tedious data entry can unlock strategic growth.

image 0

The Cost of Manual Invoice Processing in Supply Chains

Supply chains are the lifeblood of global commerce, but behind the physical movement of goods lies a massive, often sluggish flow of documentation. At the center of this administrative burden is the vendor invoice. Despite living in a digital-first era, a staggering number of supply chain invoices are still exchanged as unstructured, static PDF documents.

Relying on manual human intervention to process these documents incurs a massive hidden tax on your operations. It is not just about the direct labor costs of manual data entry; manual processing introduces high error rates, delayed vendor payments, missed early-payment discounts, and ultimately, disrupted procurement cycles. When your Accounts Payable (AP) and procurement teams are bogged down acting as human data-routers, they are unable to focus on strategic vendor management, spend analysis, or supply chain optimization. In a modern cloud ecosystem, treating skilled employees as a bridge between a PDF and an ERP system is a costly misallocation of resources.

Identifying Bottlenecks in Vendor PDF Ingestion

To engineer a better solution, we first have to map the friction points. In a typical supply chain workflow, the vendor PDF ingestion pipeline is riddled with operational bottlenecks.

The journey usually begins in an inbox. Vendors email PDF invoices to a centralized billing or AP email address. From there, the workflow degrades into a highly manual triage process:

image 1
  1. Discovery and Extraction: An AP specialist must monitor the inbox, open the email, and manually download the attachment.

  2. Visual Parsing: The specialist visually scans the document to locate critical data points—Purchase Order (PO) numbers, line-item quantities, SKU codes, tax amounts, and freight charges.

  3. Data Entry: The extracted data is manually keyed into an ERP, accounting software, or inventory management system.

  4. Validation: The entered data must be cross-referenced against original purchase orders and receiving reports (three-way matching).

The bottleneck isn’t just the typing; it is the constant context switching and the sheer volume of unstructured data. When a global supply chain interacts with hundreds or thousands of distinct vendors, this manual ingestion pipeline inevitably chokes. Emails get missed, attachments get buried in local drive folders, and the time-to-process for a single invoice can stretch from minutes to days.

Why Traditional OCR Fails at Complex Layouts

For years, the standard technological band-aid for this ingestion bottleneck has been Optical Character Recognition (OCR). However, anyone who has tried to scale traditional OCR across a diverse vendor base knows its painful limitations. Traditional OCR is fundamentally rigid—it relies on predefined templates, bounding boxes, and zonal extraction rules.

Supply chain invoices, by nature, feature highly complex and infinitely variable layouts. Traditional OCR fails here for several critical reasons:

  • Template Brittleness: Vendor A might place the “Total Amount” in the top right corner, while Vendor B buries it at the bottom of a multi-page nested table. If a vendor updates their billing software and shifts a column by just two inches, a traditional OCR template breaks entirely, requiring a developer to manually remap the extraction logic.

  • Inconsistent Terminology: Supply chain documents lack standardization. A purchase order number might be labeled as “PO#”, “Purch. Ref.”, “Order ID”, or simply “Reference”. Rule-based OCR struggles to adapt to these linguistic variations.

  • Complex Line Items: Supply chain invoices frequently contain multi-page tables with nested line items, varying descriptions, and inconsistent row heights. Traditional OCR often concatenates these rows incorrectly, destroying the relationship between a SKU, its quantity, and its unit price.

Ultimately, traditional OCR can read the text, but it lacks semantic understanding. It does not know what the text means in the broader context of a supply chain transaction. This brittleness makes legacy OCR impossible to scale without a dedicated IT team constantly maintaining templates—a reality that defeats the entire purpose of Automated Job Creation in Jobber from Gmail and highlights the need for a more intelligent, generative approach.

Architecting an Automated Ingestion Pipeline

To transform a chaotic influx of supply chain invoices into a streamlined, queryable dataset, we need a robust ingestion pipeline. The goal here isn’t just to read a document; it is to build an event-driven architecture that catches an invoice the moment it arrives, processes it intelligently, and routes the extracted data to a centralized repository without human intervention. By leaning into the Google ecosystem, we can achieve this with minimal infrastructure overhead, relying entirely on serverless execution and advanced generative AI models.

Core Components of the Automatically create new folders in Google Drive, generate templates in new folders, fill out text automatically in new files, and save info in Google Sheets Tech Stack

Building this pipeline doesn’t require spinning up complex Kubernetes clusters or managing dedicated message queues. Instead, we can orchestrate the entire workflow using the built-in, serverless capabilities of AC2F Streamline Your Google Drive Workflow. Here are the core components that make up our architecture:

  • Gmail (The Ingestion Layer): This acts as our primary trigger. Using advanced search filters, we can isolate incoming emails from specific supply chain vendors or those containing specific subject lines (e.g., “Invoice attached”) alongside PDF attachments.

  • Google Drive (The Staging Area): Before processing, the PDF attachments are automatically saved to a designated Drive folder. This provides a reliable, long-term audit trail and a stable file reference for the Gemini API to access during the extraction phase.

  • AI Powered Cover Letter Automation Engine (The Orchestrator): Often the unsung hero of Automated Client Onboarding with Google Forms and Google Drive., Apps Script serves as the serverless glue binding our pipeline together. We use it to set up time-driven triggers (cron jobs), interact with the Gmail and Drive APIs, construct the payload for the Gemini API, and handle the asynchronous responses.

  • Google Sheets (The Data Destination): The final resting place for our parsed data. Once the AI extracts the relevant fields, Apps Script appends this data as a new row in a structured Google Sheet. From here, the data is instantly available for downstream ERP integrations, automated payment workflows, or Looker Studio dashboards.

Leveraging Gemini Pro for Structured JSON Extraction

Traditional Optical Character Recognition (OCR) systems are notoriously brittle. They rely on rigid bounding boxes and zonal templates, which inevitably break the moment a vendor updates their invoice layout or a new supplier is onboarded. This is where the Gemini API completely changes the paradigm.

By utilizing a multimodal model like Gemini 1.5 Pro, we bypass the limitations of traditional OCR entirely. Gemini can natively ingest the PDF document, understand its spatial layout, and comprehend the semantic relationship between different text elements, tables, and logos.

To make this work programmatically within our Apps Script pipeline, we leverage Structured JSON Extraction. Instead of asking Gemini for a conversational summary of the invoice, we engineer a prompt that strictly enforces a JSON schema output.

Here is how we optimize the Gemini interaction for pipeline stability:

  1. System Instructions: We define a strict persona for the model (e.g., “You are a highly accurate, deterministic data extraction API specializing in supply chain logistics.”).

  2. Multimodal Prompting: We pass the raw PDF file directly to the model alongside our extraction instructions, allowing it to process the visual and textual data simultaneously.

  3. JSON Schema Enforcement: We utilize the Gemini API’s response_mime_type: "application/json" configuration. We provide a predefined JSON structure, instructing the model to extract specific keys such as vendor_name, invoice_date, purchase_order_number, and a nested array of line_items.

Because Gemini understands context, it can accurately identify the “Total Amount” whether it is labeled as “Total Due,” “Amount Payable,” or buried at the bottom of a complex, multi-page table. The model returns a clean, predictable JSON string that Apps Script can easily parse (JSON.parse()) and map directly to our Google Sheets columns. This seamless transition from unstructured document pixels to structured, deterministic JSON is the cognitive engine that powers the entire automation.

Building the Solution Step by Step

Let’s roll up our sleeves and dive into the code. The architecture of our automation relies on Genesis Engine AI Powered Content to Video Production Pipeline acting as the serverless glue between Google Drive, the Gemini API, and Google Sheets. To make this robust and scalable, we will break the implementation down into three logical phases: file ingestion, AI-driven extraction, and data storage.

Configuring DriveApp to Monitor New Invoices

The first step is establishing a mechanism to detect when a new supply chain invoice lands in a specific Google Drive folder. Whether these invoices are uploaded manually by your logistics team or routed automatically via Gmail attachments, they need a central staging area.

Using the DriveApp service in Architecting Multi Tenant AI Workflows in Google Apps Script, we can query a designated “Incoming Invoices” folder for PDF files. To prevent the script from processing the same invoice twice, we will implement a simple workflow: once a file is processed, it gets moved to a “Processed Invoices” folder.

Here is how you configure the script to monitor and retrieve these files:


const INCOMING_FOLDER_ID = 'YOUR_INCOMING_FOLDER_ID';

const PROCESSED_FOLDER_ID = 'YOUR_PROCESSED_FOLDER_ID';

function processNewInvoices() {

const incomingFolder = DriveApp.getFolderById(INCOMING_FOLDER_ID);

const processedFolder = DriveApp.getFolderById(PROCESSED_FOLDER_ID);

// Search specifically for PDF files

const files = incomingFolder.searchFiles('mimeType = "application/pdf"');

while (files.hasNext()) {

const file = files.next();

Logger.log(`Processing invoice: ${file.getName()}`);

// Step 2 & 3 will be called here

const extractedData = extractDataWithGemini(file);

if (extractedData) {

writeToSheet(extractedData);

// Move file to processed folder to avoid duplicate processing

file.moveTo(processedFolder);

}

}

}

By attaching a Time-driven trigger (e.g., running every 15 minutes) to the processNewInvoices function, you create an automated polling mechanism that requires zero human intervention.

Sending PDFs to the Gemini API for Attribute Parsing

Once we have isolated a new PDF invoice, the real magic happens. Supply chain invoices are notoriously varied—different vendors use entirely different layouts, making traditional OCR and regex-based parsing a nightmare. The Gemini 1.5 Pro and Flash models natively support multimodal inputs, meaning we can pass the PDF directly to the model and ask it to extract what we need.

To do this in Apps Script, we must convert the PDF into a Base64-encoded string and construct a REST API payload using UrlFetchApp. We will instruct Gemini to extract specific attributes (like Vendor Name, Invoice Number, Date, and Total Amount) and mandate that it returns the response in a clean JSON format.


const GEMINI_API_KEY = 'YOUR_GEMINI_API_KEY';

const GEMINI_API_URL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${GEMINI_API_KEY}`;

function extractDataWithGemini(file) {

// Convert PDF to Base64

const blob = file.getBlob();

const base64Data = Utilities.base64Encode(blob.getBytes());

const prompt = `

You are an expert supply chain data extraction assistant.

Analyze the attached PDF invoice and extract the following attributes:

- vendorName

- invoiceNumber

- invoiceDate

- totalAmount

Return ONLY a valid JSON object with these exact keys. Do not include markdown formatting like \`\`\`json.

`;

const payload = {

"contents": [{

"parts": [

{ "text": prompt },

{

"inline_data": {

"mime_type": "application/pdf",

"data": base64Data

}

}

]

}],

"generationConfig": {

"responseMimeType": "application/json"

}

};

const options = {

"method": "post",

"contentType": "application/json",

"payload": JSON.stringify(payload),

"muteHttpExceptions": true

};

try {

const response = UrlFetchApp.fetch(GEMINI_API_URL, options);

const jsonResponse = JSON.parse(response.getContentText());

// Extract the text content from Gemini's response

const rawText = jsonResponse.candidates[0].content.parts[0].text;

return JSON.parse(rawText);

} catch (error) {

Logger.log(`Error parsing invoice with Gemini: ${error}`);

return null;

}

}

By leveraging the responseMimeType: "application/json" configuration, we force the Gemini API to adhere strictly to our requested data structure, eliminating the need to write complex string-parsing logic to clean up the AI’s output.

Writing Extracted Data to Google Sheets Automatically

The final piece of the puzzle is taking the structured JSON object returned by Gemini and persisting it into a database for your procurement or finance teams to review. Google Sheets is the perfect lightweight, collaborative tool for this.

Using the SpreadsheetApp service, we can open our target ledger and append the newly extracted attributes as a new row.


const SPREADSHEET_ID = 'YOUR_SPREADSHEET_ID';

const SHEET_NAME = 'Invoice Ledger';

function writeToSheet(invoiceData) {

const sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME);

// Map the JSON keys to an array representing the columns in the sheet

const rowData = [

new Date(), // Timestamp of processing

invoiceData.vendorName || 'Unknown',

invoiceData.invoiceNumber || 'N/A',

invoiceData.invoiceDate || 'N/A',

invoiceData.totalAmount || 0

];

// Append the data to the next available empty row

sheet.appendRow(rowData);

Logger.log(`Successfully logged invoice ${invoiceData.invoiceNumber} to Sheets.`);

}

The appendRow() method is highly efficient and automatically finds the bottom of your dataset, ensuring that concurrent invoice processing doesn’t overwrite existing records. With this final function in place, your pipeline is complete: Drive catches the file, Gemini reads and structures the unstructured PDF data, and Sheets logs it securely for downstream reporting and ERP integration.

Optimizing and Scaling Your Architecture

Transitioning your invoice automation pipeline from a successful proof-of-concept to a production-grade enterprise solution requires a strategic approach to architecture. In the fast-paced world of supply chain management, invoice volumes can spike unpredictably—especially around end-of-month billing cycles or seasonal peaks. To ensure your Automated Discount Code Management System and Google Cloud integration remains robust, you must proactively design for scale, gracefully handle system constraints, and guarantee the integrity of your financial data.

Handling API Rate Limits and Execution Timeouts

When bridging Google Apps Script with the Gemini API, you are operating within two distinct sets of boundaries: Apps Script’s execution quotas and Gemini’s rate limits.

Google Apps Script has a strict 6-minute execution time limit per trigger. If you attempt to process a backlog of 50 complex, multi-page PDF invoices in a single run, your script will inevitably time out, leaving you with partially processed data. Furthermore, the Gemini API enforces Quotas on Requests Per Minute (RPM) and Tokens Per Minute (TPM). Hitting these limits will result in HTTP 429 (Too Many Requests) errors.

To build a resilient, scalable pipeline, implement the following architectural patterns:

  • Stateful Batch Processing: Instead of processing all unread invoices at once, configure your Apps Script time-driven trigger to run frequently (e.g., every 10 minutes) and process a small, fixed batch of emails (e.g., 5 at a time). Use the PropertiesService in Apps Script to store the messageId or timestamp of the last processed email. This creates a reliable continuation token, allowing the script to pick up exactly where it left off without exceeding the 6-minute window.

  • Exponential Backoff for API Calls: Network hiccups and rate limits are a reality of cloud engineering. Wrap your UrlFetchApp calls to the Gemini API in a retry loop utilizing exponential backoff. If the API returns a 429 error, pause the script using Utilities.sleep(), progressively increasing the wait time between retries before failing gracefully.

  • Offloading to Google Cloud (For Massive Scale): If your supply chain processes thousands of invoices daily, Apps Script alone may not suffice. Use Apps Script simply as an event listener that pushes the PDF payload (or a link to the file in Google Drive) to a Google Cloud Pub/Sub topic. From there, a Cloud Function or Cloud Run service—which boasts much higher timeout limits and concurrent processing capabilities—can handle the heavy lifting of calling the Gemini API and routing the extracted data to BigQuery or your ERP system.

Ensuring Data Accuracy and System Reliability

In supply chain finance, a hallucinated digit or a missed decimal point can lead to massive overpayments or inventory discrepancies. While Gemini is exceptionally powerful at unstructured data extraction, Large Language Models (LLMs) inherently require guardrails to ensure deterministic, reliable outputs.

To guarantee data accuracy and system reliability, your architecture must incorporate strict validation layers and fail-safes:

  • Enforce Structured JSON Outputs: Never rely on parsing free-text responses. When calling the Gemini API, utilize the response_mime_type: "application/json" parameter and provide a strict responseSchema. By forcing Gemini to return a predefined JSON structure (e.g., requiring specific data types for invoice_number, line_items, and total_amount), you eliminate parsing errors and ensure the output is immediately machine-readable.

  • **Implement Deterministic Validation Logic: **LLMs should extract data, but traditional code should validate it. Add a post-processing step in your Apps Script or Cloud Function to perform basic arithmetic checks. For example, iterate through the extracted line items to verify that (Quantity* Unit Price) + Tax == Total Amount. If the math doesn’t align, flag the invoice for manual review.

  • Idempotency to Prevent Duplicate Processing: Supply chain vendors often send the same invoice multiple times. Your system must be idempotent. Before sending a PDF to Gemini, generate a unique hash of the PDF file or extract the Message-ID from Gmail, and check it against your database (Google Sheets or BigQuery). If the hash exists, skip the processing to save API tokens and prevent duplicate ledger entries.

  • Human-in-the-Loop (HITL) Fallback: Design your system with the assumption that edge cases will occur. If an invoice fails the deterministic validation, or if the Gemini API returns a low confidence score (if prompted to evaluate its own extraction), route the data to a dedicated “Needs Review” tab in Google Sheets. By highlighting the discrepancies, your accounts payable team can manually verify the flagged invoices without bottlenecking the fully automated flow of standard invoices.

Next Steps for Your Workspace Infrastructure

Automating your supply chain PDF invoices using the Gemini API and Google Apps Script is a massive leap forward in operational efficiency, but it is only the beginning. Once you have successfully deployed this AI-driven workflow, the next logical phase is to look at your broader Automated Email Journey with Google Sheets and Google Analytics and Google Cloud infrastructure. Scaling from a single automated process to an enterprise-grade, event-driven architecture requires strategic planning.

To truly maximize your return on investment in the Google ecosystem, consider the following architectural enhancements:

  • Data Centralization and Analytics: Instead of merely dumping extracted invoice data into a Google Sheet, route this structured data into Google BigQuery. By doing so, you can build real-time supply chain dashboards in Looker Studio, enabling predictive analytics on vendor pricing, payment cycles, and procurement bottlenecks.

  • Transitioning to Cloud Functions: As your invoice volume grows, you may hit Apps Script execution quotas. Migrating the heavy lifting of your Gemini API calls to Google Cloud Functions or Cloud Run ensures your architecture remains highly scalable, resilient, and capable of processing thousands of PDFs concurrently.

  • Enhanced Security and Governance: Financial documents are highly sensitive. Implementing robust Identity and Access Management (IAM) policies, utilizing Automated Google Slides Generation with Text Replacement Data Loss Prevention (DLP) rules, and leveraging Cloud Audit Logs will ensure your automated workflows remain compliant with industry security standards.

  • Expanding AI Capabilities: With the foundation laid, you can extend Building Self Correcting Agentic Workflows with Vertex AI and Gemini to other supply chain touchpoints—such as automating vendor onboarding, summarizing complex logistics contracts, or generating automated email responses for billing discrepancies.

Scaling these solutions requires a deep understanding of both Workspace limitations and Cloud Engineering best practices. Bridging the gap between a functional script and a production-ready cloud architecture is where expert guidance becomes invaluable.

Book a GDE Discovery Call with Vo Tu Duc

If you are ready to transition your automated workflows from a proof-of-concept to a resilient, enterprise-wide solution, getting expert architectural advice is your best next step.

Vo Tu Duc is a recognized Google Developer Expert (GDE) with deep-rooted expertise in Google Cloud, Automated Order Processing Wordpress to Gmail to Google Sheets to Jobber, and Cloud Engineering. Booking a discovery call with a GDE provides you with a unique opportunity to have your current infrastructure reviewed by an industry leader who understands the intricate synergies between Google’s collaborative tools and its robust cloud backend.

During this discovery call, you can expect to cover:

  • Architecture Review: A comprehensive audit of your current Apps Script and Gemini API integrations to identify potential bottlenecks, security gaps, or quota limitations.

  • Scalability Roadmap: Tailored recommendations on how to migrate lightweight Workspace automations into scalable Google Cloud environments using serverless technologies.

  • Custom AI Integration: Brainstorming and feasibility assessments for applying advanced generative AI models to your specific supply chain and operational challenges.

  • Best Practices & Governance: Actionable insights on CI/CD pipelines for Apps Script, infrastructure as code (IaC), and maintaining enterprise-grade security across your Google ecosystem.

Don’t let technical debt or architecture limitations slow down your digital transformation. Reach out and book your GDE Discovery Call with Vo Tu Duc today to unlock the full potential of your Automated Payment Transaction Ledger with Google Sheets and PayPal and Google Cloud infrastructure.


Tags

Supply ChainAutomationGemini APIGoogle Apps ScriptInvoice ProcessingPDF Extraction

Share


Previous Article
Automating At Risk Student Alerts Using Google Workspace and Pub Sub
Vo Tu Duc

Vo Tu Duc

A Google Developer Expert, Google Cloud Innovator

Stop Doing Manual Work. Scale with AI.

Hi, I'm Vo Tu Duc (Danny), a recognised Google Developer Expert (GDE). I architect custom AI agents and Google Workspace solutions that help businesses eliminate chaos and save thousands of hours.

Want to turn these blog concepts into production-ready reality for your team?
Book a Discovery Call

Table Of Contents

Portfolios

AI Agentic Workflows
Change Management
AppSheet Solutions
Strategy Playbooks
Cloud Engineering
Product Showcase
Uncategorized
Workspace Automation

Related Posts

Auto Generating Maintenance Manuals From Technical Specs Using Gemini
March 29, 2026
© 2026, All Rights Reserved.
Powered By

Quick Links

Book a CallAbout MeVolunteer Legacy

Social Media