Skip to content

Commit 232fad5

Browse files
authored
Merge pull request #3 from functional-solutions/master
Added Header and footer to PDF
2 parents d76e5e5 + 2da6d0c commit 232fad5

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf
5555

5656
## Parameters
5757

58-
| Name | Type | Description |
59-
| ---------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------- |
60-
| source | string | Set PDF document source. It must be specified. The source can be a URL or an HTML code. |
61-
| timeout | integer | Set connection timeout. Default is `10`. |
62-
| javascript | boolean | Enable JavaScripts. To enable, the value should be true. Disabled by default. |
63-
| pageRanges | string | Set pages by the numbers and ranges. Example: `1,2-4`. All pages included by default. |
64-
| mediaType | string | Set paper CSS media type. Must be one of `screen` or `print`. Default is `screen`. |
65-
| format | string | Set paper format. Must be one of `A0`, `A1`, `A2`, `A3`, `A4`, `A5`, `A6`, `Letter`, `Legal`, `Tabloid`, `Ledger`. Default is `A4`. |
66-
| width | integer | Set PDF document width. Both height and width must be used together. It will disable `format` option. |
67-
| height | integer | Set PDF document height. Both height and width must be used together. It will disable `format` option. |
68-
| scale | float | Set scale. The value must be greater than or equals to `0.1`, or lower than or equals to `2`. Default is `1`. |
69-
| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. |
70-
| margin | string | Set margin(s) for the PDF document. It can be all four margin or specified by the values separated with space. Default is `0`. |
71-
| userAgent | string | Set custom user-agent string. |
72-
| base64 | boolean | Return the PDF as a base64-encoded string (REST API only). Disabled by default. |
58+
| Name | Type | Description |
59+
| ---------------| ------- | ----------------------------------------------------------------------------------------------------------------------------------- |
60+
| source | string | Set PDF document source. It must be specified. The source can be a URL or an HTML code. |
61+
| timeout | integer | Set connection timeout. Default is `10`. |
62+
| javascript | boolean | Enable JavaScripts. To enable, the value should be true. Disabled by default. |
63+
| pageRanges | string | Set pages by the numbers and ranges. Example: `1,2-4`. All pages included by default. |
64+
| mediaType | string | Set paper CSS media type. Must be one of `screen` or `print`. Default is `screen`. |
65+
| format | string | Set paper format. Must be one of `A0`, `A1`, `A2`, `A3`, `A4`, `A5`, `A6`, `Letter`, `Legal`, `Tabloid`, `Ledger`. Default is `A4`. |
66+
| width | integer | Set PDF document width. Both height and width must be used together. It will disable `format` option. |
67+
| height | integer | Set PDF document height. Both height and width must be used together. It will disable `format` option. |
68+
| scale | float | Set scale. The value must be greater than or equals to `0.1`, or lower than or equals to `2`. Default is `1`. |
69+
| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. |
70+
| margin | string | Set margin(s) for the PDF document. It can be all four margin or specified by the values separated with space. Default is `0`. |
71+
| userAgent | string | Set custom user-agent string. |
72+
| base64 | boolean | Return the PDF as a base64-encoded string (REST API only). Disabled by default. |
73+
| headerTemplate | string | Set PDF header. HTML template for the print header. Should be valid HTML with the following classes used to inject values into them: <ul><li>date formatted print date</li><li>title document title</li><li>url document location</li> <li>pageNumber current page number</li><li>totalPages total pages in the document</li></ul> |
74+
| footerTemplate | string | Set PDF footer. HTML template for the print header. Should be valid HTML with the following classes used to inject values into them: <ul><li>date formatted print date</li><li>title document title</li><li>url document location</li> <li>pageNumber current page number</li><li>totalPages total pages in the document</li></ul> |
7375

7476
## Author
7577

src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export default class Config {
3737
readonly margin: Margin = new Margin();
3838
readonly userAgent?: string;
3939
readonly base64: boolean = false;
40+
readonly displayHeaderFooter: boolean = false;
41+
readonly headerTemplate: string = "<div/>";
42+
readonly footerTemplate: string = "<div/>";
4043

4144
constructor(params?: { [key: string]: any }) {
4245
for (const param in params) {
@@ -155,6 +158,18 @@ export default class Config {
155158
this.base64 = params[param];
156159
}
157160
break;
161+
case "headerTemplate":
162+
if (typeof params[param] === "string") {
163+
this.headerTemplate = params[param];
164+
this.displayHeaderFooter = true;
165+
}
166+
break;
167+
case "footerTemplate":
168+
if (typeof params[param] === "string") {
169+
this.footerTemplate = params[param];
170+
this.displayHeaderFooter = true;
171+
}
172+
break;
158173
}
159174
}
160175
}

src/generator/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ export default async (config: Config): Promise<Uint8Array> => {
5454
height: config.height,
5555
scale: config.scale,
5656
landscape: config.landscape,
57-
margin: config.margin,
57+
margin: config.margin,
58+
displayHeaderFooter: config.displayHeaderFooter,
59+
headerTemplate: config.headerTemplate,
60+
footerTemplate: config.footerTemplate,
5861
});
5962

6063
// Close the browser instance

test/config.others.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ describe("Others", function () {
3636
assert.equal(config.height, 200);
3737
});
3838
});
39+
40+
describe("header", function () {
41+
it("Should have header", function () {
42+
const config = new Config({ headerTemplate: "<div>Header</div>" });
43+
assert.equal(config.headerTemplate, "<div>Header</div>");
44+
assert.equal(config.footerTemplate, "<div/>");
45+
assert.equal(config.displayHeaderFooter, true);
46+
});
47+
it("Should have footer", function () {
48+
const config = new Config({ footerTemplate: "<div>footer</div>" });
49+
assert.equal(config.headerTemplate, "<div/>");
50+
assert.equal(config.footerTemplate, "<div>footer</div>");
51+
assert.equal(config.displayHeaderFooter, true);
52+
});
53+
it("Should display header be false", function () {
54+
const config = new Config({ width: 100, height: 200, format: "A2" });
55+
assert.equal(config.displayHeaderFooter, false);
56+
});
57+
});
3958
});

0 commit comments

Comments
 (0)