Module 3 · Working with Your Own Data

AI for documents: text, OCR and measurement

Lesson 9 of 15 9 min lesson Updated on October 30, 2026
Progress in the course60%

In this lesson

A PDF can be text or a photo.

You will separate, page by page, what is already text from what needs to be read by a machine. Then you will see how to read, validate and measure the error before you trust the result.

Real text or a photo

A PDF can store text written as text, as in a Word document. When you copy that text, it comes out exact. Or it can store just a photo of the page, like a photocopy. Then the text has to be read from the image.

That reading is called OCR, optical character recognition. It works like a person reading a crumpled receipt: it gets almost everything right, but it can mistake one letter for another. OCR is an estimate. Text that already exists is the exact copy.

That is why the first decision is made page by page. Try to select a sentence with the mouse. If it selects, there is text, and the page goes straight through. Only the image page goes through OCR.

The assembly line

  1. Detect: is there text or just an image?
  2. Read: copy the text, or apply OCR to the image.
  3. Organize: recognize title, paragraph and table.
  4. Extract fields: pick out the CNPJ (the Brazilian company tax ID), date and amount and put them in a digital form (JSON).
  5. Validate: check each value with simple rules.
  6. Measure and route: count the errors and send anything doubtful to a person.

How OCR reads better

Tesseract is the most widely used free OCR engine. It depends on a good image. Its documentation recommends at least 300 DPI, and says it struggles with tables, which call for a layout tool, such as Docling. For Portuguese, set the language to por; without it, Tesseract uses English and the accents come out wrong.

The S looks like the 5, the O like the 0 and the l like the 1. Asking a chat to "fix" the numbers afterwards does not solve it: the chat only completes the text and can make up a nice-looking number that OCR never read.

What the pros know

In a numbers-only field, do not let OCR think it is reading words. The Tesseract documentation explains that turning off the dictionaries improves the reading when the text is not made of common words, and that the tessedit_char_whitelist variable limits the allowed characters, for example to digits only. Without that, "1S0" turns into a similar-looking word instead of a number. People who process invoices and boletos (Brazilian bank payment slips) configure each field the way it needs, instead of using a single setting for the whole page.

Validate before you trust

Each value goes through rules written by you, not by the model. The CNPJ has check digits, and a made-up number almost never passes. The sum of the items has to match the total, and the date has to be plausible.

The strongest rule: the total amount has to appear in the text that OCR read from the same page. A model can read R$ 1,540.00 where the page says R$ 1,450.00 and still deliver a perfect JSON. Only comparing with another reading reveals the error.

Measuring: the pipeline's yardstick

The standard measure for OCR is the character error rate, CER: how many letters were swapped, deleted or made up compared with the correct text. Transcribe 5 to 10 pages by hand, including the hard ones, and compare each configuration. The choice stops being an opinion and becomes a number.

For the fields, check on 10 to 30 documents how many came out exact and how many were made up. In a financial field, the tolerance for made-up values is zero.

Careful

Payslips, contracts and medical reports contain names, CPF numbers (the Brazilian individual taxpayer ID) and even health data. Sending them to a cloud API "just to test" already counts as processing personal data under the LGPD, Brazil's data protection law. Use made-up documents or local processing.

Practice · 5 min

  1. Take 10 of your PDFs and try to select a sentence in each one. Note how many have text and how many are images.
  2. Pick 3 image pages, run them through a text reader and count the wrong letters.
  3. Write, in three lines, the rule for your human queue: what kind of doubt sends the document to be checked.
Deliverable: a sheet with the count of text pages and image pages, the errors on the three pages and your human queue rule.

Summary

Sources

Read on October 30, 2026.

  1. Tesseract OCR. Improving the quality of the output: resolution, tables, dictionaries and character list.
  2. Docling. Docling documentation: page structure and tables.
  3. Brazil. Lei 13.709/2018 (LGPD).

Lesson quiz

Three situations to apply what you learned.

Lesson 9 of 15

Before moving on to the next lesson

Put what you saw into practice. The boxes stay checked in this browser, so you can come back later.

Have a question about this lesson? The IV Help team answers personally on WhatsApp.

Ask on WhatsApp