𝗦𝘁𝗼𝗽 𝗣𝗮𝗿𝘀𝗶𝗻𝗴 𝗣𝗗𝗙𝘀 𝗮𝘁 𝗥𝗲𝗻𝗱𝗲𝗿 𝗧𝗶𝗺𝗲

Most developers build PDF extraction tools the wrong way.

They try to guess document structure from the visual output. They render a page to a canvas and look at pixel positions. They use computer vision to find columns or tables.

This approach is backwards.

A PDF already contains the structure you need in the operator stream.

A table is not just a set of pixels. It is a set of path operators like moveTo, lineTo, and rectangle. Zone boundaries are encoded in the CTM stack. You do not need to reconstruct what is already there.

Stop using visual heuristics. Use the source data.

I previously tried using De Casteljau subdivision for bounding boxes. I rejected it during testing.

De Casteljau is a subdivision algorithm. You split curves until the segments are small enough. This works for rendering, but it is bad for bounding boxes.

You have to choose a tolerance. If the tolerance is too loose, the box is wrong. If it is too tight, you waste resources on recursion. There is a better way. An analytical solution using the quadratic formula is exact. It does not recurse. It does not allocate segments.

The same logic applies to zone detection.

Many tools calculate zone boundaries by finding the midpoint between two text groups. This is a visual guess. It is not structural.

If you use midpoints, sub-pixel rounding will place regions in the wrong zones.

The fix is simple. Use the top edge of the bounding box. A region belongs to a zone based on where it starts. Use the actual Y-coordinate of the top edge.

Building a real PDF extractor is harder. You must:

This is more work than pixel-based guessing. But it produces deterministic results.

A pixel-based tool gives different results at 100% zoom than it does at 150% zoom. It is pattern-matching visual artifacts, not extracting structure.

If you do not parse the operator stream, you are building a demo. It might work on your test files, but it will fail on real user uploads.

The path through the operator stream is difficult. You must understand the fill and stroke state machines and the PDF specification. But you only have to learn it once. Then it works for every PDF.

Smetti di analizzare i PDF al momento del rendering: un'architettura migliore per l'estrazione strutturata

Analizzare i PDF è difficile. Farlo al momento del rendering è ancora più difficile.

Se hai mai costruito un'applicazione che deve estrarre dati dai PDF, probabilmente ti sei imbattuto nell'anti-pattern del "Parsing al momento del rendering".

In questo approccio, il PDF viene analizzato solo quando un utente richiede i dati. Questo di solito comporta:

L'anti-pattern del parsing al momento del rendering

Perché questo è un problema:

  1. Latenza: Il parsing e le chiamate agli LLM richiedono tempo. Gli utenti odiano aspettare.
  2. Costo: Eseguire costose chiamate agli LLM ogni volta che un utente visualizza un documento è uno spreco.
  3. Affidabilità: Il parsing dei PDF è notoriamente fragile. Una leggera modifica nel layout può rompere il tuo parser.

Un modo migliore: La pipeline di estrazione strutturata

Invece di eseguire il parsing al momento del rendering, dovremmo spostare il lavoro pesante in una pipeline di ingestione asincrona.

L'architettura

L'obiettivo è trasformare il PDF in un formato strutturato e leggibile dalle macchine (come JSON o Markdown) prima che sia mai necessario per la visualizzazione.

  1. Ingestione: Il PDF viene caricato e memorizzato in un object store (ad es. S3).
  2. Analisi del layout: Utilizza uno strumento (come Unstructured.io o Azure Document Intelligence) per