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

  1. Convert each PDF with the page reader. For long reports set max_chars up to 200,000 (the default is 50,000).
  2. Check truncated in the answer. If it is true, raise max_chars or split the PDF before converting.
  3. 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.
  4. 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

Chunking options compared

MethodGood forWeak spot
Fixed-size charactersQuick tests, any textCuts tables and sentences in half
Recursive splitter (LangChain)A safe default for plain textIgnores the document outline
Markdown header splitter (LangChain, LlamaIndex)Documents with real headingsNeeds good Markdown as input
Page reader + the code abovePDFs, 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