av6-pdf-engine 2.0.0 → 3.0.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/README.md +686 -88
- package/dist/index.d.mts +245 -23
- package/dist/index.d.ts +245 -23
- package/dist/index.js +1325 -813
- package/dist/index.mjs +1291 -812
- package/package.json +6 -1
- package/.prettierignore +0 -4
- package/.prettierrc +0 -6
- package/tmp/border-showcase.pdf +0 -0
- package/tmp/quote-details.pdf +0 -0
package/README.md
CHANGED
|
@@ -1,16 +1,38 @@
|
|
|
1
1
|
# av6-pdf-engine
|
|
2
2
|
|
|
3
3
|
`av6-pdf-engine` is a JSON-driven PDF layout engine built on top of [PDFKit](https://github.com/foliojs/pdfkit).
|
|
4
|
-
Describe your document once
|
|
4
|
+
Describe your document once and render it to a file or in-memory Buffer from any Node.js service.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
---
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
## Table of contents
|
|
9
|
+
|
|
10
|
+
1. [Installation](#installation)
|
|
11
|
+
2. [Quick start](#quick-start)
|
|
12
|
+
3. [Document definition](#document-definition)
|
|
13
|
+
4. [Styles](#styles)
|
|
14
|
+
5. [Header & footer](#header--footer)
|
|
15
|
+
6. [Page background & watermark](#page-background--watermark)
|
|
16
|
+
7. [Box model](#box-model)
|
|
17
|
+
8. [Block catalog](#block-catalog)
|
|
18
|
+
- [text](#text)
|
|
19
|
+
- [image](#image)
|
|
20
|
+
- [qr](#qr)
|
|
21
|
+
- [barcode](#barcode)
|
|
22
|
+
- [line](#line)
|
|
23
|
+
- [shape](#shape)
|
|
24
|
+
- [columns](#columns)
|
|
25
|
+
- [table](#table)
|
|
26
|
+
- [keyValueGrid](#keyvaluegrid)
|
|
27
|
+
- [list](#list)
|
|
28
|
+
- [pageBreak](#pagebreak)
|
|
29
|
+
- [signature](#signature)
|
|
30
|
+
9. [Asset handling](#asset-handling)
|
|
31
|
+
10. [Rendering targets](#rendering-targets)
|
|
32
|
+
11. [Built-in fonts](#built-in-fonts)
|
|
33
|
+
12. [Engine behaviour notes](#engine-behaviour-notes)
|
|
34
|
+
|
|
35
|
+
---
|
|
14
36
|
|
|
15
37
|
## Installation
|
|
16
38
|
|
|
@@ -22,6 +44,8 @@ pnpm add av6-pdf-engine
|
|
|
22
44
|
yarn add av6-pdf-engine
|
|
23
45
|
```
|
|
24
46
|
|
|
47
|
+
---
|
|
48
|
+
|
|
25
49
|
## Quick start
|
|
26
50
|
|
|
27
51
|
```ts
|
|
@@ -29,112 +53,686 @@ import { renderCustomPdfToBuffer, renderCustomPdf, CustomDocDefinition } from "a
|
|
|
29
53
|
|
|
30
54
|
const doc: CustomDocDefinition = {
|
|
31
55
|
pageSize: "A4",
|
|
32
|
-
margins: { top:
|
|
33
|
-
fonts: [{ name: "Inter", src: "./assets/Inter-Regular.ttf" }],
|
|
56
|
+
margins: { top: 70, right: 40, bottom: 60, left: 40 },
|
|
34
57
|
styles: {
|
|
35
|
-
heading: {
|
|
36
|
-
label:
|
|
58
|
+
heading: { fontSize: 16, bold: true, color: "#0B4A6F" },
|
|
59
|
+
label: { fontSize: 8, bold: true, color: "#6B7280" },
|
|
37
60
|
},
|
|
38
61
|
header: {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
62
|
+
paddingLeft: 40,
|
|
63
|
+
paddingTop: 16,
|
|
64
|
+
backgroundBlocks: [
|
|
65
|
+
{ type: "shape", shape: "rect", fillColor: "#0B4A6F", height: 70 },
|
|
66
|
+
],
|
|
43
67
|
blocks: [
|
|
44
|
-
{
|
|
45
|
-
type: "text",
|
|
46
|
-
text: `Page ${page} / ${Math.round(size.height)}`,
|
|
47
|
-
align: "center",
|
|
48
|
-
},
|
|
68
|
+
{ type: "text", text: "ACME Corp — Invoice #1234", fontSize: 13, bold: true, color: "#FFFFFF" },
|
|
49
69
|
],
|
|
70
|
+
},
|
|
71
|
+
footer: (page, size) => ({
|
|
72
|
+
blocks: [{ type: "text", text: `Page ${page}`, align: "center", fontSize: 8 }],
|
|
50
73
|
}),
|
|
51
|
-
|
|
52
|
-
watermark: { text: "ACME CONFIDENTIAL", mode: "exceptFirst", opacity: 0.1 },
|
|
74
|
+
watermark: { text: "DRAFT", mode: "all", opacity: 0.07 },
|
|
53
75
|
content: [
|
|
76
|
+
{ type: "text", text: "Hello, PDF!", style: "heading", marginTop: 16 },
|
|
77
|
+
{ type: "line", lineWidth: 1, color: "#0B4A6F", marginBottom: 12 },
|
|
54
78
|
{
|
|
55
|
-
type: "
|
|
56
|
-
widths: [
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
type: "keyValueGrid",
|
|
62
|
-
columns: [
|
|
63
|
-
[
|
|
64
|
-
{ key: "Issued", value: "2025-12-01" },
|
|
65
|
-
{ key: "Due", value: "2025-12-15" },
|
|
66
|
-
],
|
|
67
|
-
],
|
|
68
|
-
keyStyle: "label",
|
|
69
|
-
},
|
|
70
|
-
],
|
|
71
|
-
[
|
|
72
|
-
{
|
|
73
|
-
type: "table",
|
|
74
|
-
widths: ["*", 80, 80],
|
|
75
|
-
headerRows: 1,
|
|
76
|
-
body: [
|
|
77
|
-
[
|
|
78
|
-
{ text: "Item", style: "label" },
|
|
79
|
-
{ text: "Qty", style: "label", align: "right" },
|
|
80
|
-
{ text: "Total", style: "label", align: "right" },
|
|
81
|
-
],
|
|
82
|
-
[{ text: "Consulting" }, { text: "12", align: "right" }, { text: "$3,000", align: "right" }],
|
|
83
|
-
],
|
|
84
|
-
},
|
|
85
|
-
],
|
|
79
|
+
type: "table",
|
|
80
|
+
widths: ["*", 80, 80],
|
|
81
|
+
headerRows: 1,
|
|
82
|
+
body: [
|
|
83
|
+
[{ text: "Item", bold: true }, { text: "Qty", bold: true, align: "right" }, { text: "Total", bold: true, align: "right" }],
|
|
84
|
+
[{ text: "Consulting" }, { text: "12", align: "right" }, { text: "$3,000", align: "right" }],
|
|
86
85
|
],
|
|
87
86
|
},
|
|
88
|
-
{ type: "
|
|
89
|
-
{ type: "barcode", value: "123456789012", bcType: "EAN13", width: 200 },
|
|
90
|
-
{ type: "signature", height: 100, label: "Authorized signature" },
|
|
87
|
+
{ type: "signature", height: 80, label: "Authorized signature" },
|
|
91
88
|
],
|
|
92
89
|
};
|
|
93
90
|
|
|
94
|
-
// Write
|
|
91
|
+
// Write to disk
|
|
95
92
|
await renderCustomPdf(doc, "./tmp/invoice.pdf");
|
|
96
93
|
|
|
97
|
-
// Or capture
|
|
98
|
-
const
|
|
94
|
+
// Or capture a Buffer (HTTP responses, cloud storage, etc.)
|
|
95
|
+
const buf = await renderCustomPdfToBuffer(doc);
|
|
99
96
|
```
|
|
100
97
|
|
|
98
|
+
---
|
|
99
|
+
|
|
101
100
|
## Document definition
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
102
|
+
`CustomDocDefinition` is the root object passed to both render functions.
|
|
103
|
+
|
|
104
|
+
| Property | Type | Required | Description |
|
|
105
|
+
|---|---|---|---|
|
|
106
|
+
| `pageSize` | `string \| [width, height]` | No | Any PDFKit page size (`"A4"`, `"LETTER"`, `"A5"`, etc.) or a `[w, h]` tuple in points. Defaults to `"A4"`. |
|
|
107
|
+
| `pageOrientation` | `"portrait" \| "landscape"` | No | Defaults to portrait. |
|
|
108
|
+
| `margins` | `{ top, right, bottom, left }` | **Yes** | All four margins in points. `top` doubles as the header band height; `bottom` doubles as the footer band height. |
|
|
109
|
+
| `fonts` | `FontRegistration[]` | No | Custom TTF/OTF fonts to register before rendering. See [Custom fonts](#custom-fonts). |
|
|
110
|
+
| `styles` | `Record<string, StyleDef>` | No | Named style map. Any block with a `style` property references an entry here. |
|
|
111
|
+
| `header` | `HeaderDef` | No | Rendered inside the top margin band on every page. |
|
|
112
|
+
| `footer` | `FooterInput` | No | Rendered inside the bottom margin band. Accepts a `FooterDef`, a `Block`, a `Block[]`, or a callback. |
|
|
113
|
+
| `pageBackground` | `PageBackgroundDef` | No | Image painted behind all content on every page. |
|
|
114
|
+
| `watermark` | `WatermarkDef` | No | Diagonal text overlay. |
|
|
115
|
+
| `content` | `Block[]` | **Yes** | Ordered list of blocks rendered top-to-bottom with automatic pagination. |
|
|
116
|
+
| `defaultImage` | `string \| Buffer` | No | Fallback image used whenever an `image`, `qr`, or `barcode` block fails to load its source. |
|
|
117
|
+
|
|
118
|
+
### Custom fonts
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
fonts: [
|
|
122
|
+
{ name: "Inter", src: "./assets/Inter-Regular.ttf" },
|
|
123
|
+
{ name: "Inter-Bold", src: "./assets/Inter-Bold.ttf" },
|
|
124
|
+
],
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Once registered, use the name in any `font` property: `{ type: "text", font: "Inter", text: "Hello" }`.
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Styles
|
|
132
|
+
|
|
133
|
+
`StyleDef` is a reusable object referenced by name via the `style` property on any text-bearing block or table cell.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
styles: {
|
|
137
|
+
h1: { fontSize: 20, bold: true, color: "#0B4A6F" },
|
|
138
|
+
label: { fontSize: 8, bold: true, color: "#6B7280" },
|
|
139
|
+
mono: { font: "Courier", fontSize: 9 },
|
|
140
|
+
},
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
| Property | Type | Description |
|
|
144
|
+
|---|---|---|
|
|
145
|
+
| `fontSize` | `number` | Font size in points. |
|
|
146
|
+
| `bold` | `boolean` | Bold weight. |
|
|
147
|
+
| `italic` | `boolean` | Italic style. |
|
|
148
|
+
| `underline` | `boolean` | Underline decoration. |
|
|
149
|
+
| `strike` | `boolean` | Strikethrough decoration. |
|
|
150
|
+
| `color` | `string` | Text fill color (hex, rgb, named). |
|
|
151
|
+
| `align` | `"left" \| "center" \| "right" \| "justify"` | Horizontal alignment. |
|
|
152
|
+
| `lineGap` | `number` | Extra spacing between lines in points. |
|
|
153
|
+
| `font` | `FontName` | Built-in or registered font name. |
|
|
154
|
+
| `link` | `string` | URL to attach to the text as a hyperlink. |
|
|
155
|
+
|
|
156
|
+
A block can reference multiple styles as an array; properties are merged left-to-right, with the block's own properties taking precedence:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
{ type: "text", text: "Warning!", style: ["label", "highlight"] }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Header & footer
|
|
165
|
+
|
|
166
|
+
### Header (`HeaderDef`)
|
|
167
|
+
|
|
168
|
+
The header is rendered inside the `top` margin band on **every** page (including pages added internally by PDFKit when long text overflows).
|
|
169
|
+
|
|
170
|
+
| Property | Type | Description |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| `blocks` | `Block[]` | **Optional.** Foreground blocks rendered inside the band. Omit for a background-only header. |
|
|
173
|
+
| `backgroundBlocks` | `Block[]` | Blocks rendered behind `blocks` — useful for shapes, waves, or filled rectangles. |
|
|
174
|
+
| `backgroundColor` | `string` | Solid fill behind the entire band. |
|
|
175
|
+
| `backgroundImage` | `string \| Buffer` | Image painted to cover the entire band. |
|
|
176
|
+
| `backgroundOpacity` | `number` | Opacity for `backgroundColor` / `backgroundImage` (0–1). |
|
|
177
|
+
| `visible` | `boolean` | Set `false` to suppress the header entirely. |
|
|
178
|
+
| `marginTop/Bottom/Left/Right` | `number` | Outer spacing around the band content. |
|
|
179
|
+
| `padding / paddingTop/Right/Bottom/Left` | `number \| BoxSpacing` | Inner spacing before the foreground blocks. |
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
header: {
|
|
183
|
+
paddingTop: 14,
|
|
184
|
+
paddingLeft: 40,
|
|
185
|
+
backgroundBlocks: [
|
|
186
|
+
{ type: "shape", shape: "rect", fillColor: "#0B4A6F", height: 70 },
|
|
187
|
+
],
|
|
188
|
+
blocks: [
|
|
189
|
+
{ type: "text", text: "My Company", bold: true, color: "#FFFFFF" },
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Footer (`FooterInput`)
|
|
195
|
+
|
|
196
|
+
The footer accepts four different shapes:
|
|
197
|
+
|
|
198
|
+
**1. FooterDef** — same options as `HeaderDef`:
|
|
199
|
+
```ts
|
|
200
|
+
footer: {
|
|
201
|
+
blocks: [{ type: "text", text: "© 2025 Acme Corp", align: "center", fontSize: 8 }],
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**2. Single Block:**
|
|
206
|
+
```ts
|
|
207
|
+
footer: { type: "text", text: "Confidential", align: "right", fontSize: 8 }
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**3. Block array:**
|
|
211
|
+
```ts
|
|
212
|
+
footer: [
|
|
213
|
+
{ type: "line", lineWidth: 0.5, color: "#D1D5DB", marginBottom: 4 },
|
|
214
|
+
{ type: "text", text: "Page footer text", fontSize: 8 },
|
|
215
|
+
]
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**4. Callback** — receives `(pageNumber, pageSize)`:
|
|
219
|
+
```ts
|
|
220
|
+
footer: (page, size) => ({
|
|
221
|
+
blocks: [
|
|
222
|
+
{ type: "text", text: `Page ${page}`, align: "center", fontSize: 8 },
|
|
223
|
+
],
|
|
224
|
+
}),
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The callback form is the only way to include dynamic per-page data like page numbers.
|
|
228
|
+
|
|
229
|
+
---
|
|
230
|
+
|
|
231
|
+
## Page background & watermark
|
|
232
|
+
|
|
233
|
+
### Page background
|
|
234
|
+
|
|
235
|
+
Painted before the header and content on every new page.
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
pageBackground: {
|
|
239
|
+
src: "./assets/letterhead.png", // path, Buffer, or HTTPS URL
|
|
240
|
+
opacity: 0.05,
|
|
241
|
+
},
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Watermark
|
|
245
|
+
|
|
246
|
+
Diagonal text overlay drawn on top of content.
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
watermark: {
|
|
250
|
+
text: "CONFIDENTIAL",
|
|
251
|
+
mode: "exceptFirst", // see modes below
|
|
252
|
+
opacity: 0.07,
|
|
253
|
+
fontSize: 55,
|
|
254
|
+
color: "gray",
|
|
255
|
+
angle: 45,
|
|
256
|
+
},
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
| `mode` | Pages that receive the watermark |
|
|
260
|
+
|---|---|
|
|
261
|
+
| `"all"` (default) | Every page |
|
|
262
|
+
| `"first"` | First page only |
|
|
263
|
+
| `"last"` | Last page only |
|
|
264
|
+
| `"firstLast"` | First and last pages |
|
|
265
|
+
| `"exceptFirst"` | All pages except the first |
|
|
266
|
+
| `"exceptLast"` | All pages except the last |
|
|
267
|
+
| `"exceptFirstLast"` | All pages except first and last |
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Box model
|
|
272
|
+
|
|
273
|
+
Every block type inherits `BaseBlock`, which adds a box model layer on top of the block's own rendering. This lets you attach a background, an image, or child blocks behind any block, and control inner spacing with padding.
|
|
274
|
+
|
|
275
|
+
| Property | Type | Description |
|
|
276
|
+
|---|---|---|
|
|
277
|
+
| `visible` | `boolean` | `false` skips rendering entirely (block is also excluded from height measurement). |
|
|
278
|
+
| `backgroundColor` | `string` | Filled rectangle drawn behind the block's content area. |
|
|
279
|
+
| `backgroundImage` | `string \| Buffer` | Image stretched to cover the content area. |
|
|
280
|
+
| `backgroundOpacity` | `number` | Opacity applied to `backgroundColor` / `backgroundImage`. |
|
|
281
|
+
| `backgroundBlocks` | `Block[]` | Arbitrary blocks rendered in a separate background layer before the main content. |
|
|
282
|
+
| `padding` | `number \| { top, right, bottom, left }` | Uniform or per-side inner spacing. |
|
|
283
|
+
| `paddingTop/Right/Bottom/Left` | `number` | Individual padding overrides. |
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
{
|
|
287
|
+
type: "text",
|
|
288
|
+
text: "Highlighted note",
|
|
289
|
+
fontSize: 9,
|
|
290
|
+
backgroundColor: "#EFF6FF",
|
|
291
|
+
padding: 10,
|
|
292
|
+
marginTop: 8,
|
|
293
|
+
marginBottom: 8,
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
---
|
|
111
298
|
|
|
112
299
|
## Block catalog
|
|
113
300
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
301
|
+
All blocks accept the `BaseBlock` properties listed in [Box model](#box-model) in addition to their own properties.
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
### text
|
|
306
|
+
|
|
307
|
+
Renders a paragraph of text.
|
|
308
|
+
|
|
309
|
+
| Property | Type | Default | Description |
|
|
310
|
+
|---|---|---|---|
|
|
311
|
+
| `text` | `string` | — | **Required.** Content to render. |
|
|
312
|
+
| `fontSize` | `number` | `10` | Size in points. |
|
|
313
|
+
| `lineGap` | `number` | `4` | Extra vertical space between lines and after the block. |
|
|
314
|
+
| `align` | `Alignment` | `"left"` | `"left"`, `"center"`, `"right"`, `"justify"`. |
|
|
315
|
+
| `bold` | `boolean` | `false` | |
|
|
316
|
+
| `italic` | `boolean` | `false` | |
|
|
317
|
+
| `underline` | `boolean` | `false` | |
|
|
318
|
+
| `strike` | `boolean` | `false` | Strikethrough. |
|
|
319
|
+
| `link` | `string` | — | URL hyperlink attached to the text. |
|
|
320
|
+
| `color` | `string` | `"black"` | Fill color. |
|
|
321
|
+
| `font` | `FontName` | auto | Built-in or registered font. Auto-selects bold/italic variant when those flags are set. |
|
|
322
|
+
| `style` | `string \| string[]` | — | Named style reference(s) from the `styles` map. |
|
|
323
|
+
| `marginTop` | `number` | `0` | Space above the block. |
|
|
324
|
+
| `marginBottom` | `number` | `0` | Space below the block. |
|
|
325
|
+
| `marginLeft` | `number` | `0` | Left indent (reduces available width). |
|
|
326
|
+
| `marginRight` | `number` | `0` | Right indent (reduces available width). |
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
{ type: "text", text: "Hello world", bold: true, marginTop: 12, marginBottom: 8, align: "center" }
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
### image
|
|
335
|
+
|
|
336
|
+
Renders a raster image (PNG, JPEG).
|
|
337
|
+
|
|
338
|
+
| Property | Type | Default | Description |
|
|
339
|
+
|---|---|---|---|
|
|
340
|
+
| `src` | `string \| Buffer` | — | File path, HTTPS URL, or Buffer. Falls back to `defaultImage` on error. |
|
|
341
|
+
| `width` | `number` | `80` | Render width in points. |
|
|
342
|
+
| `height` | `number` | `50` | Render height in points. |
|
|
343
|
+
| `align` | `Alignment` | `"left"` | Horizontal alignment within the available width. |
|
|
344
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
{ type: "image", src: "https://example.com/logo.png", width: 120, height: 60, align: "right" }
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
### qr
|
|
353
|
+
|
|
354
|
+
Generates a QR code via `qrcode` and renders it as an image.
|
|
355
|
+
|
|
356
|
+
| Property | Type | Default | Description |
|
|
357
|
+
|---|---|---|---|
|
|
358
|
+
| `value` | `string` | — | Data to encode. |
|
|
359
|
+
| `size` | `number` | `80` | Width and height of the rendered square in points. |
|
|
360
|
+
| `qrVersion` | `number` | auto | QR version (1–40). |
|
|
361
|
+
| `errorCorrectionLevel` | `"L" \| "M" \| "Q" \| "H"` | `"M"` | Error correction level. |
|
|
362
|
+
| `align` | `Alignment` | `"left"` | |
|
|
363
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
{ type: "qr", value: "https://acme.example.com/pay/1234", size: 80, errorCorrectionLevel: "H" }
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
### barcode
|
|
124
372
|
|
|
125
|
-
|
|
373
|
+
Generates a 1D barcode via `bwip-js`.
|
|
126
374
|
|
|
127
|
-
|
|
375
|
+
| Property | Type | Default | Description |
|
|
376
|
+
|---|---|---|---|
|
|
377
|
+
| `value` | `string` | — | Data to encode. |
|
|
378
|
+
| `bcType` | `BarcodeType` | `"CODE128"` | `"CODE128"`, `"EAN13"`, `"CODE39"`, `"ITF"`, `"CODE93"`. |
|
|
379
|
+
| `width` | `number` | auto | Render width in points. |
|
|
380
|
+
| `height` | `number` | `40` | Render height in points. |
|
|
381
|
+
| `align` | `Alignment` | `"left"` | |
|
|
382
|
+
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | |
|
|
383
|
+
| `rotate` | `number` | `0` | Rotation in degrees. |
|
|
384
|
+
| `scale` | `number` | — | bwip-js scale factor. |
|
|
385
|
+
| `barHeight` | `number` | — | Bar height hint passed to bwip-js. |
|
|
386
|
+
| `includetext` | `boolean` | `false` | Render the human-readable text below the bars. |
|
|
387
|
+
| `textalign` | `"left" \| "center" \| "right"` | `"center"` | Alignment of the human-readable text. |
|
|
388
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
{ type: "barcode", value: "5901234123457", bcType: "EAN13", width: 160, height: 60, includetext: true }
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
---
|
|
395
|
+
|
|
396
|
+
### line
|
|
397
|
+
|
|
398
|
+
Draws a horizontal rule.
|
|
399
|
+
|
|
400
|
+
| Property | Type | Default | Description |
|
|
401
|
+
|---|---|---|---|
|
|
402
|
+
| `lineWidth` | `number` | `1` | Stroke width in points. |
|
|
403
|
+
| `color` | `string` | black | Stroke color. |
|
|
404
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
405
|
+
|
|
406
|
+
```ts
|
|
407
|
+
{ type: "line", lineWidth: 0.5, color: "#E5E7EB", marginTop: 8, marginBottom: 8 }
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
---
|
|
411
|
+
|
|
412
|
+
### shape
|
|
413
|
+
|
|
414
|
+
Draws a vector shape. All shapes share the same property set; only the `shape` type determines which properties are used.
|
|
415
|
+
|
|
416
|
+
| Property | Type | Default | Description |
|
|
417
|
+
|---|---|---|---|
|
|
418
|
+
| `shape` | `ShapeType` | — | **Required.** One of `"rect"`, `"roundedRect"`, `"circle"`, `"ellipse"`, `"line"`, `"polygon"`, `"path"`. |
|
|
419
|
+
| `width` | `number` | inner width | Shape width in points. |
|
|
420
|
+
| `height` | `number` | `lineWidth` | Shape height in points. |
|
|
421
|
+
| `radius` | `number` | `6` | Corner radius (`roundedRect`) or circle radius (`circle`). |
|
|
422
|
+
| `points` | `{ x, y }[]` | — | Vertices for `"polygon"` (relative to the shape origin). |
|
|
423
|
+
| `path` | `string` | — | SVG path data for `"path"`. Translated to the shape's `(x, y)` position. |
|
|
424
|
+
| `fillColor` | `string` | — | Fill color. |
|
|
425
|
+
| `strokeColor` | `string` | — | Stroke color. |
|
|
426
|
+
| `lineWidth` | `number` | `1` | Stroke width. |
|
|
427
|
+
| `opacity` | `number` | `1` | Opacity (0–1). |
|
|
428
|
+
| `align` | `Alignment` | `"left"` | Horizontal alignment of the shape within the available width. |
|
|
429
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
// Filled navy rectangle
|
|
433
|
+
{ type: "shape", shape: "rect", fillColor: "#0B4A6F", height: 4 }
|
|
434
|
+
|
|
435
|
+
// Rounded badge
|
|
436
|
+
{ type: "shape", shape: "roundedRect", width: 60, height: 24, radius: 12, fillColor: "#D1FAE5", strokeColor: "#047857" }
|
|
437
|
+
|
|
438
|
+
// Triangle polygon
|
|
439
|
+
{
|
|
440
|
+
type: "shape", shape: "polygon",
|
|
441
|
+
width: 50, height: 50,
|
|
442
|
+
strokeColor: "#0B4A6F", lineWidth: 1.5,
|
|
443
|
+
points: [{ x: 25, y: 0 }, { x: 50, y: 50 }, { x: 0, y: 50 }],
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Custom SVG path (e.g. a wave)
|
|
447
|
+
{
|
|
448
|
+
type: "shape", shape: "path",
|
|
449
|
+
height: 40, fillColor: "#0B4A6F",
|
|
450
|
+
path: "M 0 0 L 200 0 L 200 20 C 150 40 50 10 0 30 Z",
|
|
451
|
+
}
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
---
|
|
455
|
+
|
|
456
|
+
### columns
|
|
457
|
+
|
|
458
|
+
Lays out child block arrays side-by-side.
|
|
459
|
+
|
|
460
|
+
| Property | Type | Default | Description |
|
|
461
|
+
|---|---|---|---|
|
|
462
|
+
| `columns` | `Block[][]` | — | **Required.** Each element is the block array for one column. |
|
|
463
|
+
| `widths` | `(number \| "*")[]` | equal split | Per-column widths. `"*"` means "take remaining space". |
|
|
464
|
+
| `gap` | `number` | `20` | Fixed gap between columns (used in `"fixedGap"` mode). |
|
|
465
|
+
| `mode` | `"fixedGap" \| "spaceBetween"` | `"fixedGap"` | `"spaceBetween"` distributes all remaining space between columns. |
|
|
466
|
+
| `keepTogether` | `boolean` | `false` | Measure the total columns height before rendering; if it does not fit, trigger a page break first. |
|
|
467
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
{
|
|
471
|
+
type: "columns",
|
|
472
|
+
widths: [150, "*"],
|
|
473
|
+
gap: 24,
|
|
474
|
+
columns: [
|
|
475
|
+
[{ type: "text", text: "Left column" }],
|
|
476
|
+
[{ type: "text", text: "Right column" }],
|
|
477
|
+
],
|
|
478
|
+
}
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
---
|
|
482
|
+
|
|
483
|
+
### table
|
|
484
|
+
|
|
485
|
+
Renders a grid with optional header rows, borders, and per-cell styling.
|
|
486
|
+
|
|
487
|
+
**TableBlock properties:**
|
|
488
|
+
|
|
489
|
+
| Property | Type | Default | Description |
|
|
490
|
+
|---|---|---|---|
|
|
491
|
+
| `widths` | `(number \| "*")[]` | — | **Required.** Per-column widths. |
|
|
492
|
+
| `body` | `TableCell[][]` | — | **Required.** 2-D array of cells. |
|
|
493
|
+
| `headerRows` | `number` | `0` | Number of leading rows treated as headers (styled differently and repeated on new pages). |
|
|
494
|
+
| `lineGap` | `number` | `2` | Extra vertical gap inside cells. |
|
|
495
|
+
| `layout.border` | `"all" \| "none"` | `"all"` | Master border switch. |
|
|
496
|
+
| `layout.hLineColor` | `string` | | Horizontal line color. |
|
|
497
|
+
| `layout.vLineColor` | `string` | | Vertical line color. |
|
|
498
|
+
| `layout.borderWidth` | `number` | `1` | Default border width. |
|
|
499
|
+
| `layout.borderDash` | `number[]` | | Dash pattern, e.g. `[2, 2]`. |
|
|
500
|
+
| `layout.rowStyles` | `Record<rowIndex, TableRowStyle>` | | Per-row `fillColor` and `border` overrides. |
|
|
501
|
+
| `layout.columnStyles` | `Record<colIndex, TableColumnStyle>` | | Per-column `fillColor` and `border` overrides. |
|
|
502
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
503
|
+
|
|
504
|
+
**TableCell properties:**
|
|
505
|
+
|
|
506
|
+
| Property | Type | Description |
|
|
507
|
+
|---|---|---|
|
|
508
|
+
| `text` | `string` | Cell text content. |
|
|
509
|
+
| `blocks` | `Block[]` | Rich block content inside the cell (replaces `text`). |
|
|
510
|
+
| `bold / italic / underline / strike` | `boolean` | Typography flags. |
|
|
511
|
+
| `fontSize` | `number` | |
|
|
512
|
+
| `font` | `FontName` | |
|
|
513
|
+
| `color` | `string` | Text color. |
|
|
514
|
+
| `fillColor` | `string` | Cell background. |
|
|
515
|
+
| `align` | `Alignment` | |
|
|
516
|
+
| `style` | `StyleRef` | Named style reference. |
|
|
517
|
+
| `colSpan` | `number` | Span across N columns. |
|
|
518
|
+
| `rowSpan` | `number` | Span across N rows. |
|
|
519
|
+
| `paddingTop/Right/Bottom/Left` | `number` | Cell inner padding. |
|
|
520
|
+
| `border` | `TableCellBorder` | Per-cell border override. |
|
|
521
|
+
|
|
522
|
+
```ts
|
|
523
|
+
{
|
|
524
|
+
type: "table",
|
|
525
|
+
widths: ["*", 80, 80],
|
|
526
|
+
headerRows: 1,
|
|
527
|
+
layout: { hLineColor: "#E5E7EB", rowStyles: { 0: { fillColor: "#0B4A6F" } } },
|
|
528
|
+
body: [
|
|
529
|
+
[{ text: "Item", bold: true, color: "#FFFFFF" }, { text: "Qty", bold: true, color: "#FFFFFF", align: "right" }, { text: "Total", bold: true, color: "#FFFFFF", align: "right" }],
|
|
530
|
+
[{ text: "Consulting" }, { text: "12", align: "right" }, { text: "$3,000", align: "right" }],
|
|
531
|
+
],
|
|
532
|
+
}
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
---
|
|
536
|
+
|
|
537
|
+
### keyValueGrid
|
|
538
|
+
|
|
539
|
+
Two-column key/value pairs with flexible orientation and styling.
|
|
540
|
+
|
|
541
|
+
| Property | Type | Default | Description |
|
|
542
|
+
|---|---|---|---|
|
|
543
|
+
| `columns` | `KeyValueItem[][]` | — | **Required.** Each sub-array is one grid column of items. |
|
|
544
|
+
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | `"horizontal"`: key and value on the same row. `"vertical"`: key stacked above value. |
|
|
545
|
+
| `keyWidth` | `number \| "*"` | `80` | Width reserved for the key in horizontal mode. `"*"` uses 35 % of the column. |
|
|
546
|
+
| `columnWidths` | `(number \| "*")[]` | equal | Widths of each grid column. |
|
|
547
|
+
| `separator` | `string` | `""` | Text inserted between key and value (e.g. `":"`). |
|
|
548
|
+
| `rowGap` | `number` | `4` | Vertical gap between rows. |
|
|
549
|
+
| `verticalKeyValueGap` | `number` | `2` | Gap between key and value in vertical orientation. |
|
|
550
|
+
| `keyStyle / valueStyle` | `StyleRef` | | Default named style for all keys / values. |
|
|
551
|
+
| `keyTextStyle / valueTextStyle` | `InlineTextStyle` | | Inline style overrides for keys / values. |
|
|
552
|
+
| `keyAlign / valueAlign` | `Alignment` | | |
|
|
553
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
554
|
+
|
|
555
|
+
**KeyValueItem properties:**
|
|
556
|
+
|
|
557
|
+
| Property | Type | Description |
|
|
558
|
+
|---|---|---|
|
|
559
|
+
| `key` | `string` | Label text. |
|
|
560
|
+
| `value` | `string` | Value text. |
|
|
561
|
+
| `keyStyle / valueStyle` | `StyleRef` | Per-item style overrides. |
|
|
562
|
+
| `keyTextStyle / valueTextStyle` | `InlineTextStyle` | Per-item inline style overrides. |
|
|
563
|
+
| `keyAlign / valueAlign` | `Alignment` | Per-item alignment. |
|
|
564
|
+
|
|
565
|
+
```ts
|
|
566
|
+
{
|
|
567
|
+
type: "keyValueGrid",
|
|
568
|
+
orientation: "horizontal",
|
|
569
|
+
keyWidth: 100,
|
|
570
|
+
separator: ":",
|
|
571
|
+
keyStyle: "label",
|
|
572
|
+
rowGap: 5,
|
|
573
|
+
columns: [
|
|
574
|
+
[
|
|
575
|
+
{ key: "Invoice", value: "#INV-0042" },
|
|
576
|
+
{ key: "Due", value: "2025-12-31" },
|
|
577
|
+
],
|
|
578
|
+
[
|
|
579
|
+
{ key: "Client", value: "Acme Corp" },
|
|
580
|
+
{ key: "Total", value: "$3,000.00", valueStyle: "highlight" },
|
|
581
|
+
],
|
|
582
|
+
],
|
|
583
|
+
}
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
---
|
|
587
|
+
|
|
588
|
+
### list
|
|
589
|
+
|
|
590
|
+
Bullet or numbered lists with hanging-indent layout. Each item is a string or an object with per-item typography overrides. Long lists paginate item-by-item.
|
|
591
|
+
|
|
592
|
+
| Property | Type | Default | Description |
|
|
593
|
+
|---|---|---|---|
|
|
594
|
+
| `items` | `(string \| ListItem)[]` | — | **Required.** List entries. |
|
|
595
|
+
| `listStyle` | `"bullet" \| "number"` | `"bullet"` | Marker style. |
|
|
596
|
+
| `bullet` | `string` | `"•"` | Custom bullet character (bullet lists only). |
|
|
597
|
+
| `start` | `number` | `1` | Starting number (numbered lists only). |
|
|
598
|
+
| `markerWidth` | `number` | auto | Fixed marker column width; otherwise computed from widest marker. |
|
|
599
|
+
| `markerGap` | `number` | `6` | Space between marker and text in points. |
|
|
600
|
+
| `itemGap` | `number` | `4` | Vertical gap between items in points. |
|
|
601
|
+
| `fontSize` | `number` | `10` | Default font size for all items. |
|
|
602
|
+
| `lineGap` | `number` | `4` | Default line gap inside items. |
|
|
603
|
+
| `color` | `string` | black | Default text color. |
|
|
604
|
+
| `style` | `StyleRef` | | Default named style for all items. |
|
|
605
|
+
| `marginTop/Bottom/Left/Right` | `number` | `0` | |
|
|
606
|
+
|
|
607
|
+
**ListItem properties** (when `items` entry is an object):
|
|
608
|
+
|
|
609
|
+
| Property | Type | Description |
|
|
610
|
+
|---|---|---|
|
|
611
|
+
| `text` | `string` | **Required.** Item body text. |
|
|
612
|
+
| `bold / italic / underline / strike` | `boolean` | Typography flags. |
|
|
613
|
+
| `link` | `string` | Hyperlink URL. |
|
|
614
|
+
| `color` | `string` | Text color override. |
|
|
615
|
+
| `fontSize` | `number` | Font size override. |
|
|
616
|
+
| `font` | `FontName` | Font override. |
|
|
617
|
+
| `lineGap` | `number` | Line gap override. |
|
|
618
|
+
| `style` | `StyleRef` | Named style override. |
|
|
619
|
+
|
|
620
|
+
```ts
|
|
621
|
+
{
|
|
622
|
+
type: "list",
|
|
623
|
+
listStyle: "bullet",
|
|
624
|
+
style: "label",
|
|
625
|
+
items: [
|
|
626
|
+
"Revenue grew 18% year-on-year",
|
|
627
|
+
{ text: "Audited by Grant & Partners", bold: true, link: "https://example.com" },
|
|
628
|
+
],
|
|
629
|
+
marginBottom: 12,
|
|
630
|
+
},
|
|
631
|
+
{
|
|
632
|
+
type: "list",
|
|
633
|
+
listStyle: "number",
|
|
634
|
+
start: 1,
|
|
635
|
+
fontSize: 9,
|
|
636
|
+
items: ["Download report", "Review appendix", "Sign authorization"],
|
|
637
|
+
},
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
---
|
|
641
|
+
|
|
642
|
+
### pageBreak
|
|
643
|
+
|
|
644
|
+
Immediately finishes the current page and starts a new one. No additional properties.
|
|
645
|
+
|
|
646
|
+
```ts
|
|
647
|
+
{ type: "pageBreak" }
|
|
648
|
+
```
|
|
649
|
+
|
|
650
|
+
---
|
|
651
|
+
|
|
652
|
+
### signature
|
|
653
|
+
|
|
654
|
+
Reserves a block of vertical space for a handwritten or digital signature, optionally rendering label text and/or inner blocks below the signing area.
|
|
655
|
+
|
|
656
|
+
| Property | Type | Default | Description |
|
|
657
|
+
|---|---|---|---|
|
|
658
|
+
| `height` | `number` | — | **Required.** Total reserved height in points. |
|
|
659
|
+
| `width` | `number` | inner width | Width of the signing area. |
|
|
660
|
+
| `label` | `string` | — | Caption printed below the signing line (e.g. `"Authorized signature"`). |
|
|
661
|
+
| `image` | `string` | — | Path to a signature image pre-filled in the space. |
|
|
662
|
+
| `blocks` | `Block[]` | — | Custom blocks rendered inside the reserved area. |
|
|
663
|
+
| `marginLeft / marginRight` | `number` | `0` | |
|
|
664
|
+
|
|
665
|
+
```ts
|
|
666
|
+
{ type: "signature", height: 80, label: "Chief Executive Officer" }
|
|
667
|
+
```
|
|
668
|
+
|
|
669
|
+
---
|
|
670
|
+
|
|
671
|
+
## Asset handling
|
|
672
|
+
|
|
673
|
+
- **Images, QR codes, and barcodes** accept local file paths, `Buffer` values, or `https://` URLs.
|
|
674
|
+
Remote assets are fetched and cached as `Buffer`s before rendering begins (parallel per-asset).
|
|
675
|
+
- Nested assets in `columns`, `signature.blocks`, `header.blocks`, `footer.blocks`, etc. are traversed automatically.
|
|
676
|
+
- If an image source fails to load and `defaultImage` is set on the document, the engine renders the fallback instead.
|
|
677
|
+
- **Performance tip:** for frequently rendered documents, pre-download and pass `Buffer` values directly to avoid network round-trips.
|
|
678
|
+
|
|
679
|
+
---
|
|
680
|
+
|
|
681
|
+
## Rendering targets
|
|
682
|
+
|
|
683
|
+
```ts
|
|
684
|
+
import { renderCustomPdf, renderCustomPdfToBuffer } from "av6-pdf-engine";
|
|
685
|
+
|
|
686
|
+
// Write to a file path
|
|
687
|
+
await renderCustomPdf(def, "./output/doc.pdf");
|
|
688
|
+
|
|
689
|
+
// Get a Buffer (for HTTP, S3, etc.)
|
|
690
|
+
const buf: Buffer = await renderCustomPdfToBuffer(def);
|
|
691
|
+
res.setHeader("Content-Type", "application/pdf");
|
|
692
|
+
res.send(buf);
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
Both functions:
|
|
696
|
+
1. Normalize all remote assets asynchronously.
|
|
697
|
+
2. Create a `PDFDocument`, register fonts, set up layout helpers.
|
|
698
|
+
3. Attach a `pageAdded` listener so PDFKit-internal page additions (e.g. long-text overflow) also receive the header, background, and watermark.
|
|
699
|
+
4. Run the block render loop, then call `doc.end()`.
|
|
700
|
+
|
|
701
|
+
---
|
|
702
|
+
|
|
703
|
+
## Built-in fonts
|
|
704
|
+
|
|
705
|
+
The following PDFKit built-in fonts are always available without registration:
|
|
706
|
+
|
|
707
|
+
| Family | Variants |
|
|
708
|
+
|---|---|
|
|
709
|
+
| Helvetica | `Helvetica`, `Helvetica-Bold`, `Helvetica-Oblique`, `Helvetica-BoldOblique` |
|
|
710
|
+
| Times | `Times-Roman`, `Times-Bold`, `Times-Italic`, `Times-BoldItalic` |
|
|
711
|
+
| Courier | `Courier`, `Courier-Bold`, `Courier-Oblique`, `Courier-BoldOblique` |
|
|
712
|
+
|
|
713
|
+
---
|
|
714
|
+
|
|
715
|
+
## Engine behaviour notes
|
|
716
|
+
|
|
717
|
+
**Automatic pagination**
|
|
718
|
+
`ensureSpaceFor` checks whether the next block fits before rendering it. If not, `finishPage(true)` is called — drawing the watermark (if applicable), the footer, and then `doc.addPage()` followed by `startNewPageLayout()` (background → header → watermark).
|
|
719
|
+
|
|
720
|
+
**PDFKit-internal page additions**
|
|
721
|
+
When `doc.text()` overflows a page, PDFKit calls `doc.addPage()` internally. The engine listens on the `pageAdded` event to draw the background, header, and watermark on those pages as well. A `ctx.inManualPageAdd` flag prevents double-drawing when the engine's own `finishPage` adds the page.
|
|
722
|
+
|
|
723
|
+
**Overflow guard**
|
|
724
|
+
If a block is taller than the entire page content area (between top and bottom margins), the engine emits a `console.warn`:
|
|
725
|
+
```
|
|
726
|
+
[pdf-engine] Block height (420pt) exceeds page content area (701pt). Content will overflow.
|
|
727
|
+
```
|
|
128
728
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
- When using remote assets, ensure your runtime has outbound network access and consider wrapping `renderCustomPdf*` in your own caching layer.
|
|
729
|
+
**Height measurement accuracy**
|
|
730
|
+
`heightOfString` is called with the same `lineGap` option that is passed to `doc.text()`, so the measured height matches the rendered height even when a custom `lineGap` is set.
|
|
132
731
|
|
|
133
|
-
|
|
732
|
+
**`visible: false`**
|
|
733
|
+
Any block (including deeply nested ones inside columns, tables, etc.) with `visible: false` is skipped during both rendering and height measurement.
|
|
134
734
|
|
|
135
|
-
|
|
136
|
-
- `pageBackground` is painted before header/content on every new page, while `watermark.mode` decides which pages receive the overlay.
|
|
137
|
-
- Footer callbacks receive `(pageNumber, pageSize)` so you can display pagination, print timestamps, or conditionally hide content.
|
|
735
|
+
---
|
|
138
736
|
|
|
139
737
|
## Contributors
|
|
140
738
|
|
|
@@ -142,4 +740,4 @@ Each block supports a `visible` flag so you can skip rendering without mutating
|
|
|
142
740
|
|
|
143
741
|
## License
|
|
144
742
|
|
|
145
|
-
MIT — see `LICENSE
|
|
743
|
+
MIT — see `LICENSE`.
|