Skip to content
Markdown to PDF

Markdown to PDF in Python: 3 Libraries Compared

Convert Markdown to PDF in Python with markdown-pdf, Python-Markdown + WeasyPrint or pypandoc — working code for each, what to install, and which to choose.

Updated · 6 min read

There is no single standard way to turn Markdown into a PDF from Python. The three approaches below cover most needs: one that installs with pip alone, one that gives you full CSS control, and one that uses Pandoc for LaTeX-quality typesetting.

Which one should you use?

Approach Install Styling Best for
markdown-pdf pip only Basic CSS Scripts and servers where you can’t install system packages
Python-Markdown + WeasyPrint pip + the Pango library Full CSS, including page rules Branded reports and invoices
pypandoc pip + a LaTeX distribution LaTeX templates Academic documents with heavy math

Option 1: markdown-pdf (pure pip)

markdown-pdf parses Markdown with markdown-it-py and writes the PDF with PyMuPDF, so there is nothing to install outside Python.

pip install markdown-pdf
from markdown_pdf import MarkdownPdf, Section

with open("notes.md", encoding="utf-8") as f:
    text = f.read()

pdf = MarkdownPdf(toc_level=2)       # headings up to level 2 go into the PDF outline
pdf.add_section(Section(text))
pdf.meta["title"] = "Notes"
pdf.save("notes.pdf")

Each Section starts on a new page, so you can build a document from several Markdown files by calling add_section once per file. Pass toc=False to a section (a cover page, say) to keep its headings out of the outline.

Option 2: Python-Markdown + WeasyPrint (full CSS)

This is a two-step pipeline: Python-Markdown converts Markdown to HTML, and WeasyPrint lays that HTML out as a PDF. Because the middle step is HTML, you style the result with ordinary CSS.

pip install markdown weasyprint
import markdown
from weasyprint import HTML, CSS

with open("report.md", encoding="utf-8") as f:
    body = markdown.markdown(f.read(), extensions=["tables", "fenced_code", "toc"])

css = CSS(string="""
    @page { size: A4; margin: 2cm; @bottom-right { content: "Page " counter(page) " of " counter(pages); } }
    body  { font-family: sans-serif; font-size: 11pt; line-height: 1.5; }
    table { border-collapse: collapse; }
    td, th { border: 1px solid #ccc; padding: 4px 8px; }
    pre   { background: #f6f8fa; padding: 8px; white-space: pre-wrap; }
""")

HTML(string=f"<html><body>{body}</body></html>").write_pdf("report.pdf", stylesheets=[css])

Two things to know:

  • HTML(string=...) needs the keyword — a bare string is treated as a file name.
  • WeasyPrint depends on the Pango text library. On macOS, brew install weasyprint takes care of it; on Linux, install your distribution’s Pango package; on Windows, the WeasyPrint docs walk through installing Pango with MSYS2.

Tables and fenced code blocks are extensions in Python-Markdown, which is why they are listed explicitly above. Without them, a pipe table comes out as plain text.

Option 3: pypandoc (Pandoc from Python)

pypandoc is a thin wrapper around Pandoc. pypandoc_binary bundles Pandoc itself, so you don’t have to install it separately:

pip install pypandoc_binary
import pypandoc

pypandoc.convert_file(
    "paper.md",
    "pdf",
    outputfile="paper.pdf",
    extra_args=["--toc", "-V", "geometry:margin=2cm", "--pdf-engine=xelatex"],
)

PDF output always needs outputfile, and Pandoc builds PDFs through LaTeX, so a LaTeX engine such as xelatex must be installed. In return you get properly typeset math written as $…$, numbered sections, citations and everything else Pandoc offers. The Pandoc guide covers the options you can pass in extra_args.

What none of them do out of the box

  • Mermaid diagrams. Mermaid is drawn by JavaScript in a browser. None of these libraries run JavaScript, so a mermaid code block stays a code block unless you render the diagram to an image first.
  • Math without LaTeX. Options 1 and 2 don’t typeset $…$ equations; only the Pandoc route does.
  • GitHub-style callouts such as > [!NOTE] come out as ordinary block quotes.

When you don’t need a script

If you’re converting a document by hand rather than as part of a program, a script is more work than the job deserves. The online Markdown to PDF converter renders math, Mermaid diagrams, callouts and highlighted code in the browser, with page numbers and a table of contents, and nothing to install. Use Python when the conversion has to run unattended — in a build, a web app or a batch job.

Try it on your own document

Free, private and right in your browser — no signup.

Open the editor