Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web-to-pdf

将多页 Web 教程/文档网站转换为带书签的 A4 PDF。

功能

  • 自动发现教程站点侧边栏/导航中的所有页面链接
  • 提取正文内容,移除广告/导航/侧边栏
  • SVG 图表 CJK 字体修复(Mermaid 等)
  • 生成封面 + 目录 + 全部章节
  • Playwright (Chromium) 渲染 PDF,pypdf 添加书签

依赖

# Python 包
pip install requests beautifulsoup4 playwright pypdf

# 安装 Chromium
playwright install chromium

# macOS 字体回退
brew install pango

使用

# 基础用法
python3 web_to_pdf.py <start_url> [--output path.pdf]

# 示例
python3 web_to_pdf.py https://www.runoob.com/claude-code/claude-code-tutorial.html \
  --output ~/Claude_Code_Tutorial.pdf

# 跳过封面/目录
python3 web_to_pdf.py <url> --no-cover --no-toc

# 非 CJK 站点,跳过字体修复
python3 web_to_pdf.py <url> --no-font-fix

参数

参数 说明 默认值
url 教程起始页 URL -
--output, -o 输出 PDF 路径 ./output.pdf
--title, -t PDF 封面标题 自动检测
--no-font-fix 跳过 SVG 字体替换 false
--delay 页面请求间隔(秒) 0.5
--no-cover 跳过封面页 false
--no-toc 跳过目录页 false

技术选型

决策 选择 原因
PDF 渲染 Playwright (Chromium) 唯一能正确渲染 SVG foreignObject + CJK 的引擎
不用 WeasyPrint WeasyPrint 丢失 SVG foreignObject 文本 Mermaid 图表使用 foreignObject 显示标签
SVG 处理 下载 → 替换字体 → 内联 <img src=".svg"> 无法访问系统字体,内联解决
CJK 字体回退 PingFang SC, Heiti SC, Hiragino Sans GB macOS 内置,覆盖 zh-CN/zh-TW
书签生成 Playwright 渲染 → pypdf 添加大纲 Chromium PDF 导出无书签 API,后处理添加

作为 CodeBuddy Skill 使用

SKILL.mdweb_to_pdf.py 放入 ~/.claude/skills/web-to-pdf/ 目录即可。

License

MIT

About

Convert multi-page web tutorials/docs into a single, clean, bookmarked A4 PDF. Playwright + pypdf.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages