Agent-friendly Markdown-to-PDF CLI with HTML preview and JSON diagnostics
Project description
mdtopdf: Agent-friendly Markdown-to-PDF CLI
One command gives agents a controlled Markdown-to-PDF path.
Why it works for agents
Agents are good at writing Markdown. The problem is the handoff: PDFs exported
through ad hoc paths rarely share the same style. mdtopdf gives the user a
command-line interface where the style can be defined up front.
- Agent-friendly -
mdtopdf --helpis an interface description an agent can read. - JSON when it matters - conversion, HTML preview, environment checks, and theme listing can return machine-readable output.
- Local files in, local files out - no browser dependency, no upload step, no remote rendering service.
- More than plain Markdown - Obsidian links, highlights, frontmatter, comments, and callouts are rendered.
Quick start
Install from PyPI:
python -m pip install agent-markdown-pdf
The PyPI distribution is agent-markdown-pdf; it installs the mdtopdf command.
Do not use mdtopdf as the PyPI package name; the distribution name is
intentionally different from the command name.
| Use case | Name |
|---|---|
| Install from PyPI | agent-markdown-pdf |
| Run the CLI | mdtopdf |
| Import in Python | mdtopdf |
Check the machine:
mdtopdf doctor --json
Convert a file:
mdtopdf convert report.md -o report.pdf --overwrite
Try the bundled visual test document:
git clone https://github.com/ABClize/mdtopdf.git
cd mdtopdf
python -m pip install -e .[dev]
mdtopdf html examples/visual-test-en.md -o visual-test-en.html --overwrite
mdtopdf convert examples/visual-test-en.md -o visual-test-en.pdf --overwrite --json
The same visual test is also available in Chinese at
examples/visual-test-cn.md.
Agent workflow
The bundled agent skill lives at
mdtopdf/skills/SKILL.md.
Use that file when another agent needs a compact runtime guide for mdtopdf.
mdtopdf doctor --json
mdtopdf convert report.md -o report.pdf --overwrite --json
Use HTML preview when layout needs a quick look:
mdtopdf html report.md -o report.html --overwrite --json
mdtopdf convert report.md -o report.pdf --overwrite --json
convert --json returns the input path, output path, file size, theme, font
check summary, warnings, and render method. If conversion fails in JSON mode,
the error is structured enough for an agent to show the command, explain the
likely cause, and retry after a fix.
Visual output
The gallery below is rendered from the final PDF produced by
examples/visual-test-en.md. It shows the actual pages an agent can hand back
to a user: headings, callouts, tables, code, math, images, Mermaid, and
pagination.
| Page 1 | Page 2 |
|---|---|
| Page 3 | Page 4 |
| Page 5 | Page 6 |
How it works
Markdown -> markdown-it-py HTML -> theme/custom CSS -> WeasyPrint PDF
Mermaid rendering is optional. If a local mmdc command exists, Mermaid blocks
render to SVG. If it is missing, conversion still succeeds and Mermaid blocks
remain visible as highlighted code.
Features
| Feature | Notes |
|---|---|
| JSON output | --json is available for conversion, HTML preview, doctor, and theme listing. |
| Environment checks | doctor --json checks Python imports, native WeasyPrint libraries, Windows DLL paths, Mermaid availability, and recommended fonts. |
| Local rendering | Markdown, CSS, math, Mermaid SVG generation, and PDF export stay on the machine. |
| HTML preview | Generate standalone HTML before PDF export for fast visual inspection. |
| Obsidian compatibility | Wikilinks, aliases, frontmatter hiding, comments, highlights, and typed callouts. |
| Document Markdown | Tables, task lists, footnotes, heading anchors, fenced code, and Pygments highlighting. |
| KaTeX math | Inline and block TeX render with bundled KaTeX assets, without a CDN. |
| Safe HTML default | Common inline document tags are allowed; unsafe raw HTML stays escaped unless opted in. |
| Python API | Convert Markdown strings or files from your own code. |
Commands
Render a PDF:
mdtopdf convert report.md -o report.pdf
mdtopdf convert report.md -o report.pdf --overwrite
Preview HTML:
mdtopdf html report.md -o report.html --overwrite
Set document metadata and page chrome:
mdtopdf convert report.md -o report.pdf --title "Report"
mdtopdf convert report.md -o report.pdf --header "Report" --footer "Draft"
mdtopdf convert report.md -o report.pdf --no-header --no-footer
Use extra CSS or resource lookup paths:
mdtopdf convert report.md -o report.pdf --css print.css
mdtopdf convert report.md -o report.pdf --base-url assets
mdtopdf convert report.md -o report.pdf --resource-dir attachments
Custom CSS is the style extension point. Put document-specific rules in a CSS
file; fonts, spacing, colors, page rules, and code block styling all live there.
Use a system-installed font directly, or define @font-face for a local font
file. Relative URLs in CSS are resolved from the Markdown file's base URL, so
use --base-url when those assets live next to your document:
@font-face {
font-family: "Report Sans";
src: url("fonts/NotoSansSC-Regular.otf");
}
:root {
font-family: "Report Sans", "Noto Sans SC", "Source Han Sans SC", sans-serif;
}
code,
pre {
font-family: "Cascadia Code", "Liberation Mono", monospace;
}
Then pass the CSS file:
mdtopdf convert report.md -o report.pdf --css print.css --base-url .
During export, mdtopdf checks the final CSS font stacks. Missing fonts do not
stop PDF generation, but they are reported in CLI warnings and in the JSON
warnings field.
Return JSON:
mdtopdf --json convert report.md -o report.pdf --overwrite
mdtopdf doctor --json
mdtopdf themes list --json
Allow raw HTML only for trusted local Markdown:
mdtopdf convert trusted.md -o trusted.pdf --unsafe-html
Python API
from mdtopdf import (
markdown_file_to_html,
markdown_file_to_pdf,
markdown_to_html,
markdown_to_pdf,
)
rendered = markdown_to_html("# Report\n\n==highlight==")
print(rendered.html)
markdown_to_pdf("# Report\n\nBody", "report.pdf", title="Report", overwrite=True)
markdown_file_to_html("report.md", output_path="report.html", overwrite=True)
markdown_file_to_pdf("report.md", output_path="report.pdf", overwrite=True)
Markdown support
mdtopdf supports:
- CommonMark
- Tables
- Strikethrough
- Task lists
- Footnotes
- Heading anchors
- Fenced code blocks with Pygments highlighting
- Obsidian-style
==highlight==marks - Obsidian-style
[[target|alias]]wikilinks - Obsidian-style
%%comment%%comments outside code - Obsidian/YAML frontmatter hiding at the start of the file
- Obsidian-style callouts such as
> [!note] Title - Safe inline HTML tags such as
<br>,<kbd>,<mark>,<sup>, and<sub> - TeX math through
$inline$,$$block$$, and commonamsmathenvironments - Mermaid diagrams through local
mmdc, when installed
Raw HTML is disabled by default except for the safe subset above. For trusted
local Markdown, pass --unsafe-html.
Mermaid diagrams
Install a persistent local renderer:
npm install -g @mermaid-js/mermaid-cli
mdtopdf does not call Mermaid.ink and does not download Mermaid CLI through
npx during conversion. Run mdtopdf doctor --json to check whether Mermaid
rendering is available.
Platform notes
mdtopdf requires Python 3.10+ and installs its Python dependencies from PyPI:
click, markdown-it-py, mdit-py-plugins, pygments, latex2mathml,
matplotlib, mini-racer, and weasyprint.
WeasyPrint also needs native libraries such as Pango, GLib, and Cairo. Linux and macOS package managers usually provide them through system packages.
The default theme uses a PDFium-safe Latin-first font stack. Latin fonts are
listed before CJK fonts so ASCII digits, dates, versions, and page numbers are
not embedded into CJK font subsets that some Chrome/PDFium renderers handle
poorly. Chinese text still falls back to the CJK side of the stack, where
Microsoft YaHei is the first preferred CJK font. If it is missing, the theme
falls back to PingFang SC, Noto Sans SC, Noto Sans CJK SC,
Source Han Sans SC, and other available CJK fonts.
For Linux containers or agent sandboxes, install fontconfig plus the fonts you expect the default theme to use. A practical open-font baseline is:
sudo apt-get install -y --no-install-recommends \
fontconfig \
fonts-noto-cjk \
fonts-liberation \
fonts-dejavu-core
fc-cache -f
This baseline is a fallback. If you want Linux Chinese typography to match the
default Windows output more closely, provide Microsoft YaHei in the runtime
under your own font license. mdtopdf names that font in CSS but does not
bundle or download Microsoft font files. Providing Microsoft YaHei improves the
Chinese fallback; the Latin-first order is still kept for PDFium-safe digits.
Code blocks prefer Cascadia Mono / Cascadia Code, then Consolas,
Noto Sans Mono CJK SC, Liberation Mono, and DejaVu Sans Mono. On Debian,
fonts-cascadia-code provides Cascadia fonts; on other Linux images, provide
Cascadia Code yourself or let the theme fall back to the installed monospace
fonts.
Emoji are rendered through the system emoji font. The default theme prefers
Segoe UI Emoji for emoji spans on every platform, including Linux. Windows
usually provides it; Linux containers only get the same glyphs if the runtime
provides that font. If Segoe UI Emoji is not available, the theme falls back to
Apple Color Emoji, Noto Emoji, Noto Color Emoji, and other installed emoji
fonts. Whether the final PDF keeps color emoji still depends on WeasyPrint,
Pango/Cairo, and the PDF viewer.
Optional fallback fonts:
sudo apt-get install -y --no-install-recommends fonts-noto-color-emoji fonts-stix
fc-cache -f
On Windows, install the native libraries separately. A common MSYS2 setup is:
winget install MSYS2.MSYS2
Then install Pango from an MSYS2 MINGW64 shell:
pacman -S mingw-w64-x86_64-pango
Finally, point WeasyPrint at the DLL directory from PowerShell. Adjust the path if MSYS2 is installed somewhere else:
setx WEASYPRINT_DLL_DIRECTORIES "C:\msys64\mingw64\bin"
Run this after installation. The JSON output also reports whether recommended Latin, CJK, emoji, monospace, and math fallback fonts are present:
mdtopdf doctor --json
Development
git clone https://github.com/ABClize/mdtopdf.git
cd mdtopdf
python -m pip install -e .[dev]
python -m pytest tests/ -q
Build and check the package:
python -m build
python -m twine check dist/*
License
MIT. Bundled KaTeX assets are also distributed under the MIT license; see
mdtopdf/vendor/katex/LICENSE.
mdtopdf does not bundle CJK body fonts, emoji fonts, or proprietary system
fonts. The default theme references local system fonts such as Segoe UI,
Microsoft YaHei, PingFang SC, Segoe UI Emoji, Noto Emoji, Noto Color Emoji,
Cascadia Code, and Consolas, but those font files come from the user's operating
system or runtime environment.
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
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 agent_markdown_pdf-0.2.1.tar.gz.
File metadata
- Download URL: agent_markdown_pdf-0.2.1.tar.gz
- Upload date:
- Size: 1.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93709bdbb825956cfbb5db1429f036a91c9570cd34f6554901a55f9e92b35a7c
|
|
| MD5 |
19f929358db0184c3d18a2d921ae06e5
|
|
| BLAKE2b-256 |
375b19f065977d842324d08ff8f16957d384fc6c2400fd498b095fc82ce14df0
|
Provenance
The following attestation bundles were made for agent_markdown_pdf-0.2.1.tar.gz:
Publisher:
release.yml on ABClize/mdtopdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_markdown_pdf-0.2.1.tar.gz -
Subject digest:
93709bdbb825956cfbb5db1429f036a91c9570cd34f6554901a55f9e92b35a7c - Sigstore transparency entry: 1906671822
- Sigstore integration time:
-
Permalink:
ABClize/mdtopdf@165e8dcbdd8d375798056b34762fcaf429a759d3 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/ABClize
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@165e8dcbdd8d375798056b34762fcaf429a759d3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file agent_markdown_pdf-0.2.1-py3-none-any.whl.
File metadata
- Download URL: agent_markdown_pdf-0.2.1-py3-none-any.whl
- Upload date:
- Size: 1.0 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d55c8d03ab32fb0366b7b87fda3a3d1cf874c249c864b0f1314dca98a086366
|
|
| MD5 |
4b1c395398f0fe8e41bbb59bddf219c7
|
|
| BLAKE2b-256 |
0a2ef4eeb60af0a275e14023dffdf222293b5a1dbde0e36518806c58a9a5cd23
|
Provenance
The following attestation bundles were made for agent_markdown_pdf-0.2.1-py3-none-any.whl:
Publisher:
release.yml on ABClize/mdtopdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
agent_markdown_pdf-0.2.1-py3-none-any.whl -
Subject digest:
2d55c8d03ab32fb0366b7b87fda3a3d1cf874c249c864b0f1314dca98a086366 - Sigstore transparency entry: 1906672009
- Sigstore integration time:
-
Permalink:
ABClize/mdtopdf@165e8dcbdd8d375798056b34762fcaf429a759d3 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/ABClize
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@165e8dcbdd8d375798056b34762fcaf429a759d3 -
Trigger Event:
push
-
Statement type: