diff --git a/README.md b/README.md index 14e5331..a2f7204 100644 --- a/README.md +++ b/README.md @@ -55,21 +55,23 @@ html2pdf ./example.html --format A4 --pageRanges 1 -o example.pdf ## Parameters -| Name | Type | Description | -| ---------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------- | -| source | string | Set PDF document source. It must be specified. The source can be a URL or an HTML code. | -| timeout | integer | Set connection timeout. Default is `10`. | -| javascript | boolean | Enable JavaScripts. To enable, the value should be true. Disabled by default. | -| pageRanges | string | Set pages by the numbers and ranges. Example: `1,2-4`. All pages included by default. | -| mediaType | string | Set paper CSS media type. Must be one of `screen` or `print`. Default is `screen`. | -| format | string | Set paper format. Must be one of `A0`, `A1`, `A2`, `A3`, `A4`, `A5`, `A6`, `Letter`, `Legal`, `Tabloid`, `Ledger`. Default is `A4`. | -| width | integer | Set PDF document width. Both height and width must be used together. It will disable `format` option. | -| height | integer | Set PDF document height. Both height and width must be used together. It will disable `format` option. | -| 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`. | -| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. | -| 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`. | -| userAgent | string | Set custom user-agent string. | -| base64 | boolean | Return the PDF as a base64-encoded string (REST API only). Disabled by default. | +| Name | Type | Description | +| ---------------| ------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| source | string | Set PDF document source. It must be specified. The source can be a URL or an HTML code. | +| timeout | integer | Set connection timeout. Default is `10`. | +| javascript | boolean | Enable JavaScripts. To enable, the value should be true. Disabled by default. | +| pageRanges | string | Set pages by the numbers and ranges. Example: `1,2-4`. All pages included by default. | +| mediaType | string | Set paper CSS media type. Must be one of `screen` or `print`. Default is `screen`. | +| format | string | Set paper format. Must be one of `A0`, `A1`, `A2`, `A3`, `A4`, `A5`, `A6`, `Letter`, `Legal`, `Tabloid`, `Ledger`. Default is `A4`. | +| width | integer | Set PDF document width. Both height and width must be used together. It will disable `format` option. | +| height | integer | Set PDF document height. Both height and width must be used together. It will disable `format` option. | +| 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`. | +| landscape | boolean | Enable landscape mode. To enable, the value should be true. Disabled by default. | +| 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`. | +| userAgent | string | Set custom user-agent string. | +| base64 | boolean | Return the PDF as a base64-encoded string (REST API only). Disabled by default. | +| 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: | +| 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: | ## Author diff --git a/src/config.ts b/src/config.ts index 8a7406b..a6576b9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -37,6 +37,9 @@ export default class Config { readonly margin: Margin = new Margin(); readonly userAgent?: string; readonly base64: boolean = false; + readonly displayHeaderFooter: boolean = false; + readonly headerTemplate: string = "
"; + readonly footerTemplate: string = "
"; constructor(params?: { [key: string]: any }) { for (const param in params) { @@ -155,6 +158,18 @@ export default class Config { this.base64 = params[param]; } break; + case "headerTemplate": + if (typeof params[param] === "string") { + this.headerTemplate = params[param]; + this.displayHeaderFooter = true; + } + break; + case "footerTemplate": + if (typeof params[param] === "string") { + this.footerTemplate = params[param]; + this.displayHeaderFooter = true; + } + break; } } } diff --git a/src/generator/index.ts b/src/generator/index.ts index 51be3f2..ec95533 100644 --- a/src/generator/index.ts +++ b/src/generator/index.ts @@ -54,7 +54,10 @@ export default async (config: Config): Promise => { height: config.height, scale: config.scale, landscape: config.landscape, - margin: config.margin, + margin: config.margin, + displayHeaderFooter: config.displayHeaderFooter, + headerTemplate: config.headerTemplate, + footerTemplate: config.footerTemplate, }); // Close the browser instance diff --git a/test/config.others.test.ts b/test/config.others.test.ts index a1c3659..5366de9 100644 --- a/test/config.others.test.ts +++ b/test/config.others.test.ts @@ -36,4 +36,23 @@ describe("Others", function () { assert.equal(config.height, 200); }); }); + + describe("header", function () { + it("Should have header", function () { + const config = new Config({ headerTemplate: "
Header
" }); + assert.equal(config.headerTemplate, "
Header
"); + assert.equal(config.footerTemplate, "
"); + assert.equal(config.displayHeaderFooter, true); + }); + it("Should have footer", function () { + const config = new Config({ footerTemplate: "
footer
" }); + assert.equal(config.headerTemplate, "
"); + assert.equal(config.footerTemplate, "
footer
"); + assert.equal(config.displayHeaderFooter, true); + }); + it("Should display header be false", function () { + const config = new Config({ width: 100, height: 200, format: "A2" }); + assert.equal(config.displayHeaderFooter, false); + }); + }); });