(Note · · 6 min read)

Extracting data from documents people actually send

Automatic document processing demonstrates beautifully on a clean sample and meets reality immediately: a photograph taken at an angle in poor light, a scan of a photocopy, a PDF that is an image of text. The difficulty is not the extraction model. It is everything around it.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Decide what kind of file you were given

A PDF may contain a real text layer, in which case extracting it directly is faster and more accurate than any image processing. Or it may be a scan wrapped in a PDF, where there is no text to extract at all.

Detect which before processing. Running recognition over a document that already has selectable text is slower and worse; attempting text extraction on a scan returns empty and looks like a corrupt file.

Preparation beats model choice

Correcting rotation, straightening a skewed scan, increasing contrast and removing speckle typically improves results more than changing recognition engine. The input is the variable with the widest range.

Keep the original file untouched, forever, and do all processing on derived copies. When a result is disputed months later, the original is the only thing that settles it — and a pipeline that overwrote its input cannot be re-run with a better model either.

Stage it, and make each stage resumable

Receive, classify, prepare, extract, validate, store. Separate stages with the intermediate result persisted at each one, so a failure in extraction does not mean re-uploading, and improving one stage does not mean re-running all of them.

This is background work, not request work: a large document takes longer than any reasonable request timeout. Accept the upload, return an identifier, process asynchronously and report progress — the same event-driven shape as any other long job.

Validate the extraction against what you know

Recognition produces plausible wrong answers: a zero read as an eight, a date in the wrong format, a decimal point in the wrong place. Check what you can — that a total equals the sum of its lines, that a date is in a sensible range, that an identifier matches its expected pattern and checksum.

Keep a confidence value with every extracted field. One number for the document hides the one field that was guessed, and that field is often the amount.

Design for the human reviewer

Full automation is the wrong target for documents that matter. Route low-confidence extractions to a person, showing the field beside the region of the image it came from so checking it takes a second rather than a minute.

Record every correction. That record is your measurement of accuracy, your training data if you later fine-tune, and the evidence for which document types are worth automating at all.

Treat uploaded files as hostile

Documents arrive from outside. Cap the size and page count, verify the type by content rather than by extension, and run processing tools in a restricted environment — document parsers are a long-standing source of vulnerabilities, and you are feeding them arbitrary input.

These documents also tend to be the most sensitive data you hold: identity documents, bank statements, payslips. Encrypt them at rest, restrict who can read them, log access, and keep extracted personal data out of logs and error reports — an exception handler that attaches the parsed content copies it somewhere with much weaker protection.

Common questions

Why does OCR work on samples and fail on real documents?

Because real files are photographed at an angle, scanned crooked, or are images of text inside a PDF. The extraction model is rarely the limiting factor — input preparation and handling of file variety are.

What improves OCR accuracy most?

Preparation: correcting rotation, straightening skew, increasing contrast and removing speckle usually beats changing recognition engine. Detect first whether the PDF already has a text layer, since extracting that directly is faster and more accurate.

Should document processing run in the request?

No. A large document exceeds any reasonable request timeout. Accept the upload, return an identifier and process asynchronously in resumable stages with intermediate results persisted, so one failure does not require re-uploading.

How should extraction errors be caught?

Validate against what you know — totals matching line sums, dates in range, identifiers matching a pattern and checksum — and keep a confidence value per field, not per document. Route low-confidence fields to a reviewer and record every correction as your accuracy measurement.

Need something like this built?