From f8040cb2a29214915900231c32103211f24f9f3f Mon Sep 17 00:00:00 2001 From: Audrius Jakumavicius Date: Wed, 20 Dec 2017 01:54:34 +0100 Subject: [PATCH] feat(pdf): add `headerTemplate` and `footerTemplate` to available PDF options (#1625) This patch allows specifying header and footer templates for PDF printing. Fixes #373. --- docs/api.md | 7 +++++++ lib/Page.js | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/docs/api.md b/docs/api.md index 7a90a79ac8d..0e20758280a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -891,6 +891,13 @@ Page is guaranteed to have a main frame which persists during navigations. - `path` <[string]> The file path to save the PDF to. If `path` is a relative path, then it is resolved relative to [current working directory](https://nodejs.org/api/process.html#process_process_cwd). If no path is provided, the PDF won't be saved to the disk. - `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. - `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`. + - `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: + - `date` formatted print date + - `title` document title + - `url` document location + - `pageNumber` current page number + - `totalPages` total pages in the document + - `footerTemplate` <[string]> HTML template for the print footer. Should use the same format as the `headerTemplate`. - `printBackground` <[boolean]> Print background graphics. Defaults to `false`. - `landscape` <[boolean]> Paper orientation. Defaults to `false`. - `pageRanges` <[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages. diff --git a/lib/Page.js b/lib/Page.js index 2146aac1a49..297a7064213 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -711,6 +711,8 @@ class Page extends EventEmitter { async pdf(options = {}) { const scale = options.scale || 1; const displayHeaderFooter = !!options.displayHeaderFooter; + const headerTemplate = options.headerTemplate || ''; + const footerTemplate = options.footerTemplate || ''; const printBackground = !!options.printBackground; const landscape = !!options.landscape; const pageRanges = options.pageRanges || ''; @@ -736,6 +738,8 @@ class Page extends EventEmitter { const result = await this._client.send('Page.printToPDF', { landscape: landscape, displayHeaderFooter: displayHeaderFooter, + headerTemplate: headerTemplate, + footerTemplate: footerTemplate, printBackground: printBackground, scale: scale, paperWidth: paperWidth,