Pull data out of invoices, receipts and PDFs

You will be able to extract fields such as supplier, date, amount and GST from documents into a spreadsheet.

Pulling fields such as the supplier, date and amounts out of invoices and receipts is one of the most useful things an AI step can do. Otherwise someone has to type them in by hand. It is also a place where AI makes confident mistakes with numbers. A misplaced decimal point can turn S$1,250 into S$125 (an invented example), and the wrong row in your sheet looks just as tidy as a correct one. You need a flow that copies the fields, checks them with formulas and sends anything doubtful to a person.

Arif handles supplier invoices for a business with site supervisors. At the end of each month he sits with a stack of them: PDFs from email, phone photos taken by the site supervisors, and a few crumpled paper receipts from a hardware shop in Geylang. He types the supplier, date, invoice number, amount and GST into the accounts sheet one by one. It is slow, dull and prone to decimal errors.

Text PDFs, scans and photos

Not all PDFs are the same underneath. A text PDF contains real text, and PDFs generated by accounting software or online invoicing tools usually are this kind. You can tell because you can select the words with your cursor. Tools can read the text directly.

Scanned pages, photos of receipts and PDFs made by phone scanning apps are only pictures of text. Before anything can read the words, they must go through text recognition (OCR), which turns an image of text into text. Some automation tools and AI steps do OCR automatically when given an image or scanned PDF, and others need a separate step, so check what yours supports.

Text recognition adds errors. A crumpled receipt, poor lighting, faded thermal print or handwriting can turn a 7 into a 1 or drop a decimal point. Scans and photos need more checking than text PDFs. If your tool can report which kind of document it received, the flow should record that.

Writing the extraction prompt

Use the same discipline as Lesson 5.2, Make the model pick from a fixed list: name each field exactly, state the format for each, and ask for a small JSON object. Turn on the tool's structured output option if it has one.

Arif's prompt asks for the supplier name, invoice number, invoice date in year-month-day format, subtotal before GST, GST amount, total, and a list of line items with description and amount. It tells the model to use null, meaning empty, for any field it cannot find. It also tells the model never to guess or calculate a value that is not printed on the document, because models are poor at arithmetic, as Lesson 5.1, What an AI step is good for inside a workflow, explains. The model copies what the document says, and the flow does the sums.

Checking the figures with formulas

After extraction, add a checking step that uses a formula or the tool's built-in math functions, never the model. Two checks catch most errors. First, the line items should add up to the subtotal. Second, the subtotal plus GST should equal the total. Allow a few cents of difference for rounding.

Take an invoice with made-up figures and three line items: S$120.00 for tiles, S$85.50 for grout and adhesive, and S$40.00 for delivery. These add up to S$245.50, which matches the printed subtotal. The printed GST is S$22.10, so the total should be S$267.60, because 245.50 plus 22.10 comes to 267.60. Suppose the extraction returns a total of S$287.60. The check fails because a 6 was read as an 8, either by the model or by text recognition. The flow must not log this row as correct.

Do not use the check to fix the number either. When a check fails, you can't know which figure is wrong without looking at the original, so the document goes to a person.

Sending gaps and failures for review

Missing fields happen. An invoice may have no invoice number, the date may be unreadable, or the model may not find a supplier name. A filter after extraction handles these gaps. If the total, the date or the supplier is empty, or if the arithmetic check failed, the row goes to a review list and is not logged as done. Every other row is logged as done.

The review list can be a status column in the sheet marked "needs review", plus a notification to the person who checks. In Arif's automated setup he gets a daily message listing the invoices that need his review.

Linking each row to the original document

Every row should carry a link to the original file, saved in a shared folder. Without that link, a spreadsheet row is a number nobody can prove. When the accountant asks about a figure months later, anyone can click through to the source document in seconds.

The full flow runs in this order:

Check whether the document is a text PDF or a scan or photo. Run text recognition on scans and photos, either built into the tool or as a separate step. Run the extraction step with exactly named fields, stated formats, a small JSON object as output, structured output on if available, null for missing fields, and no guessing or calculating. Check the sums with formulas, not the model, allowing a few cents for rounding. Send any row with an empty total, date or supplier, or a failed check, to the review list and notify the reviewer, without fixing the number automatically. Log every other row as done, with a link to the original file in the shared folder.

The activity below asks you to run five of your own receipts or invoices through an extraction step and compare every extracted field with the document. Choose a mix: at least one text PDF, one photo and, if you have one, a long or messy document. Remove or blank out anything you are not allowed to send to an outside tool before you start.

Run five of your own receipts or invoices through an extraction step and compare each extracted field with the document.

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).