← Back to Skills Marketplace
ronie-shi

Html To Pdf

by Ronie-shi · GitHub ↗ · v1.0.4 · MIT-0
cross-platform ✓ Security Clean
102
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install fanzhi-html-to-pdf
Description
用 Puppeteer + 已安装的 Chrome/Chromium 将 HTML 渲染为纯中文 PDF。自动处理 ECharts 等待、Tab 展开、淡入动画、内容测高、白边消除、防分页。适用:看板/报表/网页转 PDF、中文 PDF、交互图表转 PDF。
README (SKILL.md)

\x3C!-- 🦞 fanzhi-provenance -->

ContentID: fz:skill:2f8e9a7c:wsl_lobster:mpc2j3a6 Version: 1.0.3 | License: MIT-0 (ClawHub) Owner: 泛智生态 / Ronie & 泛智小龙虾 Provenance: wsl_lobster 初次生成,v1.0.4 ClawHub 发布前优化 Note: 本 Skill 依赖本机已安装的 Chromium,不含浏览器包

html-to-pdf — HTML 页面转 PDF

用法

# 基本(PDF 输出到 HTML 同目录)
node scripts/html-to-pdf.mjs report.html

# 指定输出路径
node scripts/html-to-pdf.mjs input.html /tmp/output.pdf

# 指定 Chromium 路径
CHROME_PATH=/usr/bin/chromium node scripts/html-to-pdf.mjs input.html

不适合此技能的场景

  • ❌ 需要 PDF 文本可选中/可搜索(PDF 内容是图片级的)
  • ❌ HTML 依赖跨域 CDN 资源且网络不通
  • ❌ 页面需要用户交互才能展示内容(如点击"加载更多")
  • ❌ 需要 A4 标准分页布局(如打印排版文档)

步骤

Step0: 渲染页面 + 等 JS 图表完成
Step1: 展开隐藏内容(.tc tab / .fi 淡入 / 折叠面板)
Step2: 测高度 + 底部 2px 实体 DOM(底色自动检测)
Step3: overflow:hidden 防 Chromium 额外分页 → PDF 生成
Step4: pdf-lib 验证页数

Step0 — 渲染

等待 networkidle0,检测 canvas(ECharts)全部完成后再等 2 秒确保动画。

Step1 — 展开

Headless 模式下 IntersectionObserver 不触发视口外元素。手动展开所有隐藏内容:

隐藏模式 CSS 修复手段
Tab 内容 .tc { display: none } .act class
淡入动画 .fi { opacity: 0; transform: translateY(...) } .sho class
折叠面板 display: none style.display = ''

Step2 — 白边消除

Chromium PDF 渲染器底层有白色画布。DOM 内容与页面高度有 1px 缝隙时画布透出。

修复:底部加 2px 实体 \x3Cdiv>,背景色自动检测(--bg CSS 变量 → computedStyle → 白色)。

Step3 — 防分页 + 生成

Chromium 对超长页面会切割出空白第二页。修复:生成前注入 overflow: hidden

Step4 — 验证

输出示例:

✅ report.pdf (1288.7 KB)
   PDF 页数: 1
   页1: 1050 x 3001 pt

预期 1 页,>1 页表示有内容未展开或分页策略需调整。

依赖

npm install puppeteer-core pdf-lib
依赖 用途
puppeteer-core 浏览器控制(不含 Chromium 下载)
pdf-lib PDF 页数验证

Chromium

脚本自动检测当前平台,也可通过环境变量覆盖:

# macOS:自动定位 /Applications/Google Chrome.app/...
# Linux/WSL:自动定位 /opt/chrome-linux/chrome
# 手动指定
CHROME_PATH=/usr/bin/chromium node scripts/html-to-pdf.mjs input.html

各平台路径备忘 → references/platforms.md

snap 版 Chromium 因沙箱隔离不可直接使用。

中文字体

本机需安装中文字体,否则 PDF 中文显示为方块。

方法 A:从 Windows 复制(WSL 环境)

mkdir -p ~/.fonts
cp "/mnt/c/Windows/Fonts/Noto Sans SC (TrueType).otf" ~/.fonts/
cp "/mnt/c/Windows/Fonts/msyh.ttc" ~/.fonts/
cp "/mnt/c/Windows/Fonts/seguiemj.ttf" ~/.fonts/
fc-cache -f -v ~/.fonts/

方法 B:直接下载 Noto Sans CJK

mkdir -p ~/.fonts && cd ~/.fonts
wget -O NotoSansSC.ttf "https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf"
wget -O NotoEmoji.ttf "https://github.com/googlefonts/noto-emoji/raw/main/fonts/NotoEmoji-Regular.ttf"
fc-cache -f -v ~/.fonts/

故障排查

现象 可能原因 修复
net::ERR_INVALID_URL 相对路径 传完整路径或用 $(pwd)/ 前缀
第二页空白 Chromium 内部分页 检查 overflow:hidden 注入
中文方块 缺少中文字体 执行"中文字体"节操作
emoji 方块 缺少 emoji 字体 安装 Noto Emoji / Segoe UI Emoji
底部 1px 白边 白色画布透出 确认 filler div 已注入且底色匹配
ECharts 空框架 渲染中改动了 body 样式 addStyleTag 而非直接改 DOM

首次验证

# 用任意本地 HTML 测试
node scripts/html-to-pdf.mjs path/to/your.html
# 或者生成一个简单测试文件
echo '\x3Ch1>你好世界 🦞\x3C/h1>' > /tmp/test.html
node scripts/html-to-pdf.mjs /tmp/test.html

安全性

  • 输入校验:路径含 \x3C>"|?* 等特殊字符 × 拒绝;仅允许 .html / .htm 文件
  • 输出安全:指定输出路径时,仅在 HOME / /tmp / 当前目录下允许写入
  • 异常安全:try/catch/finally 确保浏览器正确关闭,不泄露进程
  • ⚠️ --no-sandbox:单用户环境安全;多租户部署需移除

许可与来源

直接引用:puppeteer-core(Apache-2.0)+ pdf-lib(MIT),通过 npm import 调用 API,不修改上游代码。

方案参考:jsPDF / pdfmake / HTMX / Pico.css / mini.css(仅调研,未引用代码)。

完整许可声明 → references/sources.md

已知问题

  • CDN 资源(cdn.jsdelivr.net 等)需网络连通,可替换为本地
  • 无限滚动/懒加载不会自动触发,需在 Step1 中手动处理
Usage Guidance
This result is low confidence because the artifact files could not be read in the sandbox. Re-run the review when metadata.json and artifact/ are accessible before relying on this assessment.
Capability Assessment
Purpose & Capability
No artifact-backed purpose or capability concern could be identified from the available context.
Instruction Scope
No artifact-backed instruction-scope concern could be identified from the available context.
Install Mechanism
No artifact-backed install-mechanism concern could be identified from the available context.
Credentials
No artifact-backed environment or proportionality concern could be identified from the available context.
Persistence & Privilege
No artifact-backed persistence or privilege concern could be identified from the available context.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install fanzhi-html-to-pdf
  3. After installation, invoke the skill by name or use /fanzhi-html-to-pdf
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.4
- Updated documentation with detailed usage instructions, troubleshooting, and installation steps. - Clarified supported scenarios and known limitations (e.g., PDF is image-based, requires local Chromium, Chinese font dependency). - Added step-by-step explanation of the PDF rendering process and handling of hidden/tabbed content and animations. - Provided guidance for Chinese font setup and platform-specific Chromium configuration. - Outlined troubleshooting tips and known issues for easier debugging.
Metadata
Slug fanzhi-html-to-pdf
Version 1.0.4
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Html To Pdf?

用 Puppeteer + 已安装的 Chrome/Chromium 将 HTML 渲染为纯中文 PDF。自动处理 ECharts 等待、Tab 展开、淡入动画、内容测高、白边消除、防分页。适用:看板/报表/网页转 PDF、中文 PDF、交互图表转 PDF。 It is an AI Agent Skill for Claude Code / OpenClaw, with 102 downloads so far.

How do I install Html To Pdf?

Run "/install fanzhi-html-to-pdf" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Html To Pdf free?

Yes, Html To Pdf is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Html To Pdf support?

Html To Pdf is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Html To Pdf?

It is built and maintained by Ronie-shi (@ronie-shi); the current version is v1.0.4.

💬 Comments