jspdf-utils 0.1.25 → 0.1.26

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.
Files changed (2) hide show
  1. package/README.md +96 -45
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # jsPDF Utils
2
2
 
3
- HTML to PDF utilities for jsPDF with support for Arabic text, tables, and automatic page breaking.
3
+ Utilities for rendering HTML into paginated PDF output with `jsPDF` and
4
+ `html2canvas`.
4
5
 
5
6
  ## Installation
6
7
 
@@ -8,79 +9,129 @@ 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
- ## Usage
12
+ ## Exported API
12
13
 
13
- ```javascript
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 { renderHTML } from "jspdf-utils";
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
39
  const doc = new jsPDF({ unit: "mm", format: "a4" });
18
40
 
19
- // Add fonts if needed for Arabic/RTL text
20
- doc.addFont("path/to/arial.ttf", "arial", "normal", "normal");
21
- doc.addFont("path/to/arial-bold.ttf", "arial", "normal", "bold");
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
- // Render HTML element to PDF
24
- const element = document.getElementById("content");
25
- await renderHTML(doc, element);
45
+ await generatePDF(doc, target, {
46
+ margin: { top: 20, right: 20, bottom: 20, left: 20 },
47
+ forcedPageCount: 1,
48
+ });
26
49
 
27
50
  doc.save("output.pdf");
28
51
  ```
29
52
 
30
- ## Features
53
+ ### 2) HTML -> image-based PDF (raster pages)
31
54
 
32
- - **Automatic page breaking**: Prevents tables and text from being split awkwardly
33
- - **Table splitting**: Large tables are split across pages with repeated headers
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
55
+ ```ts
56
+ import { generateImagePDF } from "jspdf-utils";
36
57
 
37
- ## Development
58
+ const target = document.getElementById("print-section");
59
+ if (!target) throw new Error("Missing #print-section");
38
60
 
39
- ### Running the Example
61
+ const imagePDF = await generateImagePDF(target, {
62
+ format: "a5",
63
+ imageFormat: "PNG",
64
+ forcedPageCount: 1,
65
+ });
40
66
 
41
- ```bash
42
- npm install
43
- npm run dev
44
- # Open http://localhost:5173
67
+ imagePDF.save("output-image.pdf");
45
68
  ```
46
69
 
47
- ### Project Structure
70
+ ### 3) Preview pages as images in a container
48
71
 
72
+ ```ts
73
+ import { previewImages } from "jspdf-utils";
74
+
75
+ const target = document.getElementById("print-section");
76
+ const preview = document.getElementById("preview-container");
77
+ if (!target || !preview) throw new Error("Missing preview elements");
78
+
79
+ await previewImages(target, preview, {
80
+ format: "a5",
81
+ forcedPageCount: 1,
82
+ });
49
83
  ```
50
- ├── src/
51
- │ └── html-to-pdf.js # Main utility functions
52
- ├── index.html # Example/demo page
53
- └── package.json
54
- ```
55
84
 
56
- ## API
85
+ ## Options
86
+
87
+ ### `PageOptionsInput`
88
+
89
+ - `unit?: string` (default: `"mm"`)
90
+ - `format?: "a0" | "a1" | "a2" | "a3" | "a4" | "a5" | "a6" | "letter" | "legal" | "tabloid"` (default: `"a4"`)
91
+ - `pageWidth?: number` (default comes from `format`)
92
+ - `pageHeight?: number` (default comes from `format`)
93
+ - `margin?: number | { top?: number; right?: number; bottom?: number; left?: number }`
94
+
95
+ ### `ImagePDFOptions`
96
+
97
+ - `imageFormat?: "JPEG" | "PNG"`
98
+ - `imageQuality?: number`
99
+ - `scale?: number`
100
+ - `marginContent?: MarginContentInput`
101
+ - `forcedPageCount?: number`
57
102
 
58
- ### `renderHTML(doc, source, opts)`
103
+ `forcedPageCount` behavior:
59
104
 
60
- Renders an HTML element to PDF.
105
+ - Forces output to the first `N` pages only.
106
+ - `generatePDF`: trims extra pages after `doc.html` rendering.
107
+ - `generateImagePDF`: only rasterizes and writes first `N` pages.
108
+ - `generateImages` and `previewImages`: only returns/displays first `N` pages.
109
+ - Invalid values (`<= 0`, `NaN`, `Infinity`) are ignored.
61
110
 
62
- - **doc**: jsPDF instance
63
- - **source**: HTML element to render
64
- - **opts**: Optional configuration (overrides defaults)
111
+ ## Margin Content and Borders
65
112
 
66
- ### `prepare(source, opts)`
113
+ `marginContent` supports:
67
114
 
68
- Prepares an HTML element for rendering (used internally by renderHTML).
115
+ - `top`, `right`, `bottom`, `left` as:
116
+ - `HTMLElement`, or
117
+ - `(page: number, totalPages: number) => HTMLElement`
118
+ - `contentBorder` (vector rectangle)
119
+ - `textBorder` (repeated text around page edges)
69
120
 
70
- Returns: `{ clone, layout, options, cleanup }`
121
+ Rendering order:
71
122
 
72
- ### Default Options
123
+ - Margin content and borders are rendered beneath page content.
124
+ - Main document content stays visually above borders/text borders.
73
125
 
74
- ```javascript
75
- {
76
- unit: 'mm',
77
- format: 'a4',
78
- pageWidth: 210,
79
- pageHeight: 297,
80
- margin: { top: 20, right: 20, bottom: 20, left: 20 }
81
- }
126
+ ## Development
127
+
128
+ ```bash
129
+ npm install
130
+ npm run dev
82
131
  ```
83
132
 
133
+ Open [http://localhost:5173](http://localhost:5173).
134
+
84
135
  ## License
85
136
 
86
137
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jspdf-utils",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "Utility helpers for jsPDF's doc.html() renderer with automatic page breaking, table splitting, and RTL support",
5
5
  "type": "module",
6
6
  "main": "dist/html-to-pdf.js",