𝗦𝘁𝗼𝗽 𝗣𝗮𝗿𝘀𝗶𝗻𝗴 𝗣𝗗𝗙𝘀 𝗮𝘁 𝗥𝗲𝗻𝗱𝗲𝗿 𝗧𝗶𝗺𝗲
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.
PDF'leri render anında ayrıştırmayı bırakın: Yapılandırılmış veri çıkarma için daha iyi bir mimari
Bir PDF'den veri çıkarmanız gerektiğinde ilk içgüdünüz nedir?
Çoğu geliştirici şu deseni izler:
- Kullanıcı bir PDF yükler.
- Sistem, görüntüleme veya işleme isteği bekler.
- Sistem, PDF'i anlık olarak (on the fly) ayrıştırır.
- Sistem veriyi döndürür.
Bu, "render anında ayrıştırma"dır. Bu bir tuzaktır.
Render Anında Ayrıştırmanın Sorunları
Bir PDF'i ayrıştırmak hesaplama açısından maliyetlidir. Şunları içerir:
- Dosya yapısının okunması.
- Metin ve koordinatların çıkarılması.
- Karmaşık düzenlerin (tablolar, sütunlar, görseller) işlenmesi.
- OCR (eğer PDF taranmış bir belgeyse).
Bunu render anında yaptığınızda şu sorunları beraberinde getirirsiniz:
- Gecikme (Latency): Sistem belgeyi ayrıştırmakla uğraşırken kullanıcı beklemek zorunda kalır.
- Tutarsızlık: Ayrıştırma mantığı değişirse veya PDF biraz farklıysa, çıktı değişebilir.
- Ölçeklenebilirlik sorunları: Yoğun istek zamanlarında yüksek CPU/Bellek kullanımı.
Çözüm: Çıkarma Anında Ayrıştırma (Extraction-time Parsing)
Veriyi görmek istediğinizde ayrıştırmak yerine, veriyi aldığınızda ayrıştırın.
Daha İyi Bir Mimari
Sağlam bir mimari; veri alımı (ingestion), çıkarma (extraction) ve tüketim (consumption) sorumluluklarını birbirinden ayırır.
1. Veri Alımı Katmanı (Ingestion Layer)
PDF yüklenir ve bir nesne deposunda (S3 gibi) saklanır.
2. Çıkarma Pipeline'ı (Extraction Pipeline - Çekirdek)
Bu, yükleme işlemiyle tetiklenen asenkron bir süreçtir.
- A Adımı: Düzen Analizi (Layout Analysis): Metinlerin, tabloların ve görsellerin nerede olduğunu belirler.
- B Adımı: Metin/Tablo Çıkarımı: Ham içeriği almak için özelleşmiş araçlar kullanır.
- C Adımı: Yapılandırılmış Veri Çıkarımı: Önceden tanımlanmış bir şemaya (schema) dayanarak, ham metni yapılandırılmış bir formata (JSON) dönüştürmek için bir LLM veya kural tabanlı bir motor kullanır.
3. Depolama Katmanı (Storage Layer)
Yapılandırılmış JSON, bir veritabanında (PostgreSQL, MongoDB vb.) saklanır.
4. Tüketim Katmanı (Consumption Layer)
Kullanıcı verileri görmek istediğinde, sistem sadece veritabanına sorgu gönderir. Ayrıştırma gerekmez.
Özet
Ayrıştırma işlemini render anından veri alım anına taşıyarak, yavaş ve kırılgan bir süreci hızlı, güvenilir ve ölçeklenebilir bir sisteme dönüştürürsünüz.