Prompt
How do I extract invoice fields from PDFs automatically?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To extract invoice fields from PDFs automatically, you generally use a pipeline with 4 parts:
- Get the text from the PDF
- Detect which parts are invoice fields
- Normalize the values
- Export to structured data like JSON, CSV, or a database
1) Identify the PDF type
There are two main cases:
- Digital PDFs: text is selectable/copyable
- Use PDF text extraction tools directly
- Scanned PDFs: page is just an image
- Use OCR first
2) Common tools
For text-based PDFs
- Python libraries:
pdfplumber,PyMuPDF (fitz),pypdf - Good for extracting text, tables, and coordinates
For scanned PDFs
- OCR tools:
Tesseract,EasyOCR,AWS Textract,Google Document AI,Azure Form Recognizer - Better when invoices are scanned images or low-quality PDFs
For field extraction
- Rule-based parsing
- regex for invoice number, date, totals, tax, etc.
- Template-based extraction
- if invoices come from a few known vendors/formats
- ML/document AI
- if invoice formats vary a lot
3) Fields you typically extract
Common invoice fields:
- Invoice number
- Invoice date
- Vendor name
- Vendor address
- Buyer/customer name
- PO number
- Line items
- Subtotal
- Tax/VAT
- Total amount
- Currency
- Due date
- Payment terms
4) Simple approach with Python
Step A: Extract text
import pdfplumber
with pdfplumber.open("invoice.pdf") as pdf:
text = ""
for page in pdf.pages:
text += page.extract_text() or ""
print(text)
Step B: Extract fields with regex
import re
invoice_number = re.search(r"Invoice\s*#?:?\s*([A-Z0-9-]+)", text, re.IGNORECASE)
date_match = re.search(r"Invoice Date:?\s*([0-9]{2}/[0-9]{2}/[0-9]{4})", text, re.IGNORECASE)
total_match = re.search(r"Total:?\s*\$?([0-9,]+\.\d{2})", text, re.IGNORECASE)
data = {
"invoice_number": invoice_number.group(1) if invoice_number else None,
"invoice_date": date_match.group(1) if date_match else None,
"total": total_match.group(1) if total_match else None,
}
print(data)
5) Better approach for varied invoice layouts
If invoices differ a lot, use an invoice extraction service:
- AWS Textract AnalyzeExpense
- Google Document AI Invoice Parser
- Azure Form Recognizer prebuilt invoice model
- Open-source models: LayoutLM, Donut, invoice-specific OCR pipelines
These services can detect:
- key-value fields
- line items
- tables
- confidence scores
6) Recommended production workflow
- Upload PDF
- Detect scanned vs digital
- Extract text/OCR
- Parse invoice fields
- Validate results
- dates format
- totals sum correctly
- currency consistency
- Send low-confidence cases to human review
- Store output in JSON/database
7) Example output format
{
"invoice_number": "INV-10293",
"invoice_date": "2026-01-14",
"vendor_name": "Acme Supplies Ltd",
"subtotal": 120.00,
"tax": 24.00,
"total": 144.00,
"currency": "USD"
}
8) Practical recommendation
- If you have simple, consistent invoices: use
pdfplumber+ regex - If you have scanned invoices: use OCR
- If you have many vendors/formats: use a document AI service or ML model
If you want, I can also give you:
- a Python script that extracts common invoice fields, or
- a full OCR + parsing pipeline for scanned PDFs.