Skip to main content

Convert Markdown to PDF with Mermaid diagram rendering and emoji support

Project description

md2pdf-mermaid

PyPI version Python versions License

Professional Markdown to PDF converter with emoji support and automatic Mermaid diagram rendering

Convert your Markdown documentation to beautiful PDFs with:

  • โœ… Full emoji support ๐ŸŽ‰ (colored, native rendering)
  • โœ… Automatic Mermaid diagram rendering (high-quality PNG images)
  • โœ… Table of contents generation (with [TOC] marker)
  • โœ… Page numbering (automatic footer pagination)
  • โœ… Professional formatting (headers, tables, code blocks)
  • โœ… Multiple page sizes (A4, A3, Letter)
  • โœ… Portrait & Landscape orientation support
  • โœ… Full Unicode/UTF-8 support (multilingual text, special characters)
  • โœ… Syntax highlighting for code blocks
  • โœ… CLI and Python API
  • โœ… Fast and reliable

๐Ÿš€ Quick Start

Installation

# From PyPI (recommended)
pip install md2pdf-mermaid

# Install Playwright browser (required for rendering)
playwright install chromium

Alternative - From Git repository:

pip install git+https://github.com/rbutinar/md2pdf-mermaid.git
playwright install chromium

macOS Users: Use a virtual environment (required for externally-managed Python):

python3 -m venv venv
source venv/bin/activate
pip install md2pdf-mermaid
playwright install chromium

See MACOS_SETUP.md for detailed instructions.

WSL Users: Use Windows Python to avoid dependency issues:

python3.exe -m venv venv  # Windows venv, no sudo needed!
venv/Scripts/pip.exe install md2pdf-mermaid
venv/Scripts/playwright.exe install chromium

See WSL_SETUP.md for details.

Command Line Usage

# Basic conversion (emoji and Mermaid just work!)
md2pdf document.md

# Custom output name
md2pdf document.md -o report.pdf

# Custom title
md2pdf document.md --title "Project Report"

# Page size and orientation
md2pdf document.md --page-size letter --orientation landscape

# High-quality Mermaid diagrams with custom theme
md2pdf document.md --mermaid-scale 3 --mermaid-theme forest

# Use legacy ReportLab engine (faster, no emoji)
md2pdf document.md --engine reportlab

# Disable Mermaid rendering (faster, text-only diagrams)
md2pdf document.md --no-mermaid

# Combine options
md2pdf document.md -o report.pdf --page-size a3 --title "Report"

Python API Usage

from md2pdf import convert_markdown_to_pdf_html

# Read markdown file
with open("document.md", "r") as f:
    markdown_content = f.read()

# Basic conversion (emoji supported!)
result = convert_markdown_to_pdf_html(
    markdown_content,
    "output.pdf",
    title="My Document"
)

# With custom page settings
result = convert_markdown_to_pdf_html(
    markdown_content,
    "report.pdf",
    title="Annual Report",
    page_size="A4",                # Options: 'A4', 'A3', 'Letter'
    orientation="portrait",         # Options: 'portrait', 'landscape'
    enable_mermaid=True            # Enable/disable Mermaid rendering
)

# Legacy ReportLab engine (for advanced PDF features)
from md2pdf import convert_markdown_to_pdf

convert_markdown_to_pdf(
    markdown_content,
    "output.pdf",
    page_numbers=True,
    font_name="arial",
    emoji_strategy="remove"  # ReportLab doesn't support emoji
)

๐Ÿ“‹ Features

๐ŸŽจ Two Rendering Engines

HTML Engine (Default) - Recommended โญ

  • Full emoji support ๐ŸŽ‰ - Colored emoji render natively
  • Modern CSS styling - Browser-quality rendering
  • Table of contents - Add [TOC] anywhere in your markdown
  • Page numbers - Automatic footer pagination (X / Y format)
  • Best quality - Superior rendering for modern documents

ReportLab Engine (Legacy) - For Special Cases

  • Custom fonts - TTF font support
  • Advanced PDF features - Custom headers/footers, bookmarks
  • Smaller file size - Slightly more compact
  • No emoji - Emoji are removed for compatibility
# Use HTML engine (default)
md2pdf document.md

# Use ReportLab engine
md2pdf document.md --engine reportlab --emoji-strategy remove

๐Ÿ“ Markdown Support

  • Headers (H1-H4) with colored borders
  • Bold, italic, inline code (including in lists!)
  • Emoji ๐ŸŽ‰ โœ… โŒ ๐Ÿ”ด ๐ŸŸข (full color support with HTML engine)
  • Bullet lists and numbered lists with full formatting support
  • Table of contents - Just add [TOC] in your markdown!
  • Tables with colored headers
  • Code blocks with syntax highlighting
  • Horizontal rules
  • Mermaid diagrams (rendered as high-quality images)
  • Full Unicode/UTF-8 support (multilingual text, special characters)

๐Ÿ“Š Mermaid Diagrams

Automatically renders Mermaid diagrams as high-quality PNG images:

```mermaid
graph LR
    A[Start] --> B[Process]
    B --> C[End]
    style A fill:#90EE90
    style C fill:#FFD700
```

Becomes a visual diagram in the PDF!

Mermaid Features:

  • Customizable themes: default, neutral, dark, forest, base
  • Quality control: Scale factor 1-4 for resolution (default: 2 = high quality)
  • Automatic sizing: Diagrams fit properly on pages
  • Full-width diagrams: Optimized for readability

๐Ÿ“„ PDF Styling

  • Multiple page sizes: A4, A3, Letter
  • Portrait and Landscape orientation support
  • Automatic page numbering in footer (HTML engine)
  • Table of contents support (HTML engine)
  • Professional color scheme
  • Tables with alternating row colors
  • Code blocks with light gray background
  • Headers with colored borders
  • 2cm margins on all sides
  • Full emoji rendering (HTML engine)

๐Ÿ”ง Requirements

  • Python 3.8+
  • Chromium browser (via Playwright, ~250 MB)
  • playwright, reportlab, markdown, pillow (installed automatically)

๐Ÿ“š Use Cases

1. Technical Documentation with Emoji

Convert your modern documentation to PDF:

# API Documentation ๐Ÿš€

## Quick Start โœ…

Install the package and get started! ๐ŸŽ‰

### Features
- โœ… Easy to use
- โšก Fast performance
- ๐Ÿ”’ Secure by default

2. Architecture Diagrams

Create professional reports with:

  • Data flow diagrams
  • System architecture
  • Process flowcharts
  • Executive summaries

3. CI/CD Integration

Automatically generate PDFs in your pipeline:

# GitHub Actions example
- name: Generate PDF Documentation
  run: |
    pip install md2pdf-mermaid
    playwright install chromium
    md2pdf README.md -o docs/README.pdf

๐Ÿ’ก Advanced Usage

Table of Contents

Add a table of contents anywhere in your markdown:

# My Document

[TOC]

## Chapter 1
Content here...

## Chapter 2
More content...

The [TOC] marker will be replaced with a clickable table of contents!

Batch Conversion

from md2pdf import convert_markdown_to_pdf_html
from pathlib import Path

# Convert all markdown files
for md_file in Path("docs").glob("*.md"):
    pdf_file = md_file.with_suffix(".pdf")

    with open(md_file, "r") as f:
        content = f.read()

    convert_markdown_to_pdf_html(content, str(pdf_file), title=md_file.stem)
    print(f"โœ“ Created {pdf_file}")

Engine Selection

# HTML engine (default) - emoji support
from md2pdf import convert_markdown_to_pdf_html

result = convert_markdown_to_pdf_html(
    markdown_text,
    "output.pdf",
    enable_mermaid=True
)
# result['emoji_support'] == True

# ReportLab engine - advanced PDF features
from md2pdf import convert_markdown_to_pdf

convert_markdown_to_pdf(
    markdown_text,
    "output.pdf",
    page_numbers=True,
    font_name="arial",
    emoji_strategy="remove"
)

๐Ÿ› Troubleshooting

"Playwright not found" Error

# Install Playwright
pip install playwright

# Install Chromium browser
playwright install chromium

"Permission denied" When Overwriting PDF

The PDF file is open in a viewer. Close it first, then try again.

Mermaid Diagrams Not Rendering

  1. Check Playwright is installed: python -c "import playwright"
  2. Check Chromium is installed: playwright install --force chromium
  3. Try disabling and re-enabling Mermaid: md2pdf file.md --no-mermaid

Emoji Not Rendering

If you're using the ReportLab engine, emoji are not supported. Switch to HTML engine:

md2pdf document.md --engine html  # or just: md2pdf document.md (HTML is default)

macOS: "externally-managed-environment" Error

macOS uses externally-managed Python. Always use a virtual environment:

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate

# Install package
pip install md2pdf-mermaid
playwright install chromium

# Use the package (with venv activated)
md2pdf document.md

Note: Remember to activate the virtual environment (source venv/bin/activate) before using md2pdf.

WSL: "Host system is missing dependencies to run browsers"

Best solution: Use Windows Python instead of Linux Python (no sudo needed):

# Create Windows venv from WSL
python3.exe -m venv venv
venv/Scripts/pip.exe install md2pdf-mermaid
venv/Scripts/playwright.exe install chromium
venv/Scripts/md2pdf.exe document.md  # Works perfectly!

See WSL_SETUP.md for complete instructions.


๐Ÿค Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“ License

MIT License - see LICENSE file for details


๐Ÿ”— Related Projects


๐Ÿ“ง Support


๐ŸŽฏ Roadmap

  • Full emoji support (v1.4.0)
  • Table of contents generation (v1.4.0)
  • Page numbering (v1.4.0)
  • Custom CSS themes
  • Image embedding from URLs
  • Header/footer customization
  • Multi-column layouts
  • PDF metadata (author, keywords, etc.)

Version: 1.4.2 Published: 2025-10-31 PyPI: https://pypi.org/project/md2pdf-mermaid/ Status: Stable Release ๐ŸŽ‰


๐Ÿ†• What's New

v1.4.2 (2025-10-31) - macOS Support & Dependency Fix

Bug Fixes:

  • ๐Ÿ› Fixed missing dependency - Added markdown>=3.0.0 to all dependency configurations
    • Resolves ModuleNotFoundError: No module named 'markdown' on fresh installations
    • Now automatically installed with the package

Documentation:

  • ๐Ÿ“– macOS Setup Guide - Comprehensive MACOS_SETUP.md documentation
    • Virtual environment setup instructions for macOS users
    • Troubleshooting for "externally-managed-environment" error
    • Tips for aliases and global installation
    • Tested on macOS 11-15 (Big Sur through Sequoia)
    • Apple Silicon (M1/M2/M3/M4) and Intel support confirmed

Improvements:

  • โœ… Better installation experience on macOS
  • โœ… Updated README with macOS-specific instructions
  • โœ… All features fully tested on macOS environment

v1.4.1 (2025-10-30)

  • ๐Ÿ› Fixed Mermaid diagram rendering timing issues
  • โšก Performance improvement: ~18% faster rendering

v1.4.0 (2025-10-23) - Emoji Support! ๐ŸŽ‰

Major New Features:

  • ๐ŸŽ‰ Full emoji support - Colored emoji render natively via HTML/Chromium engine
  • ๐Ÿ“‘ Table of contents - Add [TOC] anywhere in your markdown for automatic TOC
  • ๐Ÿ“„ Page numbering - Automatic footer pagination (X / Y format)
  • ๐ŸŽจ HTML rendering engine - Superior quality, modern CSS styling (now default)
  • โšก Dual engine architecture - HTML (default) or ReportLab (legacy) engines

Technical Improvements:

  • โœ… Mermaid diagrams automatically sized to fit pages
  • โœ… Better diagram quality with controlled height limits
  • โœ… Professional page layout with proper margins
  • โœ… Modern CSS styling for all elements
  • โœ… Browser-grade rendering quality

How to Use:

# Emoji now work by default!
md2pdf document.md

# Table of contents
echo "[TOC]" > doc.md
md2pdf doc.md

# Legacy engine (if needed)
md2pdf document.md --engine reportlab

Breaking Changes:

  • None! Default engine changed to HTML, but ReportLab still available with --engine reportlab

v1.3.1 (2025-10-21)

  • ๐Ÿ› Fixed emoji removal for technical symbols (โ†’, โ†, โœ“, etc.)
  • ๐Ÿ”ง Improved emoji detection accuracy

v1.3.0 (2025-10-21)

  • โœจ Mermaid theme support - 5 color themes available
  • โœจ Mermaid quality control - Scale factor 1-4 for resolution
  • โœจ Automatic emoji removal (for PDF compatibility with ReportLab)
  • ๐Ÿ› Fixed Mermaid diagram sizing issues
  • ๐Ÿ› Fixed hyperlink rendering

v1.2.0 (2025-10-20)

  • โœจ Full Unicode/UTF-8 support
  • โœจ Customizable fonts (Arial, DejaVu, Helvetica, custom TTF)
  • โœจ Bold & inline code in lists
  • ๐Ÿ› Fixed encoding issues

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md2pdf_mermaid-1.4.3.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md2pdf_mermaid-1.4.3-py3-none-any.whl (30.5 kB view details)

Uploaded Python 3

File details

Details for the file md2pdf_mermaid-1.4.3.tar.gz.

File metadata

  • Download URL: md2pdf_mermaid-1.4.3.tar.gz
  • Upload date:
  • Size: 34.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for md2pdf_mermaid-1.4.3.tar.gz
Algorithm Hash digest
SHA256 48ffc0d4a0f59eb1fb44ebb85fd6267527de059e9a5db18043bb68fbbdcc71e3
MD5 e2517c499f9b529b59e2e8fd96a81083
BLAKE2b-256 aa02f2a7be4c003846bb3261f33ebf5e3db1ecc33e6e493eadc437f59b7555d4

See more details on using hashes here.

File details

Details for the file md2pdf_mermaid-1.4.3-py3-none-any.whl.

File metadata

  • Download URL: md2pdf_mermaid-1.4.3-py3-none-any.whl
  • Upload date:
  • Size: 30.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for md2pdf_mermaid-1.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c8f6e9db98062b33f475b0681a31ab0c4d2220a45baeb07802b79abbe3990ce7
MD5 a97075d9746b8f979757b7021f5daa67
BLAKE2b-256 745b6fa7df6fe689fa8a89998a429bdd352424e65d6cb71b18153dd1d79cd534

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page