The line items of an invoice inside a PDF, how to get them out

The totals are easy, it's the lines in the middle that give you the headache

A printed invoice being read line by line through a magnifying glass

Why the totals come out first time

The invoice total, the net amount, the VAT, the date and the document number are the easy fields, and the reason is a dull one. They always sit in the same spot on the page, and there is only one of each per document.

Why the line items are another story

The first problem is that you don't know how many there are. One invoice has three lines, the next has forty-seven and runs over four pages, and on every page the headings come back, and maybe a carried-forward total as well.

The second problem is that one item doesn't necessarily fit on one printed line. The description wraps and takes up two or three lines of print, while the code, the quantity and the price stay written only on the first. So whoever is reading has to work out where one item ends and the next one begins.

Then there are the lines that aren't items at all. The discount at the bottom, the supplier's note, the shipping charge, the order reference slipped in halfway down. Things that look like an item without being one.

And finally every supplier puts the columns wherever they please. One writes the code first and the description after, another the other way round, one has a discount column and another doesn't.

Inside a PDF there is no such thing as a table, there are pieces of text dropped at a point on the page, and the rows and columns are something we see when we look at it

Each supplier, though, is consistent

The job is possible because every supplier prints its invoices the same way every time, since they all come out of the same program.

So you don't try to write a universal reader that understands any invoice in the world. You learn the layout of that one supplier, where it keeps the columns, how the heading is built, how it marks discounts, where the table ends and the footer begins.

Done once, that layout covers every invoice from that supplier, until they change the form.

This is also where the practical advice comes from. You start with the handful of suppliers who send you most of the documents, not with the one who writes to you twice a year.

You find the column by its heading, not by its position

The fragile way says, the quantity is the third column from the left. It works perfectly until the supplier adds a column, and from that day the system writes into the quantity field whatever it finds in the third column, which is now the price.

The way that holds up says, look for the heading that reads Qty and take what sits underneath it, and if you can't find that heading, stop instead of guessing.

It's the same logic that applies to Excel files and CSVs, and the reasoning in full is in The file arrives every month with the columns moved

The lines have to add up to the total

Once you have read all the lines you add them up, and that sum has to match the net amount printed on the invoice, which you had already read earlier, from the easy spot.

If it matches, you have proof that you didn't lose a line, that you didn't count one twice, that you didn't read 1.250,00 where it said 1,250, and that the page break didn't eat anything.

If it doesn't match, something went wrong, and the system stops and puts that document in a queue for a person. It doesn't try to sort it out on its own.

Say, just to show the arithmetic, that an invoice has three lines of 100, 250 and 40 euros, numbers made up here to show the method. The sum is 390. If the net amount on the invoice is 390 the document goes through. If it is 430, either a line of 40 was read twice or there was a fourth one you didn't see, and in both cases nothing gets recorded.

Stopping is the right behavior

An automation that reads documents is judged by what it does when it doesn't understand, not by how it behaves when everything runs smoothly.

A system built properly, faced with an invoice that doesn't add up, puts its hand up and leaves it there. An invoice that wasn't recorded you record yourself and that's the end of it, while an invoice recorded wrong comes back at you much later, with a credit note and a reconciliation that won't balance.

Anyone who tells you their PDF reader never gets anything wrong is selling you the sentence, not the product.

When the PDF isn't even needed

Before setting out to read the tables in a PDF, ask yourself the laziest question. Does that document already exist in a tidy form.

If the supplier also sends you the XML of the e-invoice, the lines are already in there, each one in its own field, and reading them is a completely different sport from interpreting a printout.

If instead the PDF is a scan, it's a photograph and it has to be interpreted before you can even think about columns, and the checks around it count for even more. That's covered in Why the computer sometimes misreads a document

What we need to tell whether your case can be done

To say anything serious about a job like this, all it takes is the suppliers who send you the most documents, a few of their invoices as examples with the lines inside, and the screen where that data ends up.

The documents tell you the rest, better than any meeting. If you want to start there, write to us from the Contact page

What people usually ask us

Because there is only one total and it always sits in the same spot on the page, while the line items are a variable number, the descriptions wrap onto the next line, the table breaks across pages, and every supplier arranges the columns its own way
No, and it would be the wrong road, because every supplier always prints with the same layout, so you learn that one layout once and it covers all of their invoices, and it is worth starting with the handful of suppliers who send you most of the documents
You add up the lines you read and the sum has to match the net amount printed on the invoice, which is a figure already read from the easy spot, if the two numbers agree you have arithmetic proof on that document, if they do not agree something went wrong
The document stops and goes into a queue for a person, because an automation built properly would rather stop than guess, an invoice left there gets sorted out by a person when they look at it, while an invoice recorded wrong turns up much later and brings a credit note with it
No, an automation that reads invoices notices that the layout from that supplier is no longer the same and stops instead of writing data at random, then the layout has to be put right, and adjustments like these fall under maintenance according to the plan agreed in the project contract
No, if the supplier also sends the XML of the e-invoice the lines are already in there with defined fields, and reading them from there is far more reliable than interpreting a printout, the PDF stays the way in for documents that arrive only like that

Show us what you still do by hand

Describe the process or send a short screen recording and we'll tell you what can be automated

WA