p3x-html-pdf 2025.4.154 → 2025.4.155
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 +147 -3
- package/package.json +1 -1
- package/test/test.js +17 -9
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
# 📃 Generates PDF from HTML with custom headers and footers with wkhtmltopdf v2025.4.
|
|
9
|
+
# 📃 Generates PDF from HTML with custom headers and footers with wkhtmltopdf v2025.4.155
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
@@ -181,7 +181,8 @@ You can use placeholders in your HTML for dynamic data (only these, but it is en
|
|
|
181
181
|
</div>
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
-
The `p3x-footer` and `p3x-header` should not have any styles other than `id` and `data-height`.
|
|
184
|
+
The `p3x-footer` and `p3x-header` should not have any styles other than `id` and `data-height`.
|
|
185
|
+
|
|
185
186
|
---
|
|
186
187
|
|
|
187
188
|
## 📊 Advanced Features
|
|
@@ -192,6 +193,149 @@ The `p3x-footer` and `p3x-header` should not have any styles other than `id` and
|
|
|
192
193
|
|
|
193
194
|
---
|
|
194
195
|
|
|
196
|
+
## 🌟 Headers and Footers in `p3x-html-pdf`
|
|
197
|
+
|
|
198
|
+
This document provides a detailed explanation of how to work with headers and footers using `p3x-html-pdf`, including first-page-specific headers and indexed headers for subsequent pages. With this approach, you can create professional-grade PDFs with precise control over header and footer content.
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### 📖 Overview
|
|
203
|
+
|
|
204
|
+
Headers and footers in `p3x-html-pdf` are managed via HTML templates. You can:
|
|
205
|
+
- Define **default headers and footers** for all pages.
|
|
206
|
+
- Create **specific headers or footers** for certain pages using indexing (e.g., `p3x-header-1` for the first page).
|
|
207
|
+
- Dynamically calculate content for headers and footers, such as page numbers, document titles, or custom logic.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
### 🚀 How It Works
|
|
212
|
+
|
|
213
|
+
`p3x-html-pdf` uses the `id` attribute and `data-height` to manage headers and footers. The key attributes and elements are:
|
|
214
|
+
|
|
215
|
+
- **Header IDs**:
|
|
216
|
+
- `p3x-header`: The default header for all pages.
|
|
217
|
+
- `p3x-header-<page>`: A header for a specific page (e.g., `p3x-header-1` for the first page).
|
|
218
|
+
- **Footer IDs**:
|
|
219
|
+
- `p3x-footer`: The default footer for all pages.
|
|
220
|
+
- `p3x-footer-<page>`: A footer for a specific page.
|
|
221
|
+
- **`data-height`**: Specifies the height of the header/footer (in millimeters). Ensure this matches the expected content size to prevent overlap.
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
### 🌟 Example: First Page Header and Default Header
|
|
226
|
+
|
|
227
|
+
The following example demonstrates a **custom header** for the first page and a **default header** for the rest of the document.
|
|
228
|
+
|
|
229
|
+
#### HTML Template
|
|
230
|
+
|
|
231
|
+
```html
|
|
232
|
+
<div id="p3x-header-1" data-height="40mm">
|
|
233
|
+
<div style="width: 100%; padding: 0px; display: table;">
|
|
234
|
+
<div style="display: table-cell; vertical-align: middle;">
|
|
235
|
+
<img src="http://cdn.corifeus.com/assets/png/patrikx3.png" alt="Header Logo" style="height:40mm; margin:0;"/>
|
|
236
|
+
</div>
|
|
237
|
+
<div style="display: table-cell; vertical-align: middle; text-align: right; width: 100%;">
|
|
238
|
+
<h1 style="margin: 0; font-size: 20px; color: #333;">P3X HTML Invoice - First Page</h1>
|
|
239
|
+
<p style="margin: 5px 0 0; font-size: 14px; color: #555;">Generated: ${new Date().toLocaleDateString()}</p>
|
|
240
|
+
</div>
|
|
241
|
+
</div>
|
|
242
|
+
</div>
|
|
243
|
+
|
|
244
|
+
<div id="p3x-header" data-height="40mm">
|
|
245
|
+
<div style="display: table; width: 100%; height: 40mm; text-align: right;">
|
|
246
|
+
<div style="display: table-cell; vertical-align: middle; text-align: right;">
|
|
247
|
+
<h1 style="margin: 0; font-size: 20px; color: #333;">P3X HTML Invoice</h1>
|
|
248
|
+
<p style="margin: 5px 0 0; font-size: 14px; color: #555;">Generated: ${new Date().toLocaleDateString()}</p>
|
|
249
|
+
</div>
|
|
250
|
+
</div>
|
|
251
|
+
</div>
|
|
252
|
+
|
|
253
|
+
<div id="p3x-footer" data-height="10mm">
|
|
254
|
+
<div style="text-align: right; font-size: 12px; color: #777;">
|
|
255
|
+
Page ${page} of ${pages}
|
|
256
|
+
</div>
|
|
257
|
+
</div>
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
### 🛠️ Dynamic Header and Footer Logic
|
|
263
|
+
|
|
264
|
+
- Use indexed headers or footers for specific pages.
|
|
265
|
+
- Utilize placeholders like `${page}` and `${pages}` to dynamically display the current page and total pages.
|
|
266
|
+
|
|
267
|
+
#### Example Configuration in JavaScript
|
|
268
|
+
|
|
269
|
+
```javascript
|
|
270
|
+
const options = {
|
|
271
|
+
settings: {
|
|
272
|
+
save: true,
|
|
273
|
+
template: {
|
|
274
|
+
format: 'A4',
|
|
275
|
+
marginLeft: 10,
|
|
276
|
+
marginRight: 10,
|
|
277
|
+
orientation: 'portrait',
|
|
278
|
+
},
|
|
279
|
+
html: `
|
|
280
|
+
<div id="p3x-header-1" data-height="40mm">
|
|
281
|
+
<!-- Custom header for the first page -->
|
|
282
|
+
</div>
|
|
283
|
+
<div id="p3x-header" data-height="40mm">
|
|
284
|
+
<!-- Default header for subsequent pages -->
|
|
285
|
+
</div>
|
|
286
|
+
<div id="p3x-footer" data-height="10mm">
|
|
287
|
+
<!-- Footer for all pages -->
|
|
288
|
+
</div>
|
|
289
|
+
`,
|
|
290
|
+
},
|
|
291
|
+
title: 'Dynamic Headers and Footers Example',
|
|
292
|
+
saveFile: path.resolve(__dirname, 'output.pdf'),
|
|
293
|
+
};
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
### 📏 Calculating Headers and Footers per Page
|
|
299
|
+
|
|
300
|
+
When designing headers and footers:
|
|
301
|
+
1. **Estimate Content Size:**
|
|
302
|
+
- Use `data-height` to reserve enough space for your header or footer content.
|
|
303
|
+
- Example: A header with a logo and text may need `40mm`.
|
|
304
|
+
|
|
305
|
+
2. **Adjust Margins:**
|
|
306
|
+
- Ensure the margins accommodate both the header/footer and the main content.
|
|
307
|
+
|
|
308
|
+
3. **Testing for Multi-Page Documents:**
|
|
309
|
+
- For multi-page documents, validate the alignment of headers and footers across all pages.
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
#### 📄 Headers and Footers with Indexed Customization
|
|
314
|
+
|
|
315
|
+
`p3x-html-pdf` supports indexed headers and footers, allowing unique designs for specific pages. For example, `p3x-header-1` can define a header for the first page, while `p3x-header` applies to subsequent pages. Similarly, `p3x-footer-1` can be used for a custom first-page footer.
|
|
316
|
+
|
|
317
|
+
##### Key Points:
|
|
318
|
+
1. **Indexed IDs**: Use `p3x-header-1`, `p3x-footer-1`, etc., for specific pages. Default headers (`p3x-header`) and footers (`p3x-footer`) are used when no specific index is found.
|
|
319
|
+
2. **Consistent Heights**: All headers and footers must share the same `data-height` (e.g., `40mm` for headers, `10mm` for footers) to ensure proper alignment and accurate page calculations.
|
|
320
|
+
3. **Dynamic Content**: Use placeholders like `${page}` and `${pages}` to display page-specific data dynamically.
|
|
321
|
+
|
|
322
|
+
This approach allows tailored styling for specific pages while maintaining consistent layouts throughout the document.
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
### 📊 Advanced Features
|
|
327
|
+
|
|
328
|
+
- Combine **indexed headers/footers** for specific pages with a **default** fallback.
|
|
329
|
+
- Use JavaScript in the `header-footer.html` template to dynamically adjust content.
|
|
330
|
+
- Use indexed configurations to simulate "first-page" or "last-page" behavior.
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
### 🖼️ Example Output
|
|
335
|
+
|
|
336
|
+
You can generate a PDF with the provided configuration to see how headers and footers are applied dynamically. For larger documents, this approach allows consistent styling with room for customization.
|
|
337
|
+
|
|
338
|
+
|
|
195
339
|
## 🌍 Architecture
|
|
196
340
|
|
|
197
341
|
`p3x-html-pdf` works seamlessly on Linux, Windows and ARM64.
|
|
@@ -365,7 +509,7 @@ All my domains, including [patrikx3.com](https://patrikx3.com), [corifeus.eu](ht
|
|
|
365
509
|
---
|
|
366
510
|
|
|
367
511
|
|
|
368
|
-
[**P3X-HTML-PDF**](https://corifeus.com/html-pdf) Build v2025.4.
|
|
512
|
+
[**P3X-HTML-PDF**](https://corifeus.com/html-pdf) Build v2025.4.155
|
|
369
513
|
|
|
370
514
|
[](https://www.npmjs.com/package/p3x-html-pdf) [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QZVM4V6HVZJW6) [](https://www.patrikx3.com/en/front/contact) [](https://www.facebook.com/corifeus.software)
|
|
371
515
|
|
package/package.json
CHANGED
package/test/test.js
CHANGED
|
@@ -34,18 +34,26 @@ const fs = require('fs');
|
|
|
34
34
|
orientation: 'portrait',
|
|
35
35
|
},
|
|
36
36
|
html: `
|
|
37
|
-
<div id="p3x-header" data-height="40mm"
|
|
38
|
-
<div
|
|
37
|
+
<div id="p3x-header-1" data-height="40mm">
|
|
38
|
+
<div style="width: 100%; padding: 0px; display: table;">
|
|
39
39
|
<div style="display: table-cell; vertical-align: middle;">
|
|
40
40
|
<img src="http://cdn.corifeus.com/assets/png/patrikx3.png" alt="Header Logo" style="height:40mm; margin:0;"/>
|
|
41
41
|
</div>
|
|
42
42
|
<div style="display: table-cell; vertical-align: middle; text-align: right; width: 100%;">
|
|
43
|
-
<h1 style="margin: 0; font-size: 20px; color: #333;">P3X HTML Invoice</h1>
|
|
43
|
+
<h1 style="margin: 0; font-size: 20px; color: #333;">P3X HTML Invoice - First Page</h1>
|
|
44
44
|
<p style="margin: 5px 0 0; font-size: 14px; color: #555;">Generated: ${new Date().toLocaleDateString()}</p>
|
|
45
45
|
</div>
|
|
46
46
|
</div>
|
|
47
47
|
</div>
|
|
48
|
-
<div id="p3x-
|
|
48
|
+
<div id="p3x-header" data-height="40mm" >
|
|
49
|
+
<div style="display: table; width: 100%; height: 40mm; text-align: right;">
|
|
50
|
+
<div style="display: table-cell; vertical-align: middle; text-align: right;">
|
|
51
|
+
<h1 style="margin: 0; font-size: 20px; color: #333;">P3X HTML Invoice </h1>
|
|
52
|
+
<p style="margin: 5px 0 0; font-size: 14px; color: #555;">Generated: ${new Date().toLocaleDateString()}</p>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
<div id="p3x-footer" data-height="10mm">
|
|
49
57
|
<div style="text-align: right; font-size: 12px; color: #777;">
|
|
50
58
|
Page \${page} of \${pages}
|
|
51
59
|
</div>
|
|
@@ -61,16 +69,16 @@ const fs = require('fs');
|
|
|
61
69
|
<th style="border: 1px solid #ddd; padding: 8px; background-color: #f4f4f4;">Total</th>
|
|
62
70
|
</tr>
|
|
63
71
|
${Array.from({ length: 26 }).map((_, i) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
72
|
+
const price = (i + 1) * 10;
|
|
73
|
+
const quantity = (i % 5) + 1;
|
|
74
|
+
const total = price * quantity;
|
|
75
|
+
return `<tr>
|
|
68
76
|
<td style="border: 1px solid #ddd; padding: 8px;">Product ${String.fromCharCode(65 + (i % 26))}</td>
|
|
69
77
|
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">${quantity}</td>
|
|
70
78
|
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">$${price}.00</td>
|
|
71
79
|
<td style="border: 1px solid #ddd; padding: 8px; text-align: right;">$${total}.00</td>
|
|
72
80
|
</tr>`;
|
|
73
|
-
|
|
81
|
+
}).join('')}
|
|
74
82
|
<tr>
|
|
75
83
|
<td colspan="3" style="border: 1px solid #ddd; padding: 8px; text-align: right; font-weight: bold;">Subtotal</td>
|
|
76
84
|
<td style="border: 1px solid #ddd; padding: 8px; text-align: right; font-weight: bold;">$${Array.from({ length: 15 }).reduce((acc, _, i) => acc + ((i + 1) * 10 * ((i % 5) + 1)), 0)}.00</td>
|