Updated
How to Convert PDF to Markdown for LLMs and RAG (2026 Guide)
Why Markdown and not plain text
An LLM reads tokens, not pages. Plain text loses the signals that tell the model what is a title, a list or a table, and a flattened table becomes a puzzle of numbers. Markdown keeps that structure for very few extra tokens: # for headings, | for table cells. It also gives natural split points for RAG chunks.
The four hard parts of a PDF
- Tables: a PDF only stores text at positions; the columns have to be rebuilt from ruling lines or alignment.
- Reading order: on two-column papers, naive extraction reads across both columns line by line.
- Noise: hyphenated line ends, page headers and footers, rotated margin stamps.
- Scans: pages that are only images need OCR, which is slower and less exact.
Option 1: a local library
For a few files on your own machine, open-source libraries do the job. A minimal example with pymupdf4llm:
import pymupdf4llm
md = pymupdf4llm.to_markdown("paper.pdf")
open("paper.md", "w").write(md)Other options include Docling, Marker and pdfplumber (good for tables). Check the licence of each library before using it in a commercial product, and plan for OCR separately if you have scans.
Option 2: a converter on Apify
When documents come as public URLs, in batches, or must be read by an AI agent, a hosted converter avoids installing anything. We measured two of them on the same 20 public PDFs (502 pages) on 1 December 2026:
| Plainprobe | memo23/pdf-text-extractor | |
|---|---|---|
| Documents converted | 20 / 20 | 20 / 20 |
| Tables returned as Markdown / JSON | 118 | 0 |
| Text recovered on a two-column paper (LLaMA-2) | 47.4k words | 42.6k words |
| Run time for 20 PDFs | 56 s | 14 s |
| Price for these 20 PDFs | $0.08 + start | $0.105 |
Same 20 public PDFs (research papers, government forms, manuals), run on Apify the same day with default settings.
Where others do better: memo23/pdf-text-extractor is about 4 times faster because it extracts plain text only, without table detection or layout analysis.
A plain text extractor is the faster choice when you only need the words; Plainprobe Document to Markdown is the one to use when tables, reading order and chunks matter.
Chunking for RAG
- Split on paragraph or heading boundaries, never in the middle of a sentence or table.
- Keep the heading path (for example 3 Methods > 3.2 Data) and the page range with each chunk: retrieval is better and answers can cite their page.
- Keep tables whole when possible, or repeat the header row in each piece.
- Start around a few hundred words per chunk and tune on your own questions.
Checklist before you index
- Spot-check one converted table against the original PDF.
- Look at the word count: a near-empty result often means a scanned PDF without OCR.
- Check the OCR confidence on scanned pages.
- Record the source URL and conversion date with each document.
FAQ
Why convert PDF to Markdown before sending it to an LLM?
Markdown keeps headings, lists and tables with very few extra tokens, so the model sees the structure of the document instead of a flat stream of lines; it is also easy to split into chunks for RAG.
How do I keep tables from a PDF?
Use a converter that detects tables and outputs them as Markdown tables or JSON records. Plain text extractors flatten tables into lines of words and lose the columns.
How do I handle scanned PDFs?
Scanned pages contain images, not text. They need OCR (for example Tesseract); check the OCR confidence and expect lower quality on poor scans and handwriting.
What chunk size should I use for RAG?
A common starting point is a few hundred words per chunk, split on paragraph or heading boundaries, keeping the heading path and page numbers with each chunk so answers can cite their source.