html-to-pdf
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHTML to PDF Converter
HTML转PDF转换器
First time? Ifabove, runsetup_complete: falsefirst, then set./SETUP.md.setup_complete: true
Pixel-perfect HTML to PDF conversion using Puppeteer (Chrome headless). Provides excellent support for Hebrew, Arabic, and other RTL languages with automatic direction detection.
首次使用? 如果上方的,请先运行setup_complete: false,然后将./SETUP.md设置为setup_complete。true
基于Puppeteer(无头Chrome)实现像素级精准的HTML转PDF转换。自动检测文本方向,完美支持希伯来语、阿拉伯语及其他RTL语言。
Why Puppeteer?
为什么选择Puppeteer?
- Pixel-perfect rendering: Uses actual Chrome engine
- Full CSS3/HTML5 support: Flexbox, Grid, custom fonts, backgrounds
- JavaScript execution: Renders dynamic content
- Automatic RTL detection: Detects Hebrew/Arabic and sets direction
- Web font support: Loads custom fonts properly
- 像素级精准渲染:使用真实Chrome引擎
- 完整支持CSS3/HTML5:Flexbox、Grid、自定义字体、背景
- JavaScript执行:渲染动态内容
- 自动RTL检测:识别希伯来语/阿拉伯语并设置文本方向
- Web字体支持:正确加载自定义字体
CRITICAL: Fit Content to Single Page
关键提示:将内容适配单页
Backgrounds on or cause extra pages! Put backgrounds on a container element instead:
htmlbodycss
@page { size: A4; margin: 0; }
html, body {
width: 210mm;
height: 297mm;
margin: 0;
padding: 0;
overflow: hidden;
/* NO background here! */
}
.container {
width: 100%;
height: 100%;
padding: 20mm;
box-sizing: border-box;
background: linear-gradient(...); /* Background goes HERE */
}Common causes of extra pages:
- Background on html/body - always put on instead
.container - Content overflow - use
overflow: hidden - Margins/padding pushing content out
Tips:
- Use if content still overflows
--scale=0.75 --margin=0 - For landscape: use
--landscape
htmlbodycss
@page { size: A4; margin: 0; }
html, body {
width: 210mm;
height: 297mm;
margin: 0;
padding: 0;
overflow: hidden;
/* 不要在这里设置背景! */
}
.container {
width: 100%;
height: 100%;
padding: 20mm;
box-sizing: border-box;
background: linear-gradient(...); /* 背景样式写在这里 */
}生成额外页面的常见原因:
- 在html/body上设置背景 - 务必放在元素上
.container - 内容溢出 - 使用
overflow: hidden - 外边距/内边距导致内容超出
小贴士:
- 如果内容仍然溢出,使用
--scale=0.75 --margin=0 - 如需横向排版:使用参数
--landscape
Setup (One-time)
首次设置(仅需一次)
Before first use, install dependencies:
bash
cd ~/.claude/skills/html-to-pdf && npm install首次使用前,请安装依赖:
bash
cd ~/.claude/skills/html-to-pdf && npm installQuick Usage
快速使用
Convert local HTML file:
转换本地HTML文件:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js input.html output.pdfbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js input.html output.pdfConvert URL to PDF:
转换URL为PDF:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js https://example.com page.pdfbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js https://example.com page.pdfHebrew document with forced RTL:
强制RTL的希伯来语文档:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js hebrew.html hebrew.pdf --rtlbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js hebrew.html hebrew.pdf --rtlPipe HTML content:
管道输入HTML内容:
bash
echo "<h1>שלום עולם</h1>" | node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js - output.pdf --rtlbash
echo "<h1>שלום עולם</h1>" | node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js - output.pdf --rtlOptions Reference
参数参考
| Option | Description | Default |
|---|---|---|
| Page format: A4, Letter, Legal, A3, A5 | A4 |
| Use landscape orientation | false |
| Set all margins (e.g., "20mm", "1in") | 20mm |
| Top margin | 20mm |
| Right margin | 20mm |
| Bottom margin | 20mm |
| Left margin | 20mm |
| Scale factor 0.1-2.0 | 1 |
| Print background graphics | true |
| Don't print backgrounds | - |
| Header HTML template | - |
| Footer HTML template | - |
| Wait time for fonts/JS | 1000 |
| Force RTL direction | auto-detect |
| 参数 | 描述 | 默认值 |
|---|---|---|
| 页面格式:A4、Letter、Legal、A3、A5 | A4 |
| 使用横向排版 | false |
| 设置所有边距(例如:"20mm"、"1in") | 20mm |
| 上边距 | 20mm |
| 右边距 | 20mm |
| 下边距 | 20mm |
| 左边距 | 20mm |
| 缩放系数 0.1-2.0 | 1 |
| 打印背景图形 | true |
| 不打印背景 | - |
| 页眉HTML模板 | - |
| 页脚HTML模板 | - |
| 等待字体/JS加载的时间 | 1000 |
| 强制RTL方向 | 自动检测 |
Examples
示例
Basic conversion:
基础转换:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js report.html report.pdfbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js report.html report.pdfLetter format with custom margins:
Letter格式自定义边距:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js doc.html doc.pdf --format=Letter --margin=1inbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js doc.html doc.pdf --format=Letter --margin=1inHebrew invoice:
希伯来语发票:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js invoice-he.html invoice.pdf --rtlbash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js invoice-he.html invoice.pdf --rtlLandscape presentation:
横向演示文稿:
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js slides.html slides.pdf --landscape --format=A4bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js slides.html slides.pdf --landscape --format=A4No margins (full bleed):
无边距(全 bleed):
bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js poster.html poster.pdf --margin=0bash
node ~/.claude/skills/html-to-pdf/scripts/html-to-pdf.js poster.html poster.pdf --margin=0Hebrew/RTL Best Practices
希伯来语/RTL最佳实践
For best Hebrew rendering in your HTML:
- Set lang attribute:
<html lang="he" dir="rtl"> - Use UTF-8:
<meta charset="UTF-8"> - CSS direction: Add to body
direction: rtl; text-align: right; - Fonts: Use web fonts that support Hebrew (Noto Sans Hebrew, Heebo, Assistant)
为了获得最佳的希伯来语渲染效果,你的HTML应遵循以下规范:
- 设置语言属性:
<html lang="he" dir="rtl"> - 使用UTF-8编码:
<meta charset="UTF-8"> - CSS方向设置:为body添加
direction: rtl; text-align: right; - 字体选择:使用支持希伯来语的Web字体(Noto Sans Hebrew、Heebo、Assistant)
Example Hebrew HTML structure (single-page):
单页希伯来语HTML结构示例:
html
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@400;700&display=swap" rel="stylesheet">
<style>
@page { size: A4; margin: 0; }
html, body {
width: 210mm;
height: 297mm;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
width: 100%;
height: 100%;
padding: 20mm;
box-sizing: border-box;
font-family: 'Heebo', sans-serif;
direction: rtl;
text-align: right;
background: #f5f5f5; /* Background on container, NOT body */
}
</style>
</head>
<body>
<div class="container">
<h1>שלום עולם</h1>
<p>זהו מסמך בעברית</p>
</div>
</body>
</html>html
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@400;700&display=swap" rel="stylesheet">
<style>
@page { size: A4; margin: 0; }
html, body {
width: 210mm;
height: 297mm;
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
width: 100%;
height: 100%;
padding: 20mm;
box-sizing: border-box;
font-family: 'Heebo', sans-serif;
direction: rtl;
text-align: right;
background: #f5f5f5; /* 背景样式放在容器上,而非body */
}
</style>
</head>
<body>
<div class="container">
<h1>שלום עולם</h1>
<p>זהו מסמך בעברית</p>
</div>
</body>
</html>Troubleshooting
故障排除
Fonts not rendering correctly
字体渲染异常
- Add for more font loading time
--wait=2000 - Ensure fonts are loaded via or Google Fonts
@font-face
- 添加参数增加字体加载时间
--wait=2000 - 确保字体通过或Google Fonts加载
@font-face
Hebrew appearing left-to-right
希伯来语显示为从左到右
- Use flag to force RTL direction
--rtl - Add to your HTML element
dir="rtl"
- 使用参数强制RTL方向
--rtl - 为HTML元素添加属性
dir="rtl"
Page breaks not working
分页不生效
Use CSS page-break properties:
css
.page-break { page-break-after: always; }
.no-break { page-break-inside: avoid; }使用CSS分页属性:
css
.page-break { page-break-after: always; }
.no-break { page-break-inside: avoid; }Backgrounds not showing
背景不显示
- Ensure is set (default is true)
--background - Use only if you want to exclude backgrounds
--no-background
- 确保已设置(默认开启)
--background - 仅当需要排除背景时使用
--no-background
Auto-Fit Content (MANDATORY VERIFICATION)
自动适配内容(必须验证)
CRITICAL - Claude MUST do this after EVERY PDF generation:
- Read the PDF file using the Read tool to visually inspect it
- Check for vertical overflow (empty pages, content spilling to next page)
- Check for horizontal overflow (text cut off at sides)
- If ANY issue found → FIX and regenerate (max 5 attempts)
- Only deliver PDF to user after verification passes
This is NOT optional. Never deliver a PDF without visual verification.
关键要求 - 每次生成PDF后,Claude必须执行以下步骤:
- 使用读取工具打开PDF文件,进行视觉检查
- 检查垂直溢出(空白页、内容溢出到下一页)
- 检查水平溢出(文本被边缘截断)
- 如果发现任何问题 → 修复并重新生成(最多5次尝试)
- 仅在验证通过后,才将PDF交付给用户
此步骤为必填项。绝不能在未进行视觉验证的情况下交付PDF。
Problems to Look For
需要排查的问题
| Problem | Symptom | Fix |
|---|---|---|
| Vertical overflow | Empty space at page bottom, content on next page | Reduce |
| Horizontal cut-off | Text cut at left/right edges | Reduce |
| Both issues | Content cut AND extra pages | Fix HTML CSS first, then adjust scale |
| 问题 | 症状 | 修复方案 |
|---|---|---|
| 垂直溢出 | 页面底部有空白,内容跑到下一页 | 减小 |
| 水平截断 | 文本在左右边缘被截断 | 减小 |
| 两种问题同时存在 | 内容被截断且存在空白页 | 先修复HTML/CSS,再调整缩放比例 |
Fix Strategy (Max 5 Attempts)
修复策略(最多5次尝试)
Attempt 1: Default settings
bash
node scripts/html-to-pdf.js input.html output.pdfAttempt 2: If vertical overflow → reduce scale
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.9Attempt 3: If horizontal cut-off → reduce margins
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.9 --margin=10mmAttempt 4: If still issues → smaller scale + margins
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.8 --margin=5mmAttempt 5: If still failing → FIX THE HTML CSS:
css
/* Add to HTML to prevent horizontal overflow */
.container {
width: 100%;
max-width: 100%;
overflow-wrap: break-word;
word-wrap: break-word;
box-sizing: border-box;
}STOP after 5 attempts - regenerate HTML with proper constraints.
尝试1:默认设置
bash
node scripts/html-to-pdf.js input.html output.pdf尝试2:如果存在垂直溢出 → 减小缩放比例
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.9尝试3:如果存在水平截断 → 减小边距
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.9 --margin=10mm尝试4:如果问题仍存在 → 进一步减小缩放比例和边距
bash
node scripts/html-to-pdf.js input.html output.pdf --scale=0.8 --margin=5mm尝试5:如果仍失败 → 修复HTML/CSS
css
/* 添加到HTML中以防止水平溢出 */
.container {
width: 100%;
max-width: 100%;
overflow-wrap: break-word;
word-wrap: break-word;
box-sizing: border-box;
}5次尝试后停止 - 重新生成符合约束条件的HTML。
HTML Width Fix (Required for Horizontal Fit)
HTML宽度修复(解决水平截断的必要步骤)
If content is cut at sides, the HTML MUST have:
css
html, body {
width: 210mm; /* A4 width */
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
width: 100%;
max-width: 100%;
padding: 15mm;
box-sizing: border-box;
overflow-wrap: break-word;
}如果内容在边缘被截断,HTML必须包含以下样式:
css
html, body {
width: 210mm; /* A4宽度 */
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
width: 100%;
max-width: 100%;
padding: 15mm;
box-sizing: border-box;
overflow-wrap: break-word;
}Verification Checklist
验证清单
After EVERY PDF generation, verify:
- All text visible (not cut at edges)
- No unnecessary empty pages
- Content fills pages properly
- No large gaps between sections
If ANY check fails → adjust and regenerate (max 5 times).
每次生成PDF后,需验证:
- 所有文本可见(未被边缘截断)
- 无多余空白页
- 内容合理填充页面
- 各部分之间无大段空白
如果任何一项未通过 → 调整并重新生成(最多5次)。
Technical Notes
技术说明
- Uses Puppeteer with Chrome headless for rendering
- Waits for to ensure all resources load
networkidle0 - Automatically waits for
document.fonts.ready - Supports CSS rules for print styling
@page - Device scale factor set to 2 for crisp rendering
- 使用Puppeteer和无头Chrome进行渲染
- 等待状态以确保所有资源加载完成
networkidle0 - 自动等待事件
document.fonts.ready - 支持CSS规则进行打印样式设置
@page - 设备缩放系数设置为2,确保渲染清晰