jspdf-utils 0.1.25 → 0.1.27
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 +105 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# jsPDF Utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Utilities for rendering HTML into paginated PDF output with `jsPDF` and
|
|
4
|
+
`html2canvas`.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -8,79 +9,137 @@ HTML to PDF utilities for jsPDF with support for Arabic text, tables, and automa
|
|
|
8
9
|
npm install jspdf-utils jspdf html2canvas
|
|
9
10
|
```
|
|
10
11
|
|
|
11
|
-
##
|
|
12
|
+
## Exported API
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
- `generatePDF(doc, source, opts)`
|
|
15
|
+
- `generateImagePDF(source, opts)`
|
|
16
|
+
- `generateImages(source, opts)`
|
|
17
|
+
- `previewImages(source, container, opts)`
|
|
18
|
+
- `PAGE_SIZES`
|
|
19
|
+
- `PAGE_MARGINS`
|
|
20
|
+
- Types: `PageOptions`, `PageOptionsInput`, `ImagePDFOptions`
|
|
21
|
+
|
|
22
|
+
Type import example:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import type { PageOptionsInput } from "jspdf-utils";
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### 1) HTML -> vector/text PDF (`doc.html`)
|
|
31
|
+
|
|
32
|
+
```ts
|
|
14
33
|
import jsPDF from "jspdf";
|
|
15
|
-
import {
|
|
34
|
+
import { generatePDF } from "jspdf-utils";
|
|
35
|
+
|
|
36
|
+
const target = document.getElementById("print-section");
|
|
37
|
+
if (!target) throw new Error("Missing #print-section");
|
|
16
38
|
|
|
17
|
-
const doc = new jsPDF({ unit: "mm"
|
|
39
|
+
const doc = new jsPDF({ unit: "mm" });
|
|
18
40
|
|
|
19
|
-
//
|
|
20
|
-
doc.addFont("
|
|
21
|
-
doc.addFont("
|
|
41
|
+
// Optional for Arabic/RTL text:
|
|
42
|
+
// doc.addFont("/fonts/arial.ttf", "arial", "normal");
|
|
43
|
+
// doc.addFont("/fonts/arial-bold.ttf", "arial", "bold");
|
|
22
44
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
45
|
+
await generatePDF(doc, target, {
|
|
46
|
+
format: "a4",
|
|
47
|
+
margin: { top: 20, right: 20, bottom: 20, left: 20 },
|
|
48
|
+
forcedPageCount: 1,
|
|
49
|
+
});
|
|
26
50
|
|
|
27
51
|
doc.save("output.pdf");
|
|
28
52
|
```
|
|
29
53
|
|
|
30
|
-
|
|
54
|
+
### 2) HTML -> image-based PDF (raster pages)
|
|
31
55
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- **Text wrapping**: Long text blocks are intelligently broken at word boundaries
|
|
35
|
-
- **RTL/Arabic support**: Works with right-to-left languages when proper fonts are loaded
|
|
56
|
+
```ts
|
|
57
|
+
import { generateImagePDF } from "jspdf-utils";
|
|
36
58
|
|
|
37
|
-
|
|
59
|
+
const target = document.getElementById("print-section");
|
|
60
|
+
if (!target) throw new Error("Missing #print-section");
|
|
38
61
|
|
|
39
|
-
|
|
62
|
+
const imagePDF = await generateImagePDF(target, {
|
|
63
|
+
format: "a5",
|
|
64
|
+
imageFormat: "PNG",
|
|
65
|
+
forcedPageCount: 1,
|
|
66
|
+
});
|
|
40
67
|
|
|
41
|
-
|
|
42
|
-
npm install
|
|
43
|
-
npm run dev
|
|
44
|
-
# Open http://localhost:5173
|
|
68
|
+
imagePDF.save("output-image.pdf");
|
|
45
69
|
```
|
|
46
70
|
|
|
47
|
-
###
|
|
71
|
+
### 3) Preview pages as images in a container
|
|
48
72
|
|
|
73
|
+
```ts
|
|
74
|
+
import { previewImages } from "jspdf-utils";
|
|
75
|
+
|
|
76
|
+
const target = document.getElementById("print-section");
|
|
77
|
+
const preview = document.getElementById("preview-container");
|
|
78
|
+
if (!target || !preview) throw new Error("Missing preview elements");
|
|
79
|
+
|
|
80
|
+
await previewImages(target, preview, {
|
|
81
|
+
format: "a5",
|
|
82
|
+
forcedPageCount: 1,
|
|
83
|
+
});
|
|
49
84
|
```
|
|
50
|
-
├── src/
|
|
51
|
-
│ └── html-to-pdf.js # Main utility functions
|
|
52
|
-
├── index.html # Example/demo page
|
|
53
|
-
└── package.json
|
|
54
|
-
```
|
|
55
85
|
|
|
56
|
-
##
|
|
86
|
+
## Options
|
|
87
|
+
|
|
88
|
+
### `PageOptionsInput`
|
|
89
|
+
|
|
90
|
+
- `unit?: string` (default: `"mm"`)
|
|
91
|
+
- `format?: "a0" | "a1" | "a2" | "a3" | "a4" | "a5" | "a6" | "letter" | "legal" | "tabloid"` (default: `"a4"`)
|
|
92
|
+
- `pageWidth?: number` (default comes from `format`)
|
|
93
|
+
- `pageHeight?: number` (default comes from `format`)
|
|
94
|
+
- `margin?: number | { top?: number; right?: number; bottom?: number; left?: number }`
|
|
95
|
+
|
|
96
|
+
Important:
|
|
97
|
+
|
|
98
|
+
- `generatePDF`, `generateImagePDF`, `generateImages`, and `previewImages` use
|
|
99
|
+
page sizing from their `opts` (`format` / `pageWidth` / `pageHeight`).
|
|
100
|
+
- Do not rely on `new jsPDF({ format: ... })` to control layout in
|
|
101
|
+
`generatePDF`; pass `format` in `opts` instead.
|
|
102
|
+
|
|
103
|
+
### `ImagePDFOptions`
|
|
104
|
+
|
|
105
|
+
- `imageFormat?: "JPEG" | "PNG"`
|
|
106
|
+
- `imageQuality?: number`
|
|
107
|
+
- `scale?: number`
|
|
108
|
+
- `marginContent?: MarginContentInput`
|
|
109
|
+
- `forcedPageCount?: number`
|
|
57
110
|
|
|
58
|
-
|
|
111
|
+
`forcedPageCount` behavior:
|
|
59
112
|
|
|
60
|
-
|
|
113
|
+
- Forces output to the first `N` pages only.
|
|
114
|
+
- `generatePDF`: trims extra pages after `doc.html` rendering.
|
|
115
|
+
- `generateImagePDF`: only rasterizes and writes first `N` pages.
|
|
116
|
+
- `generateImages` and `previewImages`: only returns/displays first `N` pages.
|
|
117
|
+
- Invalid values (`<= 0`, `NaN`, `Infinity`) are ignored.
|
|
61
118
|
|
|
62
|
-
|
|
63
|
-
- **source**: HTML element to render
|
|
64
|
-
- **opts**: Optional configuration (overrides defaults)
|
|
119
|
+
## Margin Content and Borders
|
|
65
120
|
|
|
66
|
-
|
|
121
|
+
`marginContent` supports:
|
|
67
122
|
|
|
68
|
-
|
|
123
|
+
- `top`, `right`, `bottom`, `left` as:
|
|
124
|
+
- `HTMLElement`, or
|
|
125
|
+
- `(page: number, totalPages: number) => HTMLElement`
|
|
126
|
+
- `contentBorder` (vector rectangle)
|
|
127
|
+
- `textBorder` (repeated text around page edges)
|
|
69
128
|
|
|
70
|
-
|
|
129
|
+
Rendering order:
|
|
71
130
|
|
|
72
|
-
|
|
131
|
+
- Margin content and borders are rendered beneath page content.
|
|
132
|
+
- Main document content stays visually above borders/text borders.
|
|
73
133
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
pageHeight: 297,
|
|
80
|
-
margin: { top: 20, right: 20, bottom: 20, left: 20 }
|
|
81
|
-
}
|
|
134
|
+
## Development
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm install
|
|
138
|
+
npm run dev
|
|
82
139
|
```
|
|
83
140
|
|
|
141
|
+
Open [http://localhost:5173](http://localhost:5173).
|
|
142
|
+
|
|
84
143
|
## License
|
|
85
144
|
|
|
86
145
|
MIT
|
package/package.json
CHANGED