Kezhan Shi’s post on Towards Data Science starts with a contract table that PyMuPDF reads back as “Renewal fee 500 Setup fee 200”. Every cell was extracted, in reading order, and the column structure that made those numbers mean anything is gone. What struck me was how quietly it fails. The same parser returns empty strings for scanned pages, so a scanned amendment glued to the end of the document never gets read, and nothing raises an error. PDF parsing for RAG usually breaks at this layer, long before retrieval or the model.
What PDF parsing for RAG loses
The source counts four blind spots in fitz, PyMuPDF’s module, and they match what I run into on document-heavy builds. Tables come back as flat words, and when the y-coordinates of adjacent rows run close, cells from the row below mix into the row above. Text inside figures, chart axis labels, diagram labels and stamps is invisible, because fitz returns the image bbox and the raw bytes and never looks at the pixels. Scanned pages return nothing. Captions are handled by regex, ^Figure \d+\b and ^Table \d+\b, which misses “Fig. 2” and wrongly flags a body sentence that starts with “Figure 2 shows”.
Downstream, the chunker gets a soup of words, the retriever matches against the soup, and the model guesses which number is the renewal fee. The fix in the article is Azure’s prebuilt-layout model. One call returns cell-level tables with rows, columns and a flagged header row, OCR for every page including scans, and the text inside figure regions. Paragraph roles like title, sectionHeading and figureCaption replace the regexes. Two caveats. The model to call is prebuilt-layout, not prebuilt-read, which is OCR only. And the supported inputs are PDFs and images, so an Office file in the media library needs converting first. Microsoft’s layout model reference lists what the current API version returns.
The contract makes the parser swappable
The design point I’d copy first is the contract: both parsers in the article return the same dict of relational tables, line_df, image_df, toc_df, object_registry and the rest, and downstream code reads rows without ever touching the PDF. Swapping the engine becomes a call-site decision instead of a rewrite. If you’re building this yourself, ending the parse in custom tables is what buys you that freedom.
Two details keep the swap honest. Every row carries a parsing_method column, “fitz” or “azure_layout”, so you get provenance for free: which pages cost money, which answers came from a page that needed the expensive pass. And Azure does not replace fitz. The layout model does not expose sub-line typography, so per-word bold and italic come back empty, and the TOC it reconstructs from paragraph roles is two levels deep at most, which is fine for stamping chunks and thin for navigation. For documents where spans matter, fitz stays in the stack.
One small thing I like is that checkboxes come back as selection marks, [x] and [ ], so a check-the-box field on a supplier form becomes queryable.
Running a 60 to 120 second parse inside WordPress
The numbers in the source are the part most RAG posts skip. Prebuilt-layout returns in 2 to 4 seconds per page, so a 30-page contract takes 60 to 120 seconds, while fitz parses the same document in under a second. PHP’s default max_execution_time is 30 seconds, and the browser usually gives up before the server does. So the dispatcher belongs in a queue, and the upload request only enqueues.
Action Scheduler, the queue library WooCommerce ships with, is enough for this. Schedule one action per document at upload, run the cheap fitz pass there, and escalate only the pages a pre-parse signal flags. I wrote about splitting parsing into a cheap pass and a heavy pass a while back; this is the same shape with a metered API at the heavy end.
add_action( 'bbioon_document_uploaded', 'bbioon_schedule_document_parse', 10, 1 );
function bbioon_schedule_document_parse( $attachment_id ) {
if ( ! function_exists( 'as_schedule_single_action' ) ) {
return; // Action Scheduler not loaded, retry on the next upload hook
}
if ( ! as_has_scheduled_action( 'bbioon_parse_document', array( $attachment_id ), 'pdf-parsing' ) ) {
as_schedule_single_action( time(), 'bbioon_parse_document', array( $attachment_id ), 'pdf-parsing' );
}
}
add_action( 'bbioon_parse_document', 'bbioon_parse_document', 10, 1 );
function bbioon_parse_document( $attachment_id ) {
$pages = bbioon_document_page_count( $attachment_id );
if ( $pages > 2000 ) {
// 2,000 pages per Azure call at most: split, one action per chunk
foreach ( bbioon_split_document( $attachment_id ) as $chunk_id ) {
as_schedule_single_action( time() + 60, 'bbioon_parse_document', array( $chunk_id ), 'pdf-parsing' );
}
return;
}
bbioon_run_cheap_pass( $attachment_id ); // fitz, under a second per doc
bbioon_escalate_flagged_pages( $attachment_id ); // prebuilt-layout, 2-4 s per page
}
Before the call, check the page count. The per-call limit is 500 MB or 2,000 pages, whichever comes first, so larger documents need splitting, with one action per chunk. Give the group a name so you can watch the backlog in the queue admin. And if documents arrive in bursts, drive the queue from a real server cron rather than WP-Cron alone.
If you’re wiring something like this, that’s work I take on: an upload path that parses cheap, escalates only flagged pages, and keeps a column you can audit when an answer is disputed. Send me the document types and the daily volume and I can tell you which pass they need.
What the Azure pass costs
Numbers from the source, meant as calibration rather than a quote. The prebuilt-layout tier runs around US$10 per 1,000 pages, so a 30-page contract costs roughly US$0.30. Route every page of every document through Azure and a thousand contracts a day becomes US$300 a day, which is what you pay for not having a dispatcher. The free tier, 500 pages per month, is enough for development.
Money is not the only gate. The service is a Microsoft cloud API under their online services terms, so client documents that cannot leave the building are out of scope. The article points to Docling as the local alternative that keeps the same table contract, and I’d take that trade for confidential material (I’ve read the technical report, not run it, so verify the table output on your own docs first).
Wire the escalation to signals rather than to a feeling. Three from the article are worth starting with: a page where fitz detects a table region but the extracted lines don’t look table-like, an image-heavy page with fewer than ten text rows, and a page whose text is mostly Unicode replacement characters and non-words, which means the existing OCR layer is bad. The cheap pass computes these, the queue decides, and parsing_method records what ran. I’d keep that column even on a project that never calls Azure, because when an answer is wrong the first question is which engine read that page.