Guide · Python
Markdown to PDF in Python: three working approaches
Python can turn Markdown into a PDF with a pure-Python renderer, a Pandoc wrapper or a PDF library. Each approach makes a different trade-off between styling control and setup complexity.
Updated November 23, 2026 · Markdown2PDF by Hyperdyn
Pick the approach that matches the job
| Approach | Best for | Setup |
|---|---|---|
| WeasyPrint | HTML/CSS-styled documents and page rules. | Python packages plus native rendering libraries. |
| pypandoc | Reusing an existing Pandoc/LaTeX pipeline. | Pandoc and a PDF engine. |
| PyMuPDF | Simple Markdown files with a lightweight dependency. | Python package; check licensing. |
| Markdown2PDF | Manual writing, page preview and a finished PDF. | Browser and optional Google sign-in for download. |
WeasyPrint: Markdown → HTML → PDF
WeasyPrint renders HTML and CSS to PDF, including print-oriented page rules. Convert Markdown to HTML first, then let WeasyPrint paginate it:
pip install markdown weasyprintimport markdown
from weasyprint import HTML
with open("input.md", encoding="utf-8") as handle:
source = handle.read()
body = markdown.markdown(source, extensions=["tables", "fenced_code"])
html = (
"<!doctype html><html><head><meta charset='utf-8'>"
"<style>@page { size: A4; margin: 20mm; }"
"body { font-family: sans-serif; line-height: 1.5; }"
"pre { white-space: pre-wrap; }"
"h2 { break-after: avoid; }</style></head><body>"
+ body
+ "</body></html>"
)
HTML(string=html).write_pdf("output.pdf")WeasyPrint needs native libraries such as Pango and Cairo on some platforms. Follow the current installation guide for your operating system instead of assuming a pure wheel install is enough.
pypandoc: reuse a Pandoc installation
If Pandoc is already part of your build, pypandoc is a thin Python wrapper:
pip install pypandocimport pypandoc
pypandoc.convert_file(
"input.md",
"pdf",
outputfile="output.pdf",
extra_args=["--pdf-engine=xelatex"],
)This still requires Pandoc and the selected PDF engine to be installed. The pypandoc_binary package bundles the Pandoc executable, but it does not bundle a LaTeX distribution. The Pandoc guide covers the engine setup in more detail.
PyMuPDF: quick PDF output for simple Markdown
Current PyMuPDF builds can open a Markdown file and save it as a PDF:
import pymupdf
doc = pymupdf.open("input.md")
doc.save("output.pdf")Treat this as a lightweight path for straightforward Markdown. Check how your installed version handles headings, tables, code blocks and page size, and review the output. PyMuPDF is dual-licensed under AGPL or a commercial license.
Page CSS that prevents ugly PDFs
A PDF is paginated, so CSS needs print rules, not only screen rules. Useful defaults for a styled report include:
@page {
size: A4;
margin: 20mm;
}
h1, h2, h3 {
break-after: avoid;
}
table, pre, blockquote, figure {
break-inside: avoid;
}
img {
max-width: 100%;
height: auto;
}Long code lines still need a deliberate policy. Wrapping them can change meaning; allowing overflow can clip content. Test with the longest lines in your real document.
When a Python script is the wrong tool
Scripts are ideal for repeatable reports and batch jobs. They are less pleasant when a person wants to write Markdown, see the actual page breaks and adjust the design visually. That is the workflow Markdown2PDF is built for: the editor stays in the browser, the page preview matches the export and the PDF is generated locally after sign-in.
Frequently asked questions
Can I convert Markdown to PDF with pure Python?
WeasyPrint is the closest to a pure-Python route, but it still relies on native rendering libraries. pypandoc and Pandoc-based workflows require an external Pandoc and PDF engine.
Which Python library gives the best-looking PDF?
That depends on your design. WeasyPrint gives you HTML and CSS control, while Pandoc with a LaTeX template is strong for academic and technical documents. Test both with your real fonts and tables.
Does Markdown2PDF have a Python package?
No. Markdown2PDF is a browser application. The Python examples on this page use separate open-source libraries and are not an API offered by Markdown2PDF.