Getting two-column reading order, ruled tables and CJK text right when converting PDFs to Markdown for RAG

Many bad RAG answers over PDFs start at extraction, not embeddings. I’ve been working on PDF to Markdown conversion and wanted to share what made the biggest difference, plus a small runnable example.

What breaks with plain text extraction

  • Two-column pages are read straight across, so sentences from both columns interleave.
  • Ruled tables flatten into a stream of numbers with no row/column structure.
  • Headings become ordinary lines, so you lose the natural chunk boundaries.
  • Justified Chinese/Japanese text often comes out with a space between every character.

What worked (pdfplumber + pdfminer.six, both MIT)

  1. Reading order: split the page into column regions recursively (XY-cut on whitespace gaps) before reading lines, instead of sorting words top-to-bottom.
  2. Headings: compare each line’s font size to the page’s body-text size and map larger sizes to #/##/###.
  3. Tables: use pdfplumber’s ruled-line table detection and emit Markdown tables.
  4. Paragraphs: re-join lines that were wrapped mid-sentence back into paragraphs.
  5. CJK: drop single spaces between two CJK characters.
  6. Quality check: flag pages whose text is mostly garbled glyphs (broken font encodings), and detect image-only pages instead of returning empty or junk text.

Then chunk on headings: each chunk keeps its section title, which helps retrieval a lot.

Limits of this approach: no OCR (it only reads the existing text layer), and borderless tables still come out as text.

Full write-up with a runnable script that builds a two-column sample PDF, shows the broken output, then the fixed Markdown and heading-based chunks:

Disclosure: I also maintain a hosted version of this as a paid Apify Actor (mentioned at the end of the article), but everything above works with the open-source libraries alone.

I’d like to hear what others use. Have Docling, Marker or pymupdf4llm handled multi-column and CJK documents well for you, and how do you deal with borderless tables?

I haven’t benchmarked Docling, Marker, or pymupdf4llm, so I can’t rank them. For a fair comparison, a small synthetic fixture set could pair each PDF with gold reading order and expected table cells: two-column English, mixed CJK/Latin, ruled tables, a borderless table with aligned columns, and image-only scans. Pin library versions and report per-page column swaps, CJK spacing errors, cell/row grouping, and empty output separately; link each failure to its source PDF and expected Markdown. That would make regressions reproducible without implying the current method handles borderless tables. I work on Orke, a desktop workspace for Claude Code and Codex, at BOMTOBE; I haven’t tried your converter.

Thanks for this!!

I’m pretty interested in these topics, as I’m building an app to make the AI interaction with documents easier (doconda). On the two gaps you flag: borderless tables are the case I see fail most often too, and I’d love to see real numbers comparing layout-model detectors vs rule-based extraction if anyone has them. For image-only pages, OCR is the only way through, and it’s worth running as a fallback only when pdfplumber finds no text layer, so you don’t slow down the clean PDFs.

If you’re keen on checking out some more tools, I would like if you could try with our tool doconda.com (using the free credits, no need to pay us). It should read any file, scanned PDFs included, into Markdown. Could be an interesting comparison point for your OCR gap!

Agree that most bad answers over PDFs start at extraction. Chunking on headings and keeping the section title on each chunk is the part I’d underline too. In Pixeltable we run document_splitter with separators=‘heading,token_limit’ on the cleaned Markdown, and new docs get chunked and embedded on insert without extra code. No clean answer for borderless tables on our side either.. rendering the page to an image and asking a vision model for the table as structured output is what I’d try next. Recipe here if useful: github.com/pixeltable/pixeltable/blob/main/docs/release/howto/cookbooks/text/doc-chunk-for-rag.ipynb

Thanks, this is a really useful way to frame it.

To be clear about where we stand: we haven’t run a cross-tool benchmark either, so I can’t rank Docling, Marker or pymupdf4llm. And you’re right that a test set shouldn’t imply our method handles borderless tables. The approach in the post relies on ruling lines, so tables that are only aligned by whitespace aren’t guaranteed to come out as tables. Often they end up as plain text lines.

Your fixture list covers exactly the failure modes from the post. We’re considering publishing a small synthetic test set along those lines:

  • generated PDFs (two-column English, mixed CJK/Latin with justified text, ruled tables, a borderless table with aligned columns, and an image-only page), each paired with gold reading order, expected table cells and expected Markdown;
  • pinned library versions, and per-page counts reported separately for column swaps, CJK spacing errors, cell/row grouping errors and empty output, rather than one combined score;
  • every failure linked back to its source PDF and expected output, so a regression can be reproduced from a single file.

Generating the PDFs ourselves keeps the ground truth exact and avoids licensing questions. The trade-off is that synthetic pages are cleaner than real scans, so the image-only case would mainly test “detect it and say so” rather than OCR quality. If we publish it, I’ll post the link here.

(This reply was written with AI assistance.)

One more for the ‘what breaks’ list: running headers, footers and page numbers. pdfplumber’s extract_text() returns them as ordinary lines at the top and bottom of each page, so they end up in the middle of chunks. And maybe a fixture for rotated table pages: on a Census report I tested, pdfplumber read a rotated table backwards (‘emocni’ for ‘income’).