Create professional PDFs from Markdown with CJK font support, a theme system, and mandatory visual self-check. Scope: markdown → PDF only (for .docx use a different tool).
Quick start
uv run --with weasyprint scripts/md_to_pdf.py input.md output.pdf # default (formal, A4)
uv run --with weasyprint scripts/md_to_pdf.py input.md --theme warm-terra # training/course
uv run --with weasyprint scripts/md_to_pdf.py input.md --theme mobile # phone/WeChat reading
uv run --with weasyprint scripts/batch_convert.py *.md --theme warm-terra --no-preview
python scripts/md_to_pdf.py --list-themes dummy.md
Themes (themes/*.css)
default — A4, Songti+Heiti SC, black/grey → legal/contracts/formal.
cjk-auto — A4 → tables with uneven column content.
warm-terra — A4, PingFang SC, terracotta → course outlines/workshops.
mobile — 148×210mm, 15px, 1.9 line-height → phone reading.
Default rule: warm-terra for training, default for formal docs.
Backend (auto-selected by content)
- CJK content → Chrome (weasyprint subset-embeds PingFang SC as CID Type 0C OpenType, which macOS Preview/Adobe fail to render → garbled on recipient devices).
- Non-CJK → weasyprint (faster, no browser).
Override with
--backend chrome|weasyprint.
Anti-pattern: do NOT hand-run pandoc + Chrome
Manual pandoc → chrome --headless --print-to-pdf fails silently: no CJK CSS → boxes; default Chrome header/footer appears; no post-render check; no theme system. Always use this skill.
Visual self-check (MANDATORY)
After every run the script renders each page to PNG (via pdftoppm, into system temp dir) and prints Previews: <path>/page-NN.png. Read each page-NN.png and verify against the markdown source. Common silent failures: paragraphs collapsing (CommonMark soft-break), tables overflowing margins, missing CJK/emoji glyphs, code garbling, Chrome headers/footers. If wrong, fix the markdown (real - lists, blank lines, restructured tables) and rerun — the script does NOT silently fix non-standard markdown. Disable with --no-preview for batch. Requires pdftoppm (brew install poppler).
CJK typography (automatic, never edits user source)
- Layer 1 CSS patch:
table-layout: fixed, word-break: keep-all, th { white-space: nowrap } — fixes ~80% of CJK cell-wrap anti-patterns.
- Layer 2 lint: post-render
pdftotext -layout scan for single-char lines, broken bracket pairs, mid-thought punctuation orphans → warnings (not errors); author decides accept/shorten/restructure.
Troubleshooting
Chinese as boxes → install Songti/PingFang SC. weasyprint import error → use --backend chrome. Inline mixed CJK+ASCII blanks in Preview → font chain prioritizes CID TrueType (Songti/Heiti) before OpenType (PingFang). Table col-1 mid-break → default theme neutralizes pandoc's <colgroup> width hint.
1---2name: markdown-to-pdf-creator-cjk-aware3description: Use when converting Markdown to a professional PDF, generating printable/mobile-readable documents, or any markdown→PDF with proper Chinese font support — handles CJK typography and mandatory visual verification.4tags: [pdf, markdown, weasyprint, chrome, cjk, chinese-fonts, print, mobile, themes, convert]5source: daymade/claude-code-skills6derived_from: pdf-creator7metadata:8 source: https://skillmd.com/skills/aashutosh396/markdown-to-pdf-creator-cjk-aware9---1011Create professional PDFs from Markdown with CJK font support, a theme system, and mandatory visual self-check. Scope: **markdown → PDF only** (for .docx use a different tool).1213## Quick start14```bash15uv run --with weasyprint scripts/md_to_pdf.py input.md output.pdf # default (formal, A4)16uv run --with weasyprint scripts/md_to_pdf.py input.md --theme warm-terra # training/course17uv run --with weasyprint scripts/md_to_pdf.py input.md --theme mobile # phone/WeChat reading18uv run --with weasyprint scripts/batch_convert.py *.md --theme warm-terra --no-preview19python scripts/md_to_pdf.py --list-themes dummy.md20```2122## Themes (themes/*.css)23- `default` — A4, Songti+Heiti SC, black/grey → legal/contracts/formal.24- `cjk-auto` — A4 → tables with uneven column content.25- `warm-terra` — A4, PingFang SC, terracotta → course outlines/workshops.26- `mobile` — 148×210mm, 15px, 1.9 line-height → phone reading.27Default rule: warm-terra for training, default for formal docs.2829## Backend (auto-selected by content)30- **CJK content → Chrome** (weasyprint subset-embeds PingFang SC as CID Type 0C OpenType, which macOS Preview/Adobe fail to render → garbled on recipient devices).31- **Non-CJK → weasyprint** (faster, no browser).32Override with `--backend chrome|weasyprint`.3334## Anti-pattern: do NOT hand-run pandoc + Chrome35Manual `pandoc → chrome --headless --print-to-pdf` fails silently: no CJK CSS → boxes; default Chrome header/footer appears; no post-render check; no theme system. Always use this skill.3637## Visual self-check (MANDATORY)38After every run the script renders each page to PNG (via `pdftoppm`, into system temp dir) and prints `Previews: <path>/page-NN.png`. **Read each page-NN.png and verify against the markdown source.** Common silent failures: paragraphs collapsing (CommonMark soft-break), tables overflowing margins, missing CJK/emoji glyphs, code garbling, Chrome headers/footers. If wrong, fix the markdown (real `- ` lists, blank lines, restructured tables) and rerun — the script does NOT silently fix non-standard markdown. Disable with `--no-preview` for batch. Requires `pdftoppm` (`brew install poppler`).3940## CJK typography (automatic, never edits user source)41- Layer 1 CSS patch: `table-layout: fixed`, `word-break: keep-all`, `th { white-space: nowrap }` — fixes ~80% of CJK cell-wrap anti-patterns.42- Layer 2 lint: post-render `pdftotext -layout` scan for single-char lines, broken bracket pairs, mid-thought punctuation orphans → warnings (not errors); author decides accept/shorten/restructure.4344## Troubleshooting45Chinese as boxes → install Songti/PingFang SC. weasyprint import error → use `--backend chrome`. Inline mixed CJK+ASCII blanks in Preview → font chain prioritizes CID TrueType (Songti/Heiti) before OpenType (PingFang). Table col-1 mid-break → default theme neutralizes pandoc's `<colgroup>` width hint.