一个基于Node.js和Puppeteer的高性能HTML转图片工具,提供REST API和简洁的Web界面,支持自定义截图参数和HTML净化功能。
- 图片生成 - 将HTML内容转换为高质量PNG图片
- 尺寸定制 - 支持自定义截图宽度、高度和缩放比例
- 安全净化 - 内置HTML净化功能,防止XSS攻击
- 智能缓存 - 自动缓存生成的图片,24小时有效期
- 完善文档 - 集成Swagger UI的API文档
- 友好界面 - 简洁易用的Web操作界面
访问演示站点: http://118.25.84.228:15600/
国内服务器
bash <(curl -sSL https://fastly.jsdelivr.net/gh/czhmisaka/Html2Img/install.sh)国外服务器
bash <(curl -sSL https://raw.githubusercontent.com/czhmisaka/Html2Img/main/install.sh)使用示例
运行
bash install.sh build
停止删除
bash install.sh delete html2img
- Node.js 22+
- npm 6+
- Puppeteer依赖的Chromium
-
安装Chromium/Chrome:
# Ubuntu/Debian sudo apt-get install chromium-browser # CentOS/RHEL sudo yum install chromium # macOS (通过Homebrew) brew install --cask google-chrome
-
安装中文字体:
# Ubuntu/Debian sudo apt-get install fonts-wqy-microhei fonts-wqy-zenhei # CentOS/RHEL sudo yum install wqy-microhei-fonts wqy-zenhei-fonts # macOS (通过Homebrew) brew tap homebrew/cask-fonts brew install --cask font-wqy-microhei
-
验证字体安装:
fc-list :lang=zh
-
克隆仓库:
git clone https://github.com/czhmisaka/Html2Img.git
cd Html2Img/html-to-image- 安装依赖:
npm install- 启动服务:
npm start服务默认运行在: http://localhost:15600
访问 /docs 路径查看交互式Swagger文档:
http://localhost:15600/docs
| 端点 | 方法 | 描述 |
|---|---|---|
/api/sanitize |
POST | HTML内容净化 |
/api/screenshot |
POST | 生成截图并直接返回 |
/api/image/{id} |
GET | 通过ID获取缓存图片 |
/api/screenshot-id |
POST | 生成截图并返回缓存ID |
可通过环境变量配置服务:
# 服务端口
PORT=15600
# 缓存目录路径
CACHE_DIR=./cache
# 缓存有效期(秒)
CACHE_TTL=86400html-to-image/
├── index.js # 服务主入口
├── routes/
│ └── api.js # API路由定义
├── utils/
│ ├── cache.js # 缓存管理
│ ├── puppeteer.js # 截图核心逻辑
│ └── sanitize.js # HTML净化
├── public/ # 静态资源
│ └── index.html # Web界面
└── cache/ # 图片缓存目录
测试脚本位于 /test 目录:
cd test
./test_118.sh欢迎通过以下方式参与项目:
- 提交Issue报告问题
- 发起Pull Request贡献代码
- 完善文档和测试用例
本项目采用 Apache License 2.0 开源协议。