PDFs are not structured documents; they are instructions for where to place ink on a page. This fundamental disconnect means that converting a PDF to Markdown is always a guessing game, and most tools fail silently when it comes to RAG pipelines. A recent deep dive by developer Sid Mishra highlights exactly where standard text-layer extraction breaks down, offering a lightweight, standard-library-only Python script to patch the output before it hits your vector database.

The Anatomy of a Failed Conversion

Mishra tested the conversion of the seminal paper "Attention Is All You Need" using MeshArc’s scraping tool, reading only the text layer. While the tool processed the 15-page, 2.2MB PDF in about two seconds, the resulting 39KB Markdown file contained critical structural errors that would poison any retrieval system. The output lacked proper heading tags, treating "1 Introduction" as plain text, which breaks chunkers that rely on semantic boundaries. Worse, sentences were fragmented across multiple lines, and words like "transduction" were split into "transduc-" and "tion" due to line-break hyphenation, rendering them invisible to search queries.

A Standard Library Patch

Rather than relying on heavy layout models for every document, Mishra proposes a 30-line Python script that acts as a post-processing filter. The script uses regular expressions to identify numbered headings (e.g., "3.1 Encoder and Decoder Stacks") and converts them into proper Markdown headers. It also intelligently rejoins wrapped lines and removes trailing page numbers that often get stuck in the text stream. This approach preserves the integrity of prose documents without the computational cost of full layout analysis, making it ideal for policies, manuals, and contracts where text flow is linear.

When You Actually Need Layout Models

The script is not a silver bullet. Mishra is clear that this text-layer cleaning is insufficient for documents where structure carries the data, such as financial reports or rate cards. In these cases, a text-layer extractor returns cells in reading order without row or column context, destroying the table's meaning. For those scenarios, developers must use layout-aware tools like Docling or Marker. Additionally, scanned PDFs require OCR before any of this logic can apply, as they lack a selectable text layer entirely.

Key Takeaways

  • Text-layer extraction is fast and cheap but produces unstructured garbage that breaks RAG chunkers.
  • A simple regex-based Python script can restore headings and fix hyphenation for prose documents.
  • Layout models like Docling or Marker are mandatory for tables and complex financial documents.
  • Always include page numbers in your chunk metadata to ensure traceability for end-users.
  • For large-scale ingestion, use a crawler that tracks document changes to avoid re-embedding unchanged files.

The Bottom Line

Stop treating PDF extraction as a black box. For most RAG use cases involving prose, a lightweight post-processing script is more efficient and reliable than heavyweight layout models, but you must know when to bail out and use the right tool for tables.