PDF to Markdown in Python
Four libraries do most of the PDF to Markdown work in Python: Docling, Marker, MarkItDown and PyMuPDF4LLM. We installed all four, ran the same five PDFs through each, and wrote down what came out. Here is working code for each, how fast they were, and how to choose.
The short answer
- Scanned PDFs: Marker. It was the only library that read our scanned report without a single error.
- Tables, permissive license: Docling (MIT). Accurate tables, but slow and flat headings.
- Fast text-PDF conversion for LLMs: PyMuPDF4LLM. Correct heading levels and tables, but AGPL-licensed.
- Many file types, not just PDF: MarkItDown. It's weak on PDFs specifically.
Docling
pip install doclingfrom pathlib import Path
from docling.document_converter import DocumentConverter
converter = DocumentConverter()
result = converter.convert("report.pdf")
markdown = result.document.export_to_markdown()
Path("report.md").write_text(markdown, encoding="utf-8")IBM's converter runs layout and table-structure models (PyTorch). In our test it got every table right, including the one in the scanned PDF, and kept lists and two-column reading order. Every heading came out as ##, bold, italic and links were dropped, and formulas were replaced with a formula-not-decoded comment. MIT license. For OCR switches, batch runs and offline models see our Docling PDF to Markdown guide.
Marker
pip install marker-pdffrom marker.converters.pdf import PdfConverter
from marker.models import create_model_dict
from marker.output import text_from_rendered
converter = PdfConverter(artifact_dict=create_model_dict())
rendered = converter("report.pdf")
markdown, _, images = text_from_rendered(rendered)Or from the command line, which writes out/report/report.md:
marker_single report.pdf --output_dir outMarker 2.2.1 runs its models with ONNX on the CPU, with no PyTorch needed (our environment was about 400 MB). Its OCR was the best in our test: the scanned report came back word for word. Tables and two-column order were right and the running header and footer were removed. Headings were all ##, the bulleted list was merged into one paragraph, links and emphasis were dropped, and code lines became headings.
License: code Apache 2.0; model weights under a modified OpenRAIL-M license, free for research, personal use and startups under $5M in funding or revenue. Larger companies need a commercial license.
MarkItDown
pip install 'markitdown[pdf]'from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("report.pdf")
print(result.text_content)Microsoft's MarkItDown converts Word, Excel, PowerPoint, HTML and more, and it was the fastest library here. For PDFs it mostly extracts plain text: no headings, bullets lost, two table columns merged into one, sections of the two-column paper out of order (1, 3, 2, 4, 6, 5), and an empty result for the scan. MIT license. There is also a CLI: markitdown report.pdf -o report.md.
PyMuPDF4LLM
pip install pymupdf4llmimport pathlib
import pymupdf4llm
markdown = pymupdf4llm.to_markdown("report.pdf")
pathlib.Path("report.md").write_bytes(markdown.encode())For RAG, ask for one chunk per page, with metadata:
import pymupdf4llm
pages = pymupdf4llm.to_markdown("report.pdf", page_chunks=True)
for page in pages:
print(page["metadata"]["page_number"], len(page["text"]))Built for LLM pipelines on top of PyMuPDF. It kept the right heading levels (wrapped in bold, as in ## **Revenue by region**), lists, emphasis and tables. Links became plain text and the two-column paper kept its running header and footer. It ran OCR on the scan and got the table shape, with misread numbers ("+12%" became "412%"). PyMuPDF is AGPL-3.0, or a commercial license from Artifex, which matters if you ship it in closed-source software or a hosted service.
Speed
Seconds per file on an Apple M1 laptop (CPU only), after the converter was created. "Setup" is creating the converter, which loads the models, done once per process.
| Library | Setup | Text guide (2 pp.) | Report with tables | Scanned page | Two-column paper (2 pp.) |
|---|---|---|---|---|---|
| MarkItDown 0.1.9 | 3.0 | 0.19 | 0.06 | 0.01* | 0.41 |
| PyMuPDF4LLM 2.2 | 1.0 | 1.69 | 0.37 | 2.67 | 0.50 |
| Marker 2.2.1 | 7.0 | 2.16 | 0.40 | 1.04 | 0.98 |
| Docling 2.155.0 | 8.1 | 22.69† | 2.46 | 5.24 | 1.86 |
* MarkItDown returned nothing for the scan, hence the near-zero time. † Docling's first file includes loading models it initializes lazily; later files are much faster.
Output quality
| Docling | Marker | MarkItDown | PyMuPDF4LLM | |
|---|---|---|---|---|
| Heading levels | All ## | All ## | None | Correct (bold-wrapped) |
| Bulleted list | Yes | Merged into paragraph | Lost | Yes |
| Bold / italic | No | No | No | Yes |
| Links | No | No | No | No |
| Tables | Correct | Correct | Columns merged | Correct |
| Two-column order | Correct | Correct | Wrong | Correct |
| Running header/footer | Removed | Removed | Kept | Kept |
| Scanned page | Table right, text garbled | Perfect | Empty | OCR errors |
| Formulas | Dropped | Plain text | Broken table | Plain text |
| License | MIT | Apache 2.0 + OpenRAIL-M weights | MIT | AGPL-3.0 / commercial |
No library kept the PDF's links. If you need them, or correct heading levels without bold wrappers, our browser converter kept both on the same files. It can't OCR scans, though. The websites we tested alongside these libraries are covered in best PDF to Markdown converters.
How to choose
- Are your PDFs scanned? Use Marker (check the license) or Docling with OCR on.
- Will you ship this in a product? Docling and MarkItDown are MIT. PyMuPDF4LLM is AGPL, and Marker's weights have revenue limits.
- Do you need heading levels for chunking? PyMuPDF4LLM keeps them; Docling and Marker flatten them to one level.
- Throughput? MarkItDown and PyMuPDF4LLM are fastest; Marker is close on text PDFs; Docling is the slowest.
Tested December 2026 with Python 3.12. Every code block on this page was run against our sample PDFs before publishing; download them from the test setup.
Last updated