av6-pdf-engine 1.0.0 → 1.0.1
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 +155 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# av6-pdf-engine
|
|
2
|
+
|
|
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 (page setup, styles, headers, blocks, tables, QR/barcodes, signatures, etc.) and render it to a file or in-memory buffer from any Node.js service.
|
|
5
|
+
|
|
6
|
+
## Highlights
|
|
7
|
+
|
|
8
|
+
- JSON definition that mirrors pdfmake-style blocks while staying close to raw PDFKit features.
|
|
9
|
+
- Built-in layout helpers for headers, footers, watermarks, page backgrounds, and signature flows.
|
|
10
|
+
- Rich block catalog: text, columns, tables, key/value grids, images, QR codes, 1D barcodes, lines, and explicit page breaks.
|
|
11
|
+
- Automatic asset normalization for remote URLs, Buffers, or local paths (images, QR, barcode payloads).
|
|
12
|
+
- Custom font registration plus reusable style definitions with full TypeScript types.
|
|
13
|
+
- Two rendering targets: `renderCustomPdf(def, "/tmp/doc.pdf")` or `renderCustomPdfToBuffer(def)` for HTTP responses/storage.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install av6-pdf-engine
|
|
19
|
+
# or
|
|
20
|
+
pnpm add av6-pdf-engine
|
|
21
|
+
# or
|
|
22
|
+
yarn add av6-pdf-engine
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import {
|
|
29
|
+
renderCustomPdfToBuffer,
|
|
30
|
+
renderCustomPdf,
|
|
31
|
+
CustomDocDefinition,
|
|
32
|
+
} from "av6-pdf-engine";
|
|
33
|
+
|
|
34
|
+
const doc: CustomDocDefinition = {
|
|
35
|
+
pageSize: "A4",
|
|
36
|
+
margins: { top: 80, right: 40, bottom: 80, left: 40 },
|
|
37
|
+
fonts: [{ name: "Inter", src: "./assets/Inter-Regular.ttf" }],
|
|
38
|
+
styles: {
|
|
39
|
+
heading: { font: "Helvetica-Bold", fontSize: 16 },
|
|
40
|
+
label: { color: "#999", fontSize: 9, bold: true },
|
|
41
|
+
},
|
|
42
|
+
header: {
|
|
43
|
+
blocks: [{ type: "text", text: "ACME Corp — Invoice", style: "heading" }],
|
|
44
|
+
backgroundColor: "#f7f7f7",
|
|
45
|
+
},
|
|
46
|
+
footer: (page, size) => ({
|
|
47
|
+
blocks: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
text: `Page ${page} / ${Math.round(size.height)}`,
|
|
51
|
+
align: "center",
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
}),
|
|
55
|
+
pageBackground: { src: "./assets/background.png", opacity: 0.05 },
|
|
56
|
+
watermark: { text: "ACME CONFIDENTIAL", mode: "exceptFirst", opacity: 0.1 },
|
|
57
|
+
content: [
|
|
58
|
+
{
|
|
59
|
+
type: "columns",
|
|
60
|
+
widths: [200, "*"],
|
|
61
|
+
columns: [
|
|
62
|
+
[
|
|
63
|
+
{ type: "text", text: "Invoice #1234", style: "heading" },
|
|
64
|
+
{
|
|
65
|
+
type: "keyValueGrid",
|
|
66
|
+
columns: [
|
|
67
|
+
[
|
|
68
|
+
{ key: "Issued", value: "2025-12-01" },
|
|
69
|
+
{ key: "Due", value: "2025-12-15" },
|
|
70
|
+
],
|
|
71
|
+
],
|
|
72
|
+
keyStyle: "label",
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
[
|
|
76
|
+
{
|
|
77
|
+
type: "table",
|
|
78
|
+
widths: ["*", 80, 80],
|
|
79
|
+
headerRows: 1,
|
|
80
|
+
body: [
|
|
81
|
+
[
|
|
82
|
+
{ text: "Item", style: "label" },
|
|
83
|
+
{ text: "Qty", style: "label", align: "right" },
|
|
84
|
+
{ text: "Total", style: "label", align: "right" },
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
{ text: "Consulting" },
|
|
88
|
+
{ text: "12", align: "right" },
|
|
89
|
+
{ text: "$3,000", align: "right" },
|
|
90
|
+
],
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
{ type: "qr", value: "https://acme.example.com/pay/1234", size: 80 },
|
|
97
|
+
{ type: "barcode", value: "123456789012", bcType: "EAN13", width: 200 },
|
|
98
|
+
{ type: "signature", height: 100, label: "Authorized signature" },
|
|
99
|
+
],
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Write directly to disk
|
|
103
|
+
await renderCustomPdf(doc, "./tmp/invoice.pdf");
|
|
104
|
+
|
|
105
|
+
// Or capture an in-memory Buffer (perfect for HTTP responses / uploads)
|
|
106
|
+
const pdfBuffer = await renderCustomPdfToBuffer(doc);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Document definition
|
|
110
|
+
|
|
111
|
+
- `pageSize`: Any PDFKit-supported size (`"A4"`, `"LETTER"`, `[width, height]`, etc.). Defaults to A4.
|
|
112
|
+
- `margins`: Required `{ top, right, bottom, left }`. `top`/`bottom` double as header/footer bands.
|
|
113
|
+
- `fonts`: Optional array of `{ name, src }` for custom TTF/OTF registration before rendering.
|
|
114
|
+
- `header` / `footer`: Structured block arrays. Footers can also be a single block, array, or a callback `(pageNumber, pageSize) => Block | FooterDef`.
|
|
115
|
+
- `pageBackground`: Optional image (path, URL, or Buffer) painted before content each page.
|
|
116
|
+
- `watermark`: Text watermark with opacity, angle, font size, color, and placement mode (`all`, `first`, `last`, `exceptFirst`, etc.).
|
|
117
|
+
- `styles`: Named `StyleDef` map that can be referenced via `style` on text/table cells/key-value items.
|
|
118
|
+
- `content`: Ordered array of `Block` objects (see below) rendered top to bottom with automatic pagination.
|
|
119
|
+
|
|
120
|
+
## Block catalog
|
|
121
|
+
|
|
122
|
+
- `text`: Paragraph with typography controls (alignment, inline styles, links, margins).
|
|
123
|
+
- `image`: Local path, Buffer, or remote URL (auto-downloaded) with sizing/alignment.
|
|
124
|
+
- `qr`: QR codes via `qrcode` with size, version, and error correction level support.
|
|
125
|
+
- `barcode`: CODE128, EAN13, CODE39, ITF, CODE93 via `bwip-js`, including rotation, scaling, and caption text.
|
|
126
|
+
- `columns`: Multi-column layouts (`widths`, gaps, `keepTogether`) that can nest any other blocks.
|
|
127
|
+
- `table`: Simple table primitive with header rows, cell styles, and optional border layout hints.
|
|
128
|
+
- `keyValueGrid`: Responsive key/value pairs with shared or per-row styles, separators, and column widths.
|
|
129
|
+
- `line`: Horizontal rule with configurable width, color, and surrounding margins.
|
|
130
|
+
- `pageBreak`: Forces the renderer to finish the current page before continuing.
|
|
131
|
+
- `signature`: Reserves space (height/width) and optionally renders nested blocks or an image to support wet/digital signatures.
|
|
132
|
+
|
|
133
|
+
Each block supports a `visible` flag so you can skip rendering without mutating arrays.
|
|
134
|
+
|
|
135
|
+
## Asset handling & async preparation
|
|
136
|
+
|
|
137
|
+
- Images, QR payloads, and barcodes accept paths, Buffers, or HTTPS URLs. Remote sources are fetched and cached as Buffers before rendering begins.
|
|
138
|
+
- Nested structures (columns, signature blocks, headers/footers) are traversed automatically, so you can reference assets anywhere in the tree.
|
|
139
|
+
- When using remote assets, ensure your runtime has outbound network access and consider wrapping `renderCustomPdf*` in your own caching layer.
|
|
140
|
+
|
|
141
|
+
## Headers, footers, backgrounds, and watermarks
|
|
142
|
+
|
|
143
|
+
- Headers render inside the `top` margin band; footers render inside the `bottom` band. Adjust `marginTop/marginBottom` on header/footer defs for fine-grained spacing.
|
|
144
|
+
- `pageBackground` is painted before header/content on every new page, while `watermark.mode` decides which pages receive the overlay.
|
|
145
|
+
- Footer callbacks receive `(pageNumber, pageSize)` so you can display pagination, print timestamps, or conditionally hide content.
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
- Build TypeScript to `dist/`: `pnpm build`
|
|
150
|
+
- Regenerate before publishing: `pnpm prepublishOnly`
|
|
151
|
+
- Run the sample script (if you add one) with `pnpm demo` to exercise the renderer locally.
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT — see `LICENSE` (provide your own if distributing).
|