Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <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> |
| 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> |

## Author

Expand Down
15 changes: 15 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<div/>";
readonly footerTemplate: string = "<div/>";

constructor(params?: { [key: string]: any }) {
for (const param in params) {
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export default async (config: Config): Promise<Uint8Array> => {
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
Expand Down
19 changes: 19 additions & 0 deletions test/config.others.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<div>Header</div>" });
assert.equal(config.headerTemplate, "<div>Header</div>");
assert.equal(config.footerTemplate, "<div/>");
assert.equal(config.displayHeaderFooter, true);
});
it("Should have footer", function () {
const config = new Config({ footerTemplate: "<div>footer</div>" });
assert.equal(config.headerTemplate, "<div/>");
assert.equal(config.footerTemplate, "<div>footer</div>");
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);
});
});
});
Loading