将 HTML 文件转换为高质量 PDF,保留 CSS 样式、支持中文、自动分页。
✅ 支持中文(UTF-8 编码)
✅ 保留 CSS 样式(颜色、字体、布局)
✅ 自动分页(避免表格/卡片被切断)
✅ A4 页面尺寸(可自定义)
✅ 打印优化(移除背景、优化字体)
✅ 支持本地文件和网络 URL
Playwright 是最可靠的方案,支持现代 CSS、JavaScript 渲染。
# 1. 安装 Python 包
pip3 install playwright
# 2. 安装 Chromium 浏览器(只需一次)
python3 -m playwright install chromium
优点:
缺点:
如果 Playwright 不可用,可以使用 weasyprint(纯 Python,无浏览器依赖)。
# 安装 weasyprint
pip3 install weasyprint
注意: weasyprint 需要系统库支持(libgobject、libpango 等),在 macOS 上可能需要:
brew install gobject-introspection pango
优点:
缺点:
技能提供了封装好的 Python 脚本,自动处理依赖检查和转换。
# 基本用法
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py input.html output.pdf
# 指定页面尺寸
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py input.html output.pdf --format A3
# 自定义边距
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py input.html output.pdf --margin-top 30mm
直接在对话中要求:"将 XXX.html 转换为 PDF",AI 会自动调用此技能。
在你的 Python 代码中:
from playwright.sync_api import sync_playwright
def html_to_pdf(html_path, pdf_path):
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
# 加载 HTML(支持本地文件和 URL)
page.goto(f"file://{os.path.abspath(html_path)}", wait_until="networkidle")
# 生成 PDF
page.pdf(
path=pdf_path,
format="A4",
print_background=True,
margin={
"top": "20mm",
"right": "15mm",
"bottom": "20mm",
"left": "15mm"
}
)
browser.close()
| 参数 | 默认值 | 说明 |
|---|---|---|
| ------ | -------- | ------ |
--format | A4 | 页面尺寸:A4, A3, Letter, Legal 等 |
--margin-top | 20mm | 上边距 |
--margin-bottom | 20mm | 下边距 |
--margin-left | 15mm | 左边距 |
--margin-right | 15mm | 右边距 |
--print-background | true | 是否打印背景色/背景图 |
--landscape | false | 是否横向打印 |
A: 确保 HTML 中指定了中文字体,例如:
body {
font-family: "PingFang SC", "Microsoft YaHei", "SimHei", sans-serif;
}
Playwright 会自动使用系统中的中文字体。
A: 在 CSS 中添加:
table, .card, .avoid-break {
page-break-inside: avoid;
}
A: 使用 @page 规则:
@page {
@top-center {
content: "年轻人陪游社交 APP 竞品分析报告";
font-size: 10pt;
color: #666;
}
@bottom-right {
content: "第 " counter(page) " 页";
}
}
A: 使用 Python 模块方式安装浏览器:
python3 -m playwright install chromium
而不是 playwright install chromium。
A: 如果 HTML 中有大量图片,可以尝试:
print_background: False(不打印背景图)--scale 参数缩小(例如 --scale 0.8)python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py \
~/Desktop/report.html \
~/Desktop/report.pdf
# 先下载 HTML
curl -o /tmp/page.html https://example.com
# 再转换为 PDF
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py \
/tmp/page.html \
~/Desktop/example.pdf
for html in ~/Desktop/*.html; do
pdf="${html%.html}.pdf"
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py "$html" "$pdf"
done
我们测试了多种 HTML-to-PDF 方案:
| 方案 | 中文支持 | CSS 支持 | 稳定性 | 速度 | 推荐度 |
|---|---|---|---|---|---|
| ------ | ---------- | ---------- | -------- | ------ | -------- |
| Playwright | ✅ 完美 | ✅ 完美 | ✅ 高 | ⚡ 中 | ⭐⭐⭐⭐⭐ |
| weasyprint | ✅ 完美 | 🟡 部分 | 🟡 中 | ⚡⚡ 快 | ⭐⭐⭐ |
| wkhtmltopdf | 🟡 需配置 | 🟡 部分 | 🟡 中 | ⚡⚡ 快 | ⭐⭐ |
| xhtml2pdf | 🟡 需配置 | ❌ 差 | ❌ 低 | ⚡⚡⚡ 最快 | ⭐ |
结论: Playwright 虽然需要下载浏览器,但渲染效果最好,最适合生成专业报告。
html-to-pdf
├── python3
├── pip3
└── playwright (Python 包)
└── chromium (浏览器, ~180MB)
OSError: cannot load library 'libgobject-2.0'原因: 系统缺少 GTK 库(weasyprint 需要)
解决: 使用 Playwright 方案,或安装依赖:
# macOS
brew install gobject-introspection pango
# Ubuntu/Debian
sudo apt-get install libpango-1.0-0 libgobject-2.0-0
TimeoutError: waiting for selector "body" failed原因: HTML 文件加载超时
解决: 增加超时时间,或检查 HTML 文件是否存在语法错误:
page.goto(url, wait_until="networkidle", timeout=60000) # 60秒
PermissionError: [Errno 13] Permission denied原因: 输出文件被占用或无写入权限
解决: 关闭 PDF 阅读器,或换一个输出路径。
MIT License
OpenClaw Skill Contributor
欢迎提交 Issue 和 Pull Request!
🎯 快速开始:
# 1. 安装依赖
pip3 install playwright
python3 -m playwright install chromium
# 2. 使用技能
python3 ~/.qclaw/skills/html-to-pdf/html_to_pdf.py input.html output.pdf
共 1 个版本