agentleFS
cy070812cy070812/md2pdf-skill/SKILL.md

md2pdf

Skill0 starsChanged 2 months agoMITView on GitHub

把 Markdown 转成中文排版良好的 A4 PDF。当用户要求把 .md 文件导出/转换成 PDF、需要批量把文档目录输出成 PDF、或需要打印版文档时使用。触发词:转PDF、导出PDF、markdown转pdf、md转pdf、生成PDF、打印版、批量转PDF。

  • Installs packages

9 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

Markdown → PDF

把 .md 转成适合打印的 A4 PDF。

核心原理:Markdown → 带打印样式的 HTML → 浏览器无头模式 --print-to-pdf。 借浏览器自带的中文字体与 CJK 断行能力,不需要装字体,不需要 GTK/Pango。这是本方案相对 WeasyPrint / LaTeX / reportlab 的关键优势——那几条路在 Windows 上要么装不上,要么配置成本极高。

依赖检查

python -c "import markdown" || pip install markdown

调用方式

# 目录批量:<目录>/*.md → <目录>/PDF/
python <此目录>/md2pdf.py "<md所在目录>"

# 单文件
python <此目录>/md2pdf.py "<文件.md>"

# 指定输出目录
python <此目录>/md2pdf.py "<输入>" -o "<输出目录>"

# 递归子目录 + 自定义主题 + 保留中间 HTML
python <此目录>/md2pdf.py "<输入>" -o "<输出>" -r -c theme.css -k

脚本会自行定位 Edge / Chrome,无需额外参数。转换成功时输出 ✓ 文件名 大小 纸张(mm),其中纸张应为 (209.9, 297.0),即标准 A4。

已验证的排版能力

  • 表格:斑马纹、首列加粗、跨页自动重复表头、长内容折行
  • 任务列表 - [ ] / - [x] → 渲染为空心方框
  • 引用块、代码块(长行软换行不裁切)、嵌套列表、YAML front matter(自动剥离)
  • 编码自动兜底:utf-8-sig → utf-8 → gb18030 → big5

改样式的方法

不要直接改 md2pdf.py 里的 DEFAULT_CSS。正确做法是写一个 CSS 文件用 -c 传入,它会叠加在默认样式之后,只需写要覆盖的部分:

@page { size: A4; margin: 20mm 18mm; }   /* 改页边距 */
body { font-family: "SimSun", serif; }    /* 换正文字体 */
th   { background: #15803d; }             /* 换主色 */
.table-wide { font-size: 8pt; }           /* 救超宽表格 */

参考 examples/custom.css(墨绿学术风)作为模板。

修改脚本时务必注意的坑

  1. 替换串的反斜杠陷阱:re.sub 的替换模板若写成 r"...\"cb\"...",raw string 中的 \" 会原样输出反斜杠,生成 class=\"cb\",导致样式静默失效、且不报任何错。必须写成 r'...' 配单引号,或 '"' 拼接。
  2. 中文路径要用 ASCII 中间名:Windows 下把中文路径传给浏览器命令行有编码风险,脚本先复制成 src_0001.html 再打印,最后改回中文名。不要"优化"掉这一步。
  3. nl2br 默认开启:Markdown 里连续多行引用块默认会合并成一行,开 nl2br 才保留换行。若文档在源码中手动折行了长段落,用 --no-nl2br。
  4. 浏览器落盘有延迟:打印命令返回后 PDF 不一定写完,必须轮询等待。直接 subprocess.run 后立刻读文件会拿到空文件。
  5. pre 必须 white-space: pre-wrap:打印没有横向滚动,长代码行会整行被裁掉。
  6. 无头参数要降级重试:新版 Chromium 用 --headless=new,旧版只认 --headless,脚本两种都试。

验证转换结果

PDF 通常超过 256 KB,无法用 Read 工具直接打开。要检查排版时,用 -k 保留 HTML 后截图:

msedge.exe --headless=new --disable-gpu --no-sandbox \
  --user-data-dir=<临时目录> --window-size=794,1400 \
  --screenshot=preview.png <生成的html路径>

A4 在 96dpi 下宽 794px,用这个宽度截图所见即所得;窗口高度调大可以一屏看更多内容。

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.