dompdf.js 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.versionrc +8 -0
- package/CHANGELOG.md +32 -543
- package/README.md +201 -155
- package/README_CN.md +233 -238
- package/dist/dompdf.esm.js +26010 -14837
- package/dist/dompdf.esm.js.map +1 -1
- package/dist/dompdf.js +40542 -29369
- package/dist/dompdf.js.map +1 -1
- package/dist/dompdf.min.js +67 -0
- package/dist/index.d.ts +7 -6
- package/dist/lib/dom/node-parser.js +0 -62
- package/dist/lib/dom/node-parser.js.map +1 -1
- package/dist/lib/index.js +41 -34
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/render/canvas/pdf-renderer.js +358 -228
- package/dist/lib/render/canvas/pdf-renderer.js.map +1 -1
- package/dist/lib/render/paginate copy 2.js +178 -0
- package/dist/lib/render/paginate copy 2.js.map +1 -0
- package/dist/lib/render/paginate.js +68 -29
- package/dist/lib/render/paginate.js.map +1 -1
- package/dist/lib/utils/css-utils.js +19 -0
- package/dist/lib/utils/css-utils.js.map +1 -0
- package/dist/lib/utils/element-utils.js +25 -0
- package/dist/lib/utils/element-utils.js.map +1 -0
- package/dist/lib/utils/font-utils.js +70 -0
- package/dist/lib/utils/font-utils.js.map +1 -0
- package/dist/lib/utils/index.js +22 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/type-utils.js +40 -0
- package/dist/lib/utils/type-utils.js.map +1 -0
- package/dist/lib/utils/url-path.js +22 -0
- package/dist/lib/utils/url-path.js.map +1 -0
- package/dist/render/canvas/pdf-renderer.d.ts +29 -24
- package/dist/render/paginate copy 2.d.ts +3 -0
- package/dist/render/paginate.d.ts +1 -1
- package/dist/types/index.d.ts +7 -6
- package/dist/types/render/canvas/pdf-renderer.d.ts +29 -24
- package/dist/types/render/paginate copy 2.d.ts +3 -0
- package/dist/types/render/paginate.d.ts +1 -1
- package/dist/types/utils/css-utils.d.ts +2 -0
- package/dist/types/utils/element-utils.d.ts +7 -0
- package/dist/types/utils/font-utils.d.ts +9 -0
- package/dist/types/utils/index.d.ts +5 -0
- package/dist/types/utils/type-utils.d.ts +4 -0
- package/dist/types/utils/url-path.d.ts +7 -0
- package/dist/utils/css-utils.d.ts +2 -0
- package/dist/utils/element-utils.d.ts +7 -0
- package/dist/utils/font-utils.d.ts +9 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/type-utils.d.ts +4 -0
- package/dist/utils/url-path.d.ts +7 -0
- package/fontconverter/FileSaver.js +2 -0
- package/fontconverter/filereader.js +432 -0
- package/fontconverter/fontconverter.html +66 -0
- package/fontconverter/pure-min.css +11 -0
- package/package.json +135 -135
- package/page_sizes.md +50 -0
- package/.vscode/settings.json +0 -5
- package/html2pdf-userscript.js +0 -936
package/README_CN.md
CHANGED
|
@@ -1,305 +1,300 @@
|
|
|
1
|
-
# dompdf
|
|
2
|
-
|
|
3
|
-
dompdf 纯前端将HTML转换为可编辑,非图片式的,可打印的PDF文件。
|
|
4
|
-
|
|
5
|
-
> :warning: v0.10 版本中报告了一些问题。这些问题正在调查中,同时您可以考虑继续使用 v0.9.3 版本("^0.9.3" in npm, 或者[在 HTML script 标签中使用 cdnjs](https://cdnjs.com/libraries/dompdf.js/0.9.3))。
|
|
6
|
-
|
|
7
|
-
## 目录
|
|
8
|
-
|
|
9
|
-
- [开始使用](#开始使用)
|
|
10
|
-
- [CDN](#cdn)
|
|
11
|
-
- [原生 JS](#原生-js)
|
|
12
|
-
- [NPM](#npm)
|
|
13
|
-
- [Bower](#bower)
|
|
14
|
-
- [控制台](#控制台)
|
|
15
|
-
- [使用方法](#使用方法)
|
|
16
|
-
- [高级用法](#高级用法)
|
|
17
|
-
- [工作流程](#工作流程)
|
|
18
|
-
- [Worker API](#worker-api)
|
|
19
|
-
- [配置选项](#配置选项)
|
|
20
|
-
- [分页](#分页)
|
|
21
|
-
- [分页设置](#分页设置)
|
|
22
|
-
- [分页模式](#分页模式)
|
|
23
|
-
- [使用示例](#使用示例)
|
|
24
|
-
- [图片类型和质量](#图片类型和质量)
|
|
25
|
-
- [进度跟踪](#进度跟踪)
|
|
26
|
-
- [依赖项](#依赖项)
|
|
27
|
-
- [贡献](#贡献)
|
|
28
|
-
- [问题](#问题)
|
|
29
|
-
- [测试](#测试)
|
|
30
|
-
- [Pull requests](#pull-requests)
|
|
31
|
-
- [致谢](#致谢)
|
|
32
|
-
- [许可证](#许可证)
|
|
33
|
-
|
|
34
|
-
## 开始使用
|
|
35
|
-
|
|
36
|
-
#### CDN
|
|
37
|
-
|
|
38
|
-
使用 dompdf.js 最简单的方法是通过 cdnjs 在 HTML 中引入它:
|
|
1
|
+
# dompdf.js
|
|
39
2
|
|
|
40
|
-
|
|
41
|
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompdf.js/0.10.1/dompdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
|
42
|
-
```
|
|
3
|
+
[English](./README.md) | [中文](./README_CN.md)
|
|
43
4
|
|
|
44
|
-
|
|
5
|
+
<!-- [主页](https://html2canvas.hertzen.com) | [下载](https://github.com/niklasvh/html2canvas/releases) | [问题](https://github.com/niklasvh/html2canvas/discussions/categories/q-a)
|
|
45
6
|
|
|
46
|
-
|
|
7
|
+
[](https://gitter.im/niklasvh/html2canvas?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
|
8
|
+

|
|
9
|
+
[](https://www.npmjs.org/package/html2canvas)
|
|
10
|
+
[](https://www.npmjs.org/package/html2canvas) -->
|
|
47
11
|
|
|
48
|
-
|
|
12
|
+
该脚本允许您直接在用户浏览器上将网页或部分网页生成为可编辑、非图片式、可打印的矢量 pdf。支持分页,最多可以生成上数千页的 pdf 文件。由于生成是基于 DOM 的,因此可能与实际表现不会 100% 一致。如果是复杂的 pdf 生成需求,不建议使用。
|
|
49
13
|
|
|
50
|
-
|
|
14
|
+
在线体验:[在线体验](https://dompdfjs.lisky.com.cn)
|
|
51
15
|
|
|
52
|
-
|
|
53
|
-
<script src="dompdf.bundle.min.js"></script>
|
|
54
|
-
```
|
|
16
|
+
### 📄 pdf 生成示例
|
|
55
17
|
|
|
56
|
-
|
|
18
|
+

|
|
57
19
|
|
|
58
|
-
|
|
20
|
+
### 🛠️ 它是如何工作的
|
|
59
21
|
|
|
60
|
-
|
|
22
|
+
该脚本基于[html2canvas](https://github.com/niklasvh/html2canvas)和[jspdf](https://github.com/MrRio/jsPDF),与以往将 html 页面通过 html2canvas 渲染为图片,再通过 jspdf 将图片生成 pdf 文件不同,该脚本通过读取 DOM 和应用于元素的不同样式,改造了 html2canvas 的 canvas-renderer 文件,调用 jspdf 的方法生成 pdf 文件。
|
|
23
|
+
所以他有以下优势:
|
|
61
24
|
|
|
62
|
-
|
|
25
|
+
1. 不需要服务器端的任何渲染,因为整个 pdf 是在**客户端浏览器**上创建的。
|
|
26
|
+
2. 生成的是真正的 pdf 文件,而不是图片式的,这样生成的 pdf 质量更高,您也可以编辑和打印生成 pdf 文件。
|
|
27
|
+
3. 更小的 pdf 文件体积。
|
|
28
|
+
4. 不受 canvas 渲染高度限制,可以生成数千页的 pdf 文件。
|
|
63
29
|
|
|
64
|
-
|
|
30
|
+
当然,它也有一些缺点:
|
|
65
31
|
|
|
66
|
-
|
|
32
|
+
1. 由于是基于 DOM 的,所以可能与实际表现不会 100% 一致。
|
|
33
|
+
2. 有的 css 属性还没有被支持,查看[支持的 css 属性](https://www.html2canvas.cn/html2canvas-features.html)。
|
|
67
34
|
|
|
68
|
-
|
|
35
|
+
### ✨ 已实现功能
|
|
69
36
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
37
|
+
| 功能 | 状态 | 说明 |
|
|
38
|
+
| -------- | ---- | --------------------------------------------------------------------------------------------------------- |
|
|
39
|
+
| 分页 | ✅ | 支持 PDF 分页渲染,可生成数千页的 PDF 文件 |
|
|
40
|
+
| 文本渲染 | ✅ | 支持基础文本内容渲染,font-family,font-size,font-style,font-variant,color 等,支持文字描边,不支持文字阴影 |
|
|
41
|
+
| 图片渲染 | ✅ | 支持网络图片,base64 图片,svg 图片 |
|
|
42
|
+
| 边框 | ✅ | 支持 border-width,border-color,border-style,border-radius,暂时只实现了实线边框 |
|
|
43
|
+
| 背景 | ✅ | 支持背景颜色,背景图片,背景渐变 |
|
|
44
|
+
| canvas | ✅ | 支持渲染 canvas |
|
|
45
|
+
| svg | ✅ | 支持渲染 svg |
|
|
46
|
+
| 阴影渲染 | ✅ | 使用 foreignObjectRendering,支持边框阴影渲染 |
|
|
47
|
+
| 渐变渲染 | ✅ | 使用 foreignObjectRendering,支持背景渐变渲染 |
|
|
48
|
+
| iframe | ❌ | 暂不支持渲染 iframe |
|
|
82
49
|
|
|
83
|
-
|
|
50
|
+
### 📦 使用方法
|
|
84
51
|
|
|
85
|
-
|
|
52
|
+
dompdf 库使用 `Promise` 并期望它们在全局上下文中可用。如果您希望支持不原生支持 `Promise` 的[较旧浏览器](http://caniuse.com/#search=promise),请在引入 `dompdf` 之前包含一个 polyfill,比如 [es6-promise](https://github.com/jakearchibald/es6-promise)。
|
|
86
53
|
|
|
87
|
-
|
|
88
|
-
var element = document.getElementById('element-to-print');
|
|
89
|
-
dompdf(element);
|
|
90
|
-
```
|
|
54
|
+
安装:
|
|
91
55
|
|
|
92
|
-
|
|
56
|
+
npm install dompdf.js --save
|
|
93
57
|
|
|
94
|
-
|
|
58
|
+
CDN 引入:
|
|
95
59
|
|
|
96
|
-
```
|
|
97
|
-
|
|
60
|
+
```html
|
|
61
|
+
<script src="https://cdn.jsdelivr.net/npm/dompdf.js@latest/dist/dompdf.js"></script>
|
|
98
62
|
```
|
|
99
63
|
|
|
100
|
-
|
|
64
|
+
#### 🚀 基础用法
|
|
101
65
|
|
|
102
66
|
```js
|
|
103
|
-
|
|
104
|
-
|
|
67
|
+
import dompdf from 'dompdf.js';
|
|
68
|
+
|
|
69
|
+
dompdf(document.querySelector('#capture'), options)
|
|
70
|
+
.then((blob) => {
|
|
71
|
+
const url = URL.createObjectURL(blob);
|
|
72
|
+
const a = document.createElement('a');
|
|
73
|
+
a.href = url;
|
|
74
|
+
a.download = 'example.pdf';
|
|
75
|
+
document.body.appendChild(a);
|
|
76
|
+
a.click();
|
|
77
|
+
})
|
|
78
|
+
.catch((err) => {
|
|
79
|
+
console.error(err);
|
|
80
|
+
});
|
|
105
81
|
```
|
|
106
82
|
|
|
107
|
-
####
|
|
83
|
+
#### 📄 PDF 分页渲染
|
|
108
84
|
|
|
109
|
-
dompdf
|
|
85
|
+
默认情况下,dompdf 会将整个文档渲染到单页中。
|
|
110
86
|
|
|
111
|
-
|
|
112
|
-
.from() -> .toContainer() -> .toCanvas() -> .toImg() -> .toPdf() -> .save()
|
|
113
|
-
```
|
|
87
|
+
您可以通过设置 `pagination` 选项为 `true` 来开启分页渲染。通过 pageConfig 字段自定义页眉页脚的尺寸,内容,字体颜色/大小,位置等信息。
|
|
114
88
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
| Method | Arguments | Description |
|
|
118
|
-
|--------------|--------------------|-------------|
|
|
119
|
-
| from | src, type | Sets the source (HTML string or element) for the PDF. Optional `type` specifies other sources: `'string'`, `'element'`, `'canvas'`, or `'img'`. |
|
|
120
|
-
| to | target | Converts the source to the specified target (`'container'`, `'canvas'`, `'img'`, or `'pdf'`). Each target also has its own `toX` method that can be called directly: `toContainer()`, `toCanvas()`, `toImg()`, and `toPdf()`. |
|
|
121
|
-
| output | type, options, src | Routes to the appropriate `outputPdf` or `outputImg` method based on specified `src` (`'pdf'` (default) or `'img'`). |
|
|
122
|
-
| outputPdf | type, options | Sends `type` and `options` to the jsPDF object's `output` method, and returns the result as a Promise (use `.then` to access). See the [jsPDF source code](https://rawgit.com/MrRio/jsPDF/master/docs/jspdf.js.html#line992) for more info. |
|
|
123
|
-
| outputImg | type, options | Returns the specified data type for the image as a Promise (use `.then` to access). Supported types: `'img'`, `'datauristring'`/`'dataurlstring'`, and `'datauri'`/`'dataurl'`. |
|
|
124
|
-
| save | filename | Saves the PDF object with the optional filename (creates user download prompt). |
|
|
125
|
-
| set | opt | Sets the specified properties. See [Options](#options) below for more details. |
|
|
126
|
-
| get | key, cbk | Returns the property specified in `key`, either as a Promise (use `.then` to access), or by calling `cbk` if provided. |
|
|
127
|
-
| then | onFulfilled, onRejected | Standard Promise method, with `this` re-bound to the Worker, and with added progress-tracking (see [Progress](#progress) below). Note that `.then` returns a `Worker`, which is a subclass of Promise. |
|
|
128
|
-
| thenCore | onFulFilled, onRejected | Standard Promise method, with `this` re-bound to the Worker (no progress-tracking). Note that `.thenCore` returns a `Worker`, which is a subclass of Promise. |
|
|
129
|
-
| thenExternal | onFulfilled, onRejected | True Promise method. Using this 'exits' the Worker chain - you will not be able to continue chaining Worker methods after `.thenExternal`. |
|
|
130
|
-
| catch, catchExternal | onRejected | Standard Promise method. `catchExternal` exits the Worker chain - you will not be able to continue chaining Worker methods after `.catchExternal`. |
|
|
131
|
-
| error | msg | Throws an error in the Worker's Promise chain. |
|
|
132
|
-
|
|
133
|
-
A few aliases are also provided for convenience:
|
|
134
|
-
|
|
135
|
-
| Method | Alias |
|
|
136
|
-
|-----------|-----------|
|
|
137
|
-
| save | saveAs |
|
|
138
|
-
| set | using |
|
|
139
|
-
| output | export |
|
|
140
|
-
| then | run |
|
|
141
|
-
|
|
142
|
-
## 配置选项
|
|
143
|
-
|
|
144
|
-
dompdf.js 可以使用可选的 `opt` 参数进行配置:
|
|
89
|
+
**_ 需要注意,将要生成 PDF 的 dom 节点设置为对应的页面宽度(px),比如 a4 要设置宽度为 794px,这里是页面尺寸对照表:[page_sizes.md](./page_sizes.md) _**
|
|
145
90
|
|
|
146
91
|
```js
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
92
|
+
import dompdf from 'dompdf.js';
|
|
93
|
+
|
|
94
|
+
dompdf(document.querySelector('#capture'), {
|
|
95
|
+
pagination: true,
|
|
96
|
+
format: 'a4',
|
|
97
|
+
pageConfig: {
|
|
98
|
+
header: {
|
|
99
|
+
content: '这是页眉',
|
|
100
|
+
height: 50,
|
|
101
|
+
contentColor: '#333333',
|
|
102
|
+
contentFontSize: 12,
|
|
103
|
+
contentPosition: 'center',
|
|
104
|
+
padding: [0, 0, 0, 0]
|
|
105
|
+
},
|
|
106
|
+
footer: {
|
|
107
|
+
content: '第${currentPage}页/共${totalPages}页',
|
|
108
|
+
height: 50,
|
|
109
|
+
contentColor: '#333333',
|
|
110
|
+
contentFontSize: 12,
|
|
111
|
+
contentPosition: 'center',
|
|
112
|
+
padding: [0, 0, 0, 0]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
.then((blob) => {
|
|
117
|
+
const url = URL.createObjectURL(blob);
|
|
118
|
+
const a = document.createElement('a');
|
|
119
|
+
a.href = url;
|
|
120
|
+
a.download = 'example.pdf';
|
|
121
|
+
document.body.appendChild(a);
|
|
122
|
+
a.click();
|
|
123
|
+
})
|
|
124
|
+
.catch((err) => {
|
|
125
|
+
console.error(err);
|
|
126
|
+
});
|
|
161
127
|
```
|
|
162
128
|
|
|
163
|
-
`
|
|
129
|
+
##### 更精准的分页控制-`divisionDisable` 属性
|
|
164
130
|
|
|
165
|
-
|
|
166
|
-
|------------|----------------|--------------------------------|------------------------------------------------------------------------------------------------------------|
|
|
167
|
-
|margin |number or array |`0` |PDF margin (in jsPDF units). Can be a single number, `[vMargin, hMargin]`, or `[top, left, bottom, right]`. |
|
|
168
|
-
|filename |string |`'file.pdf'` |The default filename of the exported PDF. |
|
|
169
|
-
|pagebreak |object |`{mode: ['css', 'legacy']}` |Controls the pagebreak behaviour on the page. See [Page-breaks](#page-breaks) below. |
|
|
170
|
-
|image |object |`{type: 'jpeg', quality: 0.95}` |The image type and quality used to generate the PDF. See [Image type and quality](#image-type-and-quality) below.|
|
|
171
|
-
|enableLinks |boolean |`true` |If enabled, PDF hyperlinks are automatically added ontop of all anchor tags. |
|
|
172
|
-
|html2canvas |object |`{ }` |Configuration options sent directly to `html2canvas` ([see here](https://html2canvas.hertzen.com/configuration) for usage).|
|
|
173
|
-
|jsPDF |object |`{ }` |Configuration options sent directly to `jsPDF` ([see here](http://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html) for usage).|
|
|
131
|
+
如果您不希望某个容器在分页时被拆分时,为该元素添加 `divisionDisable` 属性,跨页时它会整体移至下一页。
|
|
174
132
|
|
|
175
|
-
|
|
133
|
+
#### ⚙️ options 参数
|
|
176
134
|
|
|
177
|
-
|
|
135
|
+
| 参数名 | 必传 | 默认值 | 类型 | 说明 |
|
|
136
|
+
| ------------------ | ---- | ------------- | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
137
|
+
| `useCORS` | 否 | `false` | `boolean` | 允许跨域资源(需服务端 CORS 配置) |
|
|
138
|
+
| `backgroundColor` | 否 | 自动解析/白色 | `string \| null` | 覆盖页面背景色;传 `null` 生成透明背景 |
|
|
139
|
+
| `fontConfig` | 否 | - | `object \| Array` | 非英文字体配置,见下表 |
|
|
140
|
+
| `encryption` | 否 | 空配置 | `object` | PDF 加密配置,属性`userPassword` 用于给定权限列表下用户的密码;属性`ownerPassword` 需要设置 userPassword 和 ownerPassword 以进行正确的身份验证;属性`userPermissions` 用于指定用户权限,可选值为 `['print', 'modify', 'copy', 'annot-forms']` |
|
|
141
|
+
| `precision` | 否 | `16` | `number` | 元素位置的精度 |
|
|
142
|
+
| `compress` | 否 | `false` | `boolean` | 是否压缩 PDF |
|
|
143
|
+
| `putOnlyUsedFonts` | 否 | `false` | `boolean` | 仅将实际使用的字体嵌入 PDF |
|
|
144
|
+
| `pagination` | 否 | `false` | `boolean` | 开启分页渲染 |
|
|
145
|
+
| `format` | 否 | `'a4'` | `string` | 页面规格,支持 `a0–a10`、`b0–b10`、`c0–c10`、`letter` 等 |
|
|
146
|
+
| `pageConfig` | 否 | 见下表 | `object` | 页眉页脚配置 |
|
|
147
|
+
| `onJspdfReady` | 否 | `` | `Function(jspdf: jsPDF)` | jspdf 实例初始化 |
|
|
148
|
+
| `onJspdfFinish` | 否 | `` | `Function(jspdf: jsPDF)` | jspdf 实例绘制 pdf 完成 |
|
|
178
149
|
|
|
179
|
-
|
|
150
|
+
##### `pageConfig`字段:
|
|
180
151
|
|
|
181
|
-
|
|
152
|
+
| 参数名 | 默认值 | 类型 | 说明 |
|
|
153
|
+
| -------- | ------------------------ | ------ | -------- |
|
|
154
|
+
| `header` | 见下表 pageConfigOptions | object | 页眉设置 |
|
|
155
|
+
| `footer` | 见下表 pageConfigOptions | object | 页脚设置 |
|
|
182
156
|
|
|
183
|
-
|
|
184
|
-
|----------|----------------|--------------------|------------|
|
|
185
|
-
|mode |string or array |`['css', 'legacy']` |The mode(s) on which to automatically add page-breaks. One or more of `'avoid-all'`, `'css'`, and `'legacy'`. |
|
|
186
|
-
|before |string or array |`[]` |CSS selectors for which to add page-breaks before each element. Can be a specific element with an ID (`'#myID'`), all elements of a type (e.g. `'img'`), all of a class (`'.myClass'`), or even `'*'` to match every element. |
|
|
187
|
-
|after |string or array |`[]` |Like 'before', but adds a page-break immediately after the element. |
|
|
188
|
-
|avoid |string or array |`[]` |Like 'before', but avoids page-breaks on these elements. You can enable this feature on every element using the 'avoid-all' mode. |
|
|
157
|
+
##### `pageConfigOptions` 字段:
|
|
189
158
|
|
|
190
|
-
|
|
159
|
+
| 参数名 | 默认值 | 类型 | 说明 |
|
|
160
|
+
| ----------------- | --------------------------------------------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
161
|
+
| `content` | 页眉默认值为空,页脚默认值为`${currentPage}/${totalPages}` | `string` | 文本内容,支持 `${currentPage}`、`${totalPages}`,`${currentPage}`为当前页码,`${totalPages}`为总页码 |
|
|
162
|
+
| `height` | `50` | `number` | 区域高度(px) |
|
|
163
|
+
| `contentPosition` | `'center'` | `string \| [number, number]` | 文本位置枚举 `center`、`centerLeft` 、 `centerRight`、`centerTop`、 `centerBottom`、`leftTop`、 `leftBottom`、`rightTop`、`rightBottom`或坐标 `[x,y]` |
|
|
164
|
+
| `contentColor` | `'#333333'` | `string` | 文本颜色 |
|
|
165
|
+
| `contentFontSize` | `16` | `number` | 文本字号(px) |
|
|
166
|
+
| `padding` | `[0,24,0,24]` | `[number, number, number, number]` | 上/右/下/左内边距(px) |
|
|
191
167
|
|
|
192
|
-
|
|
193
|
-
|-----------|-------------|
|
|
194
|
-
| avoid-all | Automatically adds page-breaks to avoid splitting any elements across pages. |
|
|
195
|
-
| css | Adds page-breaks according to the CSS `break-before`, `break-after`, and `break-inside` properties. Only recognizes `always/left/right` for before/after, and `avoid` for inside. |
|
|
196
|
-
| legacy | Adds page-breaks after elements with class `dompdf__page-break`. This feature may be removed in the future. |
|
|
168
|
+
##### 字体配置(`fontConfig`)字段:
|
|
197
169
|
|
|
198
|
-
|
|
170
|
+
| 字段 | 必传 | 默认值 | 类型 | 说明 |
|
|
171
|
+
| ------------ | ------------------------------ | ------ | -------- | ---------------------------------- |
|
|
172
|
+
| `fontFamily` | 是(启用自定义字体时) | `''` | `string` | 字体家族名(与注入的 `.ttf` 同名) |
|
|
173
|
+
| `fontBase64` | 是(启用自定义字体时) | `''` | `string` | `.ttf` 的 Base64 字符串内容 |
|
|
174
|
+
| `fontStyle` | 是(启用自定义字体时) | `''` | `string` | `normal \| italic` |
|
|
175
|
+
| `fontWeight` | 是(启用自定义字体时字体加粗) | `''` | `number` | `400 \| 700` |
|
|
176
|
+
| `iconFont` | 否 | `false` | `boolean`| `false \| true` |
|
|
199
177
|
|
|
200
|
-
|
|
201
|
-
// Avoid page-breaks on all elements, and add one before #page2el.
|
|
202
|
-
dompdf().set({
|
|
203
|
-
pagebreak: { mode: 'avoid-all', before: '#page2el' }
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// Enable all 'modes', with no explicit elements.
|
|
207
|
-
dompdf().set({
|
|
208
|
-
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
// No modes, only explicit elements.
|
|
212
|
-
dompdf().set({
|
|
213
|
-
pagebreak: { before: '.beforeClass', after: ['#after1', '#after2'], avoid: 'img' }
|
|
214
|
-
});
|
|
215
|
-
```
|
|
178
|
+
#### 🔣 乱码问题-字体导入支持
|
|
216
179
|
|
|
217
|
-
|
|
180
|
+
由于 jspdf 只支持英文,所以其他语言会出现乱码的问题,需要导入对应的字体文件来解决,如果需要自定义字体,在[这里](https://github.com/lmn1919/dompdf.js/tree/main/fontconverter)将字体 tff 文件转化成 base64 格式的 js 文件,中文字体推荐使用[思源黑体](https://github.com/lmn1919/dompdf.js/blob/main/examples/SourceHanSansSC-Normal-Min-normal.js),体积较小。
|
|
181
|
+
在代码中引入该文件即可。
|
|
218
182
|
|
|
219
|
-
|
|
183
|
+
> **注意:导入字体会导致最终的 pdf 体积增大,如果对最终 pdf 体积有要求的,建议精简字体,可以剔除不需要的字体。或者使用`Fontmin`等工具对字体进行瘦身**
|
|
220
184
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
185
|
+
```js
|
|
186
|
+
<script type="text/javascript" src="./SourceHanSansSC-Normal-Min-normal.js"></script>
|
|
187
|
+
<script type="text/javascript" src="./SourceHanSansCNBold-bold.js"></script>
|
|
188
|
+
<script type="text/javascript" src="./SourceHanSansCNNormal-normal.js"></script>
|
|
189
|
+
<script type="text/javascript" src="./SourceHanSansCNRegularItalic-normal.js"></script>
|
|
190
|
+
<script type="text/javascript" src="./iconfont-ttf.js"></script>
|
|
191
|
+
<script>
|
|
192
|
+
/* 导入字体 */
|
|
193
|
+
dompdf(document.querySelector('#capture'), {
|
|
194
|
+
useCORS: true,
|
|
195
|
+
/* 单个字体导入 */
|
|
196
|
+
/* fontConfig: {
|
|
197
|
+
fontFamily: 'SourceHanSansSC-Normal-Min',
|
|
198
|
+
fontBase64: window.fontBase64,
|
|
199
|
+
fontStyle: 'normal',
|
|
200
|
+
fontWeight: 400,
|
|
201
|
+
}, */
|
|
202
|
+
/* 导入注册多种字体,需要支持什么语种,样式,就导入对应的字体 */
|
|
203
|
+
fontConfig: [
|
|
204
|
+
{
|
|
205
|
+
fontFamily: 'iconfont',
|
|
206
|
+
fontBase64: window.iconfont,
|
|
207
|
+
fontUrl: '',
|
|
208
|
+
fontWeight: 400,
|
|
209
|
+
fontStyle: 'normal',
|
|
210
|
+
iconFont: true
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
fontFamily: 'SourceHanSansCNRegularItalic',
|
|
214
|
+
fontBase64: window.SourceHanSansCNRegularItalic,
|
|
215
|
+
fontUrl: '',
|
|
216
|
+
fontWeight: 400,
|
|
217
|
+
fontStyle: 'italic' // 斜体
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
fontFamily: 'SourceHanSansCNBold',
|
|
221
|
+
fontBase64: window.SourceHanSansCNBold,
|
|
222
|
+
fontUrl: '',
|
|
223
|
+
fontWeight: 700, // 加粗
|
|
224
|
+
fontStyle: 'normal'
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
fontFamily: 'SourceHanSansCNNormal',
|
|
228
|
+
fontBase64: window.SourceHanSansCNNormal,
|
|
229
|
+
fontUrl: '',
|
|
230
|
+
fontWeight: 400,
|
|
231
|
+
fontStyle: 'normal'
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
})
|
|
235
|
+
.then(function (blob) {
|
|
236
|
+
const url = URL.createObjectURL(blob);
|
|
237
|
+
const a = document.createElement('a');
|
|
238
|
+
a.href = url;
|
|
239
|
+
a.download = 'example.pdf';
|
|
240
|
+
document.body.appendChild(a);
|
|
241
|
+
a.click();
|
|
242
|
+
})
|
|
243
|
+
.catch(function (err) {
|
|
244
|
+
console.error(err);
|
|
245
|
+
});
|
|
246
|
+
</script>
|
|
247
|
+
```
|
|
227
248
|
|
|
228
|
-
|
|
249
|
+
#### 🎨 绘制渐变色、阴影等复杂样式-foreignObjectRendering 使用
|
|
229
250
|
|
|
230
|
-
|
|
251
|
+
在 dom 十分复杂,或者 pdf 无法绘制的情况(比如:复杂的表格,边框阴影,渐变等),可以考虑使用 foreignObjectRendering。
|
|
252
|
+
给要渲染的元素添加 foreignObjectRendering 属性,就可以通过 svg 的 foreignObject 将它渲染成一张背景图插入到 pdf 文件中。
|
|
231
253
|
|
|
232
|
-
|
|
254
|
+
但是,由于 foreignObject 元素的渲染依赖于浏览器的实现,因此在不同的浏览器中可能会有不同的表现。
|
|
255
|
+
所以,在使用 foreignObjectRendering 时,需要注意以下事项:
|
|
233
256
|
|
|
234
|
-
|
|
257
|
+
1. foreignObject 元素的渲染依赖于浏览器的实现,因此在不同的浏览器中可能会有不同的表现。
|
|
258
|
+
2. IE 浏览器完全不支持,推荐在 chrome 和 firefox,edge 中使用。
|
|
259
|
+
3. 生成的图片会导致 pdf 文件体积变大。
|
|
235
260
|
|
|
236
|
-
|
|
261
|
+
示例
|
|
237
262
|
|
|
238
263
|
```html
|
|
239
|
-
<
|
|
240
|
-
<
|
|
241
|
-
|
|
242
|
-
|
|
264
|
+
<div style="width: 100px;height: 100px;" foreignObjectRendering>
|
|
265
|
+
<div
|
|
266
|
+
style="width: 50px;height: 50px;border: 1px solid #000;box-shadow: 2px 2px 5px rgba(0,0,0,0.3);background: linear-gradient(45deg, #ff6b6b, #4ecdc4);"
|
|
267
|
+
>
|
|
268
|
+
这是一个div元素
|
|
269
|
+
</div>
|
|
270
|
+
</div>
|
|
243
271
|
```
|
|
244
272
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
### 问题
|
|
248
|
-
|
|
249
|
-
在提交问题时,请提供可重现的代码,最好是创建一个[这个模板 jsFiddle](https://jsfiddle.net/u6o6ne41/) 的 fork(已经加载了 dompdf.js),记住 dompdf.js 使用 [html2canvas](https://github.com/niklasvh/html2canvas) 和 [jsPDF](https://github.com/MrRio/jsPDF) 作为依赖项,所以检查每个仓库的问题跟踪器以查看是否已经解决了您的问题。
|
|
250
|
-
|
|
251
|
-
#### 已知问题
|
|
252
|
-
|
|
253
|
-
1. **渲染:** html2canvas 渲染引擎不是完美的(尽管它相当不错!)。如果 html2canvas 没有正确渲染您的内容,我无法修复它。
|
|
254
|
-
- 您可以使用[这个 fiddle](https://jsfiddle.net/eKoopmans/z1rupL4c/) 进行测试,看看画布创建本身是否有问题。
|
|
255
|
-
|
|
256
|
-
2. **节点克隆(CSS 等):** dompdf.js 克隆内容的方式存在错误。正在开发修复 - 尝试:
|
|
257
|
-
- 直接文件: 转到 [dompdf.js/bugfix/clone-nodes-BUILD](/eKoopmans/dompdf.js/tree/bugfix/clone-nodes-BUILD) 并替换项目中的相关文件(例如 `dist/dompdf.bundle.js`)
|
|
258
|
-
- npm: `npm install eKoopmans/dompdf.js#bugfix/clone-nodes-BUILD`
|
|
259
|
-
- 相关项目: [Bugfix: Cloned nodes](https://github.com/eKoopmans/dompdf.js/projects/9)
|
|
260
|
-
|
|
261
|
-
3. **调整大小:** 目前,dompdf.js 将根元素调整大小以适应 PDF 页面(导致内部内容"重新流动")。
|
|
262
|
-
- 这通常是所需的行为,但并非总是如此。
|
|
263
|
-
- 有计划添加替代行为(例如"shrink-to-page"),但尚未准备好测试。
|
|
264
|
-
- 相关项目: [Feature: Single-page PDFs](https://github.com/eKoopmans/dompdf.js/projects/1)
|
|
265
|
-
|
|
266
|
-
4. **渲染为图像:** dompdf.js 将所有内容渲染为图像,然后将该图像放入 PDF 中。
|
|
267
|
-
- 这意味着文本是*不可选择或可搜索的*,并导致大文件大小。
|
|
268
|
-
- 这是目前不可避免的,但是最近在 jsPDF 中的改进可能很快使其能够直接渲染为矢量图形。
|
|
269
|
-
- 相关项目: [Feature: New renderer](https://github.com/eKoopmans/dompdf.js/projects/4)
|
|
270
|
-
|
|
271
|
-
5. **Promise 冲突:** dompdf.js 依赖于特定的 Promise 行为,并且可能在与自定义 Promise 库一起使用时失败。
|
|
272
|
-
- 在下一个版本中,Promises 将在 dompdf.js 中沙盒化以删除此问题。
|
|
273
|
-
- 相关项目: [Bugfix: Sandboxed promises](https://github.com/eKoopmans/dompdf.js/projects/11)
|
|
274
|
-
|
|
275
|
-
6. **最大大小:** HTML5 画布具有[最大高度/宽度](https://stackoverflow.com/a/11585939/4080966)。任何大于该值的都会失败。
|
|
276
|
-
- 这是 HTML5 本身的限制,导致 dompdf.js 中的大 PDF 完全空白。
|
|
277
|
-
- jsPDF 画布渲染器(在已知问题 #4 中提到)可能能够修复此问题!
|
|
278
|
-
- 相关项目: [Bugfix: Maximum canvas size](https://github.com/eKoopmans/dompdf.js/projects/5)
|
|
279
|
-
|
|
280
|
-
### 测试
|
|
273
|
+
### 🌐 浏览器兼容性
|
|
281
274
|
|
|
282
|
-
|
|
275
|
+
该库应该可以在以下浏览器上正常工作(需要 `Promise` polyfill):
|
|
283
276
|
|
|
284
|
-
|
|
277
|
+
- Firefox 3.5+
|
|
278
|
+
- Google Chrome
|
|
279
|
+
- Opera 12+
|
|
280
|
+
- IE9+
|
|
281
|
+
- Safari 6+
|
|
285
282
|
|
|
286
|
-
|
|
283
|
+
### 🏗️ 构建
|
|
287
284
|
|
|
288
|
-
|
|
285
|
+
克隆 git 仓库:
|
|
289
286
|
|
|
290
|
-
|
|
287
|
+
$ git clone git@github.com:lmn1919/dompdf.js.git
|
|
291
288
|
|
|
292
|
-
|
|
289
|
+
安装依赖:
|
|
293
290
|
|
|
294
|
-
|
|
295
|
-
- [@Ranger1230](https://github.com/Ranger1230)
|
|
291
|
+
$ npm install
|
|
296
292
|
|
|
297
|
-
|
|
293
|
+
构建浏览器包:
|
|
298
294
|
|
|
299
|
-
|
|
295
|
+
$ npm run build
|
|
300
296
|
|
|
301
|
-
##
|
|
297
|
+
## 📈 Star History
|
|
302
298
|
|
|
303
|
-
[
|
|
299
|
+
[](https://www.star-history.com/#lmn1919/dompdf.js&Date)
|
|
304
300
|
|
|
305
|
-
Copyright (c) 2017-2019 Erik Koopmans <[http://www.erik-koopmans.com/](http://www.erik-koopmans.com/)>
|