file2markdown
pymupdf4llmpdfpythonmarkdownragdeveloper

PyMuPDF4LLM: What It Is and How to Use It for PDF-to-Markdown

September 16, 2026

PyMuPDF4LLM is a Python library that converts PDF documents into structured Markdown, purpose-built for feeding text into LLM and RAG pipelines. It's a thin, LLM-focused layer on top of PyMuPDF — the fast, low-level PDF engine that underpins a large chunk of the Python PDF ecosystem — and it has become one of the go-to choices for developers who need .pdf in and clean Markdown out with as little ceremony as possible.

If you don't want to manage a Python environment at all, file2markdown does the same job through a browser or a REST API — no install, no dependency updates. But if you're wiring conversion directly into a Python backend, here's what PyMuPDF4LLM actually does and how to use it.

PyMuPDF4LLM in 30 Seconds

  • What: Python library that converts PDFs to Markdown, JSON, or plain text
  • Built on: PyMuPDF (the MuPDF C engine, wrapped for Python)
  • Install: pip install pymupdf4llm
  • License: GNU AGPL-3.0 — see is PyMuPDF free for commercial use before shipping it
  • Best for: Fast, high-volume conversion of born-digital (non-scanned) PDFs for RAG and LLM ingestion

Installing and Running It

pip install pymupdf4llm

The basic API is a single function call:

import pymupdf4llm

md_text = pymupdf4llm.to_markdown("report.pdf")
print(md_text)

That's the whole thing for the default case: no GPU, no model weights to download, no configuration. pymupdf4llm reads the PDF's internal text layer directly and reconstructs headings, bold text, tables, and code-like blocks as Markdown. For a broader comparison of this approach against other Python PDF libraries, see the PDF to Markdown in Python guide.

Two other output formats are available if Markdown isn't what your pipeline needs:

data = pymupdf4llm.to_json("report.pdf")   # structured JSON
text = pymupdf4llm.to_text("report.pdf")   # plain text, no formatting

Page Chunking for RAG

The feature most relevant to RAG pipelines is page_chunks, which splits the output into one chunk per page instead of one long string:

chunks = pymupdf4llm.to_markdown("report.pdf", page_chunks=True)

for chunk in chunks:
    print(chunk["metadata"]["page_number"])
    print(chunk["text"])

Each chunk carries page-level metadata alongside the Markdown text, which is a convenient unit to hand to an embedding step. It's still a page-based split rather than a semantic one — for guidance on turning page chunks into retrieval-sized units, see chunking Markdown for vector databases and the fuller walkthrough in PDF to Markdown to chunks for RAG.

Table and Image Extraction

Tables are converted to standard GitHub-flavored Markdown pipe tables by default. If you need HTML instead:

md_text = pymupdf4llm.to_markdown("report.pdf", table_output="html")

Table reconstruction works from text positioning inside the PDF, so it holds up well on simple, single-column tables in born-digital documents. Merged cells, multi-column academic layouts, and headers that span several columns are where it starts to lose structure — a known limitation covered in more depth in PyMuPDF4LLM vs Docling, which benchmarks table accuracy between the two libraries directly.

To pull embedded images out alongside the text:

md_text = pymupdf4llm.to_markdown(
    "report.pdf",
    write_images=True,
    image_path="./images",
    image_format="png",
    dpi=150,
)

This writes each image to disk and inserts a Markdown image reference at the corresponding point in the text — useful for diagrams and figures you want to keep referenceable rather than discarded.

LlamaIndex Integration

If your pipeline is already built on LlamaIndex, PyMuPDF4LLM ships a reader that returns Document objects directly:

reader = pymupdf4llm.LlamaMarkdownReader()
docs = reader.load_data("report.pdf")

This skips the intermediate string-handling step and plugs straight into a LlamaIndex ingestion pipeline.

Scanned PDFs: OCR Is Built In, but It Needs an Engine

Current releases ship a hybrid OCR step. PyMuPDF4LLM analyses each page and runs OCR only on image-covered or illegible regions, leaving clean digital text alone. force_ocr=True makes it OCR everything, and ocr_function lets you plug in your own engine. The catch is that the text recognition itself is not bundled: it expects Tesseract (with its language data where PyMuPDF can find it) or a RapidOCR plugin on the machine. Without one, a scanned page comes back empty with a warning that no OCR text will be extracted, which is easy to mistake for a broken file. If setting up and maintaining Tesseract isn't something you want to own, file2markdown's scanned PDF converter runs OCR automatically, or see the scanned PDF to Markdown guide for a Python-native approach.

The License You Need to Check

PyMuPDF4LLM inherits its license from PyMuPDF: GNU AGPL-3.0. That's free to use if your own project is open-source under a compatible license, but it has real implications if you're shipping a closed-source app or running it inside a commercial SaaS product — AGPL's network-use clause means hosting it behind an API can obligate you to open-source the application around it. This trips up enough developers that it's worth reading in full before you build on it: see Is PyMuPDF free for commercial use? for what AGPL actually requires and when a commercial license from Artifex is the safer route.

When to Skip the Python Setup

PyMuPDF4LLM is a strong choice when you're already deep in a Python codebase and need fast, high-volume conversion of clean, born-digital PDFs. It's a worse fit when:

  • Your PDFs are scanned and you don't want to install and maintain Tesseract or RapidOCR alongside the library
  • Your tables are complex (merged cells, multi-column layouts) and accuracy matters more than speed
  • You're not in a Python stack, or don't want AGPL licensing exposure
  • You want conversion working in the next minute, not after a pip install and a licensing review

file2markdown handles all of that behind one interface — drag-and-drop in the browser, or a REST API callable from any language. It runs OCR automatically on scanned files, preserves complex table structure, and sidesteps the AGPL question entirely since you're calling a hosted service rather than shipping the library yourself.

Frequently Asked Questions

Is PyMuPDF4LLM free to use?

It's free to install and run under the GNU AGPL-3.0 license, but AGPL has conditions. If your product is closed-source or runs as a hosted SaaS, you likely need a commercial license from Artifex. See Is PyMuPDF free for commercial use? for the full breakdown.

Does PyMuPDF4LLM support OCR for scanned PDFs?

Yes, with a caveat. Recent versions detect scanned or image-only regions and OCR them automatically (or every page, with force_ocr=True), but the recognition is done by Tesseract or a RapidOCR plugin that you install separately. With no engine available, scanned pages return empty output plus a warning.

How is PyMuPDF4LLM different from PyMuPDF?

PyMuPDF is the underlying general-purpose PDF library (rendering, text extraction, manipulation). PyMuPDF4LLM is a focused extension on top of it that adds to_markdown(), page chunking, and structured output aimed specifically at LLM and RAG use cases.

What's the fastest way to convert a PDF to Markdown without installing Python?

Use the free PDF to Markdown converter at file2markdown.ai — upload a file and copy the Markdown output, no environment to set up. A REST API is also available for automated, cross-language workflows.


Ready to skip the Python setup? Try the free PDF to Markdown converter.

The Markdown Memo

A fortnightly note for lawyers, researchers, accountants, and anyone else drowning in PDFs, scans, and decks. No spam.