How to convert PDFs to Markdown for a RAG pipeline
Convert each PDF to Markdown first, then split the Markdown at its headings instead of every N characters. Each chunk keeps its section title and tables stay in one piece, so retrieval returns passages that make sense on their own. Our page reader returns that Markdown for $0.02 per PDF with no sign-up, and you can test it free 5 times a day.
Try it free Read Any Web Page, PDF or Doc — clean Markdown for AI agents · $0.02 / call
Why heading-based chunks retrieve better
Fixed-size chunking cuts wherever the character count runs out: in the middle of a table, a list or a sentence. A chunk that says "revenue rose 12%" without the section it came from is hard to match to a question and easy to misread.
Markdown gives you natural boundaries. Split at # and ## headings, and add the heading path (for example "Annual report > Risks > Currency") to the start of each chunk before you embed it. The embedding then carries the context as well as the sentence.
The pipeline in four steps
- Convert each PDF with the page reader. For long reports set max_chars up to 200,000 (the default is 50,000).
- Check truncated in the answer. If it is true, raise max_chars or split the PDF before converting.
- Split the Markdown at headings, then split any section that is still too long at blank lines. A Markdown table has no blank lines inside it, so it stays in one chunk.
- Store each chunk with the source url, title, language and heading path as metadata, then embed it.
Splitting the Markdown in Python
import re
def chunk_markdown(md, max_chars=2000):
chunks, path = [], []
for block in re.split(r"\n(?=#{1,3} )", md):
m = re.match(r"(#{1,3}) (.+)", block)
if m:
level = len(m.group(1))
path = path[:level - 1] + [m.group(2).strip()]
heading = " > ".join(path)
buf = ""
for para in block.split("\n\n"):
if buf and len(buf) + len(para) > max_chars:
chunks.append({"heading": heading, "text": buf.strip()})
buf = ""
buf += para + "\n\n"
if buf.strip():
chunks.append({"heading": heading, "text": buf.strip()})
return chunks
# doc = the "result" object from the paid page reader answer
for c in chunk_markdown(doc["markdown"]):
c.update(source=doc["url"], title=doc["title"], language=doc["language"])
# embed heading + text, keep the rest as metadata
What breaks RAG on PDFs
- Scanned PDFs have no text layer, so there is nothing to convert. Turn the pages into images and use an OCR API.
- Files over 10 MB are refused. Split them first; a refused file costs nothing.
- Multi-column layouts and footnotes can come out in an odd order with any converter. Read a few outputs before you embed thousands of files.
- Tiny chunks lose meaning and huge ones dilute it. Start around 1,000 to 2,000 characters and test with real questions.
Chunking options compared
| Method | Good for | Weak spot |
|---|---|---|
| Fixed-size characters | Quick tests, any text | Cuts tables and sentences in half |
| Recursive splitter (LangChain) | A safe default for plain text | Ignores the document outline |
| Markdown header splitter (LangChain, LlamaIndex) | Documents with real headings | Needs good Markdown as input |
| Page reader + the code above | PDFs, Word files and web pages in one pipeline | $0.02 per file, paid with an x402 wallet |
The product
Questions
What chunk size should I use?
There is no single right number. 1,000 to 2,000 characters per chunk, split at headings, is a good place to start; measure retrieval on your own questions and adjust.
Are tables kept?
Yes, they come back as Markdown tables. Keep each table in one chunk so the header row stays with its numbers.
Can I convert hundreds of PDFs?
Yes. Each successful read is $0.02, failed reads are free, and the limit is 120 calls per minute.
Related
Updated 2026-12-01
