Convert Markdown documents with Mermaid diagrams into professional PDF output.
This project is optimized for engineering documents rather than slide decks:
- Mermaid diagrams render to inline SVG for crisp PDF output.
- Markdown headings become PDF bookmarks.
- Relative Markdown assets keep working after preprocessing.
- Chinese and mixed CJK documents render with sensible defaults.
- Chrome/Chromium is auto-detected at runtime.
Generic Markdown-to-PDF tools are often good enough until a document contains a mix of Mermaid, local assets, CJK text, and heading-heavy structure.
This tool wraps md-to-pdf and Mermaid CLI with a few reliability features that
are especially useful for internal docs, RFCs, design notes, and printable
technical specifications.
- Converts
.mdto PDF with Chromium-based rendering - Renders Mermaid code fences, including indented fences inside list items
- Batch-renders Mermaid diagrams in a single browser session
- Uses conservative Mermaid batches of 4 diagrams by default to avoid resource spikes
- Reuses cached Mermaid SVGs across runs for much faster repeated exports
- Supports YAML front matter for PDF, browser, Mermaid, and wrapper settings
- Supports light and dark PDF themes (
--theme dark) - Inlines Mermaid SVGs to preserve vector quality
- Preserves relative Markdown and HTML asset references
- Adds page numbers to every page
- Generates PDF bookmarks from Markdown headings
- Clears PDF
/Titlemetadata so PDF viewers show the filename - Falls back gracefully when
pypdfis unavailable
scripts/md_to_pdf.py: main conversion entry pointtemplates/technical.css: PDF stylesheettemplates/mermaid-config.json: Mermaid theme and font settingspuppeteer-config.json: Chromium launch defaultstests/test_md_to_pdf.py: regression tests
- Python
3.10+ - Node.js
18+ npm- Python packages from
requirements.txt - A working Chrome or Chromium installation, or a CI step that installs one
The script also respects these environment variables:
PUPPETEER_EXECUTABLE_PATHCHROME_PATHGOOGLE_CHROME_BIN
cd <repo-root>
npm ci
pip install -r requirements.txtFor tests and bookmark assertions:
pip install -r requirements-test.txtrequirements.txt contains the runtime parser dependencies used for AST-based
Markdown processing and YAML front matter support. requirements-test.txt
adds pypdf, which is optional for conversion but required for the test suite
and bookmark assertions.
python3 scripts/md_to_pdf.py input.md
python3 scripts/md_to_pdf.py input.md output.pdf
python3 scripts/md_to_pdf.py input.md output.pdf --img-dir build/mermaid --max-size 12
python3 scripts/md_to_pdf.py input.md output.pdf --performance-mode
python3 scripts/md_to_pdf.py input.md output.pdf --theme darkThe wrapper accepts upstream-style md-to-pdf options at the top level and its
own wrapper-specific options under md_to_pdf:
---
stylesheet:
- ./print-overrides.css
pdf_options:
format: Letter
margin:
top: 16mm
launch_options:
args:
- --lang=en-US
md_to_pdf:
img_dir: build/mermaid
max_size: 12
mermaid_config: ./templates/mermaid.json
puppeteer_config: ./puppeteer.json
theme: dark
---Supported wrapper keys:
md_to_pdf.img_dirmd_to_pdf.max_sizemd_to_pdf.mermaid_configmd_to_pdf.puppeteer_configmd_to_pdf.performance_mode(trueenables full-throttle Mermaid batching)md_to_pdf.cache_dir(falsedisables persistent Mermaid caching)md_to_pdf.theme(lightby default,darkfor dark-mode PDF output)md_to_pdf.stylesheetormd_to_pdf.stylesheets
Default output goes to:
md-to-pdf/<stem>/<stem>.pdf
Rendered Mermaid SVG files are written to:
md-to-pdf/<stem>/mermaid/
# Example

1. Request flow
```mermaid
graph TD
Client --> Gateway
Gateway --> Service
```The generated PDF keeps the local image, renders the indented Mermaid block, and
creates a bookmark for Example.
Run the regression suite:
python3 -m unittest discover -s tests -vThe suite covers:
- nested Mermaid fences
- relative asset preservation
- front matter settings and output paths
- browser executable detection precedence
GitHub Actions is configured in .github/workflows/ci.yml.
The workflow:
- installs Node.js and Python
- installs Chrome for Testing
- runs
npm ci - installs Python test dependencies
- runs the unit and end-to-end regression suite
Compared with general-purpose tools such as md-to-pdf or marp-cli, this
project is narrower but stronger for printable engineering documents that mix
Mermaid, local assets, and heading-driven navigation.