Home / Guides / How to Convert PDF to Markdown for LLMs and RAG

Updated

How to Convert PDF to Markdown for LLMs and RAG (2026 Guide)

To feed a PDF to an LLM or a RAG index, convert it to Markdown rather than plain text: headings, lists and tables survive, and the result is easy to split into chunks. Simple text extractors are fast but flatten tables and mix up two-column layouts; layout-aware converters keep tables as Markdown or JSON, read columns in order and run OCR on scanned pages. For a few files a local library is enough; for many public documents from URLs, a ready-made converter that returns JSON per document saves the plumbing.

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

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:

Plainprobememo23/pdf-text-extractor
Documents converted20 / 2020 / 20
Tables returned as Markdown / JSON1180
Text recovered on a two-column paper (LLaMA-2)47.4k words42.6k words
Run time for 20 PDFs56 s14 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

Checklist before you index

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.

Related tool