# html2pdf
**Repository Path**: crane-education/html2pdf
## Basic Information
- **Project Name**: html2pdf
- **Description**: 将html页面转换为PDF
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-10-10
- **Last Updated**: 2025-10-10
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# html2pdf - HTML 转 PDF 工具
## 项目简介
`html2pdf` 是一款专注于 HTML 页面转 PDF 文档的轻量化工具体积,通过标准化的容器化方案解决跨平台环境依赖问题,确保在不同系统中都能稳定输出格式一致的 PDF 文件。项目核心依赖 Chrome 官方测试版内核,保障 HTML 渲染的准确性与兼容性。
## 核心特性
- **跨平台无依赖**:基于 Docker 打包,无需在本地配置复杂环境,支持 Linux、macOS、Windows 系统
- **渲染稳定**:采用 [Chrome for Testing](https://googlechromelabs.github.io/chrome-for-testing/) 官方内核,避免第三方渲染引擎导致的样式错乱问题
- **使用简单**:仅需 3 步(拉取代码 → 构建镜像 → 运行容器)即可完成 PDF 生成
- **可自定义**:支持替换自定义 HTML 内容、调整 PDF 输出路径与命名
## 项目地址
- **代码仓库**:[https://gitee.com/crane-education/html2pdf](https://gitee.com/crane-education/html2pdf)(必须通过此仓库获取源码,且需打包为 Docker 镜像后使用)
## 关键依赖说明
项目运行依赖 `chrome-linux64` 二进制文件,该文件来源于 Chrome 官方测试资源站:
- **依赖来源**:[Chrome for Testing 官网](https://googlechromelabs.github.io/chrome-for-testing/)
- **获取方式**:`Dockerfile` 中已配置自动下载逻辑,无需手动操作;若需指定版本,可从官网选择对应 `chrome-linux64` 包替换
> ⚠️ 重要提示:项目**必须打包为 Docker 镜像**才能正常使用,本地直接运行源码可能会因缺少 Chrome 内核依赖报错。
## 环境准备
### 1. 必备工具
| 工具名称 | 作用 | 下载地址 |
|----------|------|----------|
| Docker | 构建与运行容器,解决环境依赖 | [Docker 官方下载](https://www.docker.com/products/docker-desktop/) |
| Git | 拉取项目代码(可选,也可直接从 Gitee 下载压缩包) | [Git 官方下载](https://git-scm.com/downloads) |
## 使用步骤
### 1. 拉取项目代码
```bash
# 克隆 Gitee 仓库到本地
git clone https://gitee.com/crane-education/html2pdf.git
# 进入项目根目录
cd html2pdf
```
### 2. 安装node依赖
```bash
npm i
```
### 3. 下载内核文件
使用浏览器或命令行工具(如 wget、curl)下载内核压缩包:
命令行示例(wget):
```bash
wget https://storage.googleapis.com/chrome-for-testing-public/142.0.7444.23/linux64/chrome-linux64.zip
```
命令行示例(curl):
```bash
curl -O https://storage.googleapis.com/chrome-for-testing-public/142.0.7444.23/linux64/chrome-linux64.zip
```
下载完成后,确认文件完整性(可通过解压测试,确保无损坏)
```bash
# 解压 chrome-linux64.zip 到当前目录(会生成 chrome-linux64 文件夹)
unzip chrome-linux64.zip
# 解压完成后,验证解压结果(终端会显示 chrome-linux64 文件夹及内部文件)
ls -l chrome-linux64/
```
### 4. 构建 Docker 镜像
```bash
# 构建镜像(镜像名可自定义,此处示例为 html2pdf:latest)
docker build -t html2pdf:latest .
```
### 5. 运行容器并生成 PDF
```bash
docker run -p 9000:9000 html2pdf
```
## 参考链接
Chrome for Testing 官网:https://googlechromelabs.github.io/chrome-for-testing/
项目代码仓库:https://gitee.com/crane-education/html2pdf
Docker 官方文档:https://docs.docker.com/get-started/