Convert Markdown to PDF with Mermaid diagram rendering and emoji support
Project description
md2pdf-mermaid
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
- Check Playwright is installed:
python -c "import playwright" - Check Chromium is installed:
playwright install --force chromium - 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:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
๐ License
MIT License - see LICENSE file for details
๐ Related Projects
- mermaid - Diagram syntax
- reportlab - PDF generation
- playwright - Browser automation
- python-markdown - Markdown processing
๐ง Support
- Issues: https://github.com/rbutinar/md2pdf-mermaid/issues
- Documentation: https://github.com/rbutinar/md2pdf-mermaid
๐ฏ 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.0to all dependency configurations- Resolves
ModuleNotFoundError: No module named 'markdown'on fresh installations - Now automatically installed with the package
- Resolves
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48ffc0d4a0f59eb1fb44ebb85fd6267527de059e9a5db18043bb68fbbdcc71e3
|
|
| MD5 |
e2517c499f9b529b59e2e8fd96a81083
|
|
| BLAKE2b-256 |
aa02f2a7be4c003846bb3261f33ebf5e3db1ecc33e6e493eadc437f59b7555d4
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8f6e9db98062b33f475b0681a31ab0c4d2220a45baeb07802b79abbe3990ce7
|
|
| MD5 |
a97075d9746b8f979757b7021f5daa67
|
|
| BLAKE2b-256 |
745b6fa7df6fe689fa8a89998a429bdd352424e65d6cb71b18153dd1d79cd534
|