𝗦𝘁𝗼𝗽 𝗣𝗮𝗿𝘀𝗶𝗻𝗴 𝗣𝗗𝗙𝘀 𝗮𝘁 𝗥𝗲𝗻𝗱𝗲𝗿 𝗧𝗶𝗺𝗲
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:
- Read the operator stream instead of just text content.
- Build a CTM stack to track matrix state.
- Classify subpaths geometrically.
- Emit segments with provenance.
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.
Arrêtez de parser les PDF au moment du rendu : une meilleure architecture pour l'extraction structurée
L'analyse de PDF est notoirement difficile. Mais le faire au moment du rendu est une recette pour le désastre.
L'approche « au moment du rendu »
Dans de nombreuses implémentations actuelles, le flux ressemble à ceci :
- L'utilisateur demande un document.
- Le système récupère le PDF.
- Le système analyse le PDF (souvent en utilisant un LLM ou un OCR lourd).
- Le système affiche les données extraites.
Les problèmes
- Latence : Les utilisateurs attendent que l'analyse soit terminée.
- Coût : Chaque requête déclenche un processus d'extraction coûteux.
- Fiabilité : Si le parser échoue, toute la page échoue.
L'approche « prétraitement »
Au lieu de parser lorsque l'utilisateur le demande, parsez lorsque le document est ingéré.
La nouvelle architecture
- Ingestion : Le PDF est téléchargé et stocké.
- Extraction : Un worker asynchrone analyse le PDF.
- Stockage : Les données structurées sont sauvegardées dans une base de données.
- Consommation : L'interface utilisateur récupère directement les données structurées.
Conclusion
En découplant l'extraction du rendu, vous obtenez une application plus rapide, moins coûteuse et plus robuste.