Skip to content

Invoice extraction

The extract-invoice conversion reads an invoice, a PDF or a JPG, PNG, TIFF, HEIC or WebP picture of one, and writes its data as JSON, or as an Excel workbook with format: "xlsx".

  • Its words first. A PDF’s own text is read as it is. A scan, a photo or a picture is read with our OCR, and each page’s picture goes to the model too, as it keeps what OCR can miss.
  • Then a model. A language model on Cloudflare Workers AI reads the invoice from its words, and the pictures where there are any: Mistral Small 3.1, or Google’s Gemma 4 when the first isn’t available. Its answer must fit the schema below, or it’s asked once more. Neither the file’s name nor anything about you is sent with it.
  • Then our checks. Every value is checked against the invoice’s words, and the sums are compared (below).
  • Limits. Invoices of up to 10 pages. It’s an add-on: Pro, the API plans and Enterprise run it, at 5 credits a page, and test keys can try it on any plan. Accounts that keep their files in the EU can’t use it, as Workers AI has no EU setting (data location).
invoice.json
{
"supplier": {
"name": "Brightwater Print Studio LLC",
"address": "125 Tannery Row, Madison, WI 53703",
"tax_id": "00-0003177"
},
"customer": {
"name": "Juniper Ridge Veterinary Clinic",
"address": "78 Larkspur Lane, Madison, WI 53703",
"tax_id": null
},
"invoice_number": "INV2026-0562",
"issue_date": "2026-03-01",
"due_date": "2026-04-15",
"currency": "USD",
"subtotal": 6796.75,
"tax_lines": [
{
"label": "Sales tax",
"rate": 8.875,
"amount": 603.21
}
],
"tax": 603.21,
"withholding": null,
"total": 7399.96,
"line_items": [
{
"description": "Website maintenance",
"quantity": 4,
"unit_price": 384.5,
"amount": 1538
},
{
"description": "Catering for 20 guests",
"quantity": 10,
"unit_price": 451.25,
"amount": 4512.5
},
{
"description": "Copy paper, A4, 80 g, box of 5 reams",
"quantity": 3,
"unit_price": 25,
"amount": 75
},
{
"description": "Ergonomic office chair",
"quantity": 3,
"unit_price": 223.75,
"amount": 671.25
}
],
"checks": {
"values_found": {
"passed": true,
"removed": []
},
"line_items_add_up": {
"passed": true,
"sum": 6796.75,
"subtotal": 6796.75
},
"tax_lines_add_up": {
"passed": true,
"sum": 603.21,
"tax": 603.21
},
"total_adds_up": {
"passed": true,
"expected": 7399.96,
"total": 7399.96
}
}
}
  • Parties: supplier issued the invoice and customer is billed, each with a name, an address on one line and a tax_id as printed.
  • Dates and currency: issue_date and due_date as YYYY-MM-DD, always real calendar days; currency as its ISO 4217 code.
  • Amounts: plain numbers with a dot before decimals. subtotal is before tax, tax_lines lists each tax as printed (its label, its rate in percent and its amount), tax adds them up, withholding is an amount withheld from the total, such as Spain’s IRPF, as a positive number, and total is what the invoice says is due.
  • Line items: in order, each with its description, quantity, unit_price and amount; a discount line’s amount is negative.
  • Missing values are null: what the invoice doesn’t state, and what the checks removed.

We never change a value the model read. We check it, and the output says what we found in checks:

  • values_found: every value must be in the invoice’s own words: amounts however the invoice writes them (1.234,56, 1 234,56, (65.00)), dates in any of their printed forms, names and numbers word by word. A value that isn’t there is set to null and its path is listed in removed, such as customer.tax_id or line_items[2].amount. The tax counts as found when it’s printed or when it’s the sum of the tax lines found, as an invoice with two rates often prints no total of them.
  • line_items_add_up: the line items’ amounts against the subtotal.
  • tax_lines_add_up: the tax lines’ amounts against the tax.
  • total_adds_up: the subtotal and tax, less any withholding, against the total.

Each sum says passed, with the figures it compared; passed is null when a figure it needs is missing. Sums agree within a cent. A failed check doesn’t fail the job: it’s the place to look before you rely on the figures.

With format: "xlsx" the same data comes as four sheets: Invoice (one field a row), Line items, Tax lines and Checks. Amounts, rates and quantities are numbers you can sum, and dates are ISO dates.

The JSON output follows this schema (download):

invoice-extraction.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"supplier": {
"type": "object",
"properties": {
"name": {
"type": [
"string",
"null"
]
},
"address": {
"type": [
"string",
"null"
]
},
"tax_id": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"address",
"tax_id"
],
"additionalProperties": false
},
"customer": {
"type": "object",
"properties": {
"name": {
"type": [
"string",
"null"
]
},
"address": {
"type": [
"string",
"null"
]
},
"tax_id": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"address",
"tax_id"
],
"additionalProperties": false
},
"invoice_number": {
"type": [
"string",
"null"
]
},
"issue_date": {
"anyOf": [
{
"type": "string",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
},
{
"type": "null"
}
]
},
"due_date": {
"anyOf": [
{
"type": "string",
"pattern": "^\\d{4}-\\d{2}-\\d{2}$"
},
{
"type": "null"
}
]
},
"currency": {
"anyOf": [
{
"type": "string",
"pattern": "^[A-Z]{3}$"
},
{
"type": "null"
}
]
},
"subtotal": {
"type": [
"number",
"null"
]
},
"tax_lines": {
"type": "array",
"items": {
"type": "object",
"properties": {
"label": {
"type": [
"string",
"null"
]
},
"rate": {
"type": [
"number",
"null"
]
},
"amount": {
"type": [
"number",
"null"
]
}
},
"required": [
"label",
"rate",
"amount"
],
"additionalProperties": false
}
},
"tax": {
"type": [
"number",
"null"
]
},
"withholding": {
"type": [
"number",
"null"
]
},
"total": {
"type": [
"number",
"null"
]
},
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": [
"string",
"null"
]
},
"quantity": {
"type": [
"number",
"null"
]
},
"unit_price": {
"type": [
"number",
"null"
]
},
"amount": {
"type": [
"number",
"null"
]
}
},
"required": [
"description",
"quantity",
"unit_price",
"amount"
],
"additionalProperties": false
}
},
"checks": {
"type": "object",
"properties": {
"values_found": {
"type": "object",
"properties": {
"passed": {
"type": "boolean",
"description": "True when every value the model read is in the document"
},
"removed": {
"type": "array",
"items": {
"type": "string"
},
"description": "Values the model read that the document’s words don’t hold, by path (`customer.tax_id`, `line_items[2].amount`): each is null in the output"
}
},
"required": [
"passed",
"removed"
],
"additionalProperties": false,
"description": "Every value must be found in the document’s own words"
},
"line_items_add_up": {
"type": "object",
"properties": {
"passed": {
"description": "Whether the sum holds; null when a figure it needs is missing",
"type": [
"boolean",
"null"
]
},
"sum": {
"type": [
"number",
"null"
]
},
"subtotal": {
"type": [
"number",
"null"
]
}
},
"required": [
"passed",
"sum",
"subtotal"
],
"additionalProperties": false,
"description": "The line items’ amounts against the subtotal"
},
"tax_lines_add_up": {
"type": "object",
"properties": {
"passed": {
"description": "Whether the sum holds; null when a figure it needs is missing",
"type": [
"boolean",
"null"
]
},
"sum": {
"type": [
"number",
"null"
]
},
"tax": {
"type": [
"number",
"null"
]
}
},
"required": [
"passed",
"sum",
"tax"
],
"additionalProperties": false,
"description": "The tax lines’ amounts against the tax"
},
"total_adds_up": {
"type": "object",
"properties": {
"passed": {
"description": "Whether the sum holds; null when a figure it needs is missing",
"type": [
"boolean",
"null"
]
},
"expected": {
"type": [
"number",
"null"
]
},
"total": {
"type": [
"number",
"null"
]
}
},
"required": [
"passed",
"expected",
"total"
],
"additionalProperties": false,
"description": "The subtotal and tax, less any withholding, against the total"
}
},
"required": [
"values_found",
"line_items_add_up",
"tax_lines_add_up",
"total_adds_up"
],
"additionalProperties": false,
"description": "What we checked: values are never changed, only removed when they aren’t found"
}
},
"required": [
"supplier",
"customer",
"invoice_number",
"issue_date",
"due_date",
"currency",
"subtotal",
"tax_lines",
"tax",
"withholding",
"total",
"line_items",
"checks"
],
"additionalProperties": false
}