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

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.

หยุดพาร์ส PDF ในช่วงเวลาเรนเดอร์: สถาปัตยกรรมที่ดีกว่าสำหรับการสกัดข้อมูลแบบมีโครงสร้าง

เมื่อคุณกำลังสร้างแอปพลิเคชันที่ต้องมีการประมวลผล PDF ข้อผิดพลาดที่พบบ่อยคือการพาร์ส PDF ในขณะที่จำเป็นต้องใช้เพื่อการเรนเดอร์หรือการสกัดข้อมูล วิธีการนี้แม้จะทำได้ง่าย แต่ก็นำไปสู่ปัญหาคอขวดด้านประสิทธิภาพที่สำคัญ

ปัญหาของการพาร์ส PDF ในช่วงเวลาเรนเดอร์ (The Problem with Render-time Parsing)

ในสถาปัตยกรรมแบบดั้งเดิม กระบวนการมักจะเป็นดังนี้:

  1. ผู้ใช้ส่งคำขอ (Request) เพื่อดูข้อมูลจาก PDF
  2. เซิร์ฟเวอร์รับคำขอ
  3. เซิร์ฟเวอร์เริ่มพาร์ส PDF เพื่อดึงข้อมูลหรือเตรียมการเรนเดอร์
  4. เซิร์ฟเวอร์ส่งข้อมูลกลับไปยังผู้ใช้

แม้ว่ากระบวนการนี้จะดูเรียบง่าย แต่