markdown-html-pdf

A powerful Python package to convert Markdown files to PDF with syntax highlighting and emoji support.

Features

Installation

uv add markdown-html-pdf

After installation, you need to install Playwright browsers:

uv run playwright install

Or use the built-in auto-installer on first run.

Usage

Command Line

# Basic conversion
uv run markdown-html-pdf input.md output.pdf

You can also use the shorter alias:

uv run md2pdf input.md output.pdf

Python API

import asyncio
from markdown_html_pdf import markdown_to_pdf

async def convert():
    await markdown_to_pdf(
        markdown_file_path="input.md",
        pdf_output_file_path="output.pdf"
    )

asyncio.run(convert())

Requirements

How it works

  1. Markdown to HTML: Converts Markdown to HTML using markdown-it-py with GitHub-flavored Markdown support
  2. Syntax Highlighting: Applies syntax highlighting to code blocks using Pygments
  3. HTML to PDF: Uses Playwright to render HTML to PDF with optimized settings for text fidelity

Supported Markdown Features

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.