Skip to content

Convert PDF to Markdown for RAG and LLMs

Language models work best with clean Markdown: real headings, lists, tables that keep their rows, no page headers in the middle of a sentence. ReadAnyDoc returns the document as typed blocks (title, section header, text, table, figure) and as Markdown.

pip install "readanydoc[engine]"
export ANTHROPIC_API_KEY=...
import readanydoc

result = readanydoc.parse("handbook.pdf")
markdown = result.markdown              # the whole document
for block in result.blocks:             # or block by block, with type and position
    print(block.type, block.bbox.page, block.content[:80])

Tips

  • Blocks keep their page and box, so a RAG answer can link back to the page it came from.
  • Headers and footers are typed as such and can be dropped before chunking.
  • From the command line: readanydoc parse handbook.pdf --markdown > handbook.md.

Next