diff --git a/docs/api.md b/docs/api.md index 892ea053e42ea..92e0b26bd9724 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1825,6 +1825,7 @@ Page is guaranteed to have a main frame which persists during navigations. - `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units. - `left` <[string]|[number]> Left margin, accepts values labeled with units. - `preferCSSPageSize` <[boolean]> Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size. + - `omitBackground` <[boolean]> Hides default white background and allows capturing screenshots with transparency. Defaults to `false`. - returns: <[Promise]<[Buffer]>> Promise which resolves with PDF buffer. > **NOTE** Generating a pdf is currently only supported in Chrome headless. diff --git a/src/common/PDFOptions.ts b/src/common/PDFOptions.ts index 743085904a476..6c58300a02cb9 100644 --- a/src/common/PDFOptions.ts +++ b/src/common/PDFOptions.ts @@ -151,6 +151,11 @@ export interface PDFOptions { * @defaultValue the empty string, which means the PDF will not be written to disk. */ path?: string; + /** + * Hides default white background and allows generating pdfs with transparency. + * @defaultValue false + */ + omitBackground?: boolean; } /** diff --git a/src/common/Page.ts b/src/common/Page.ts index b708c23ba9b7c..aff7615a39fab 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -1288,6 +1288,22 @@ export class Page extends EventEmitter { this.emit(PageEmittedEvents.Dialog, dialog); } + /** + * Resets default white background + */ + private async _resetDefaultBackgroundColor() { + await this._client.send('Emulation.setDefaultBackgroundColorOverride'); + } + + /** + * Hides default white background + */ + private async _setTransparentBackgroundColor(): Promise { + await this._client.send('Emulation.setDefaultBackgroundColorOverride', { + color: { r: 0, g: 0, b: 0, a: 0 }, + }); + } + url(): string { return this.mainFrame().url(); } @@ -1737,18 +1753,18 @@ export class Page extends EventEmitter { } const shouldSetDefaultBackground = options.omitBackground && format === 'png'; - if (shouldSetDefaultBackground) - await this._client.send('Emulation.setDefaultBackgroundColorOverride', { - color: { r: 0, g: 0, b: 0, a: 0 }, - }); + if (shouldSetDefaultBackground) { + await this._setTransparentBackgroundColor(); + } const result = await this._client.send('Page.captureScreenshot', { format, quality: options.quality, clip, captureBeyondViewport: true, }); - if (shouldSetDefaultBackground) - await this._client.send('Emulation.setDefaultBackgroundColorOverride'); + if (shouldSetDefaultBackground) { + await this._resetDefaultBackgroundColor(); + } if (options.fullPage && this._viewport) await this.setViewport(this._viewport); @@ -1810,6 +1826,7 @@ export class Page extends EventEmitter { preferCSSPageSize = false, margin = {}, path = null, + omitBackground = false, } = options; let paperWidth = 8.5; @@ -1830,6 +1847,10 @@ export class Page extends EventEmitter { const marginBottom = convertPrintParameterToInches(margin.bottom) || 0; const marginRight = convertPrintParameterToInches(margin.right) || 0; + if (omitBackground) { + await this._setTransparentBackgroundColor(); + } + const result = await this._client.send('Page.printToPDF', { transferMode: 'ReturnAsStream', landscape, @@ -1847,6 +1868,11 @@ export class Page extends EventEmitter { pageRanges, preferCSSPageSize, }); + + if (omitBackground) { + await this._resetDefaultBackgroundColor(); + } + return await helper.readProtocolStream(this._client, result.stream, path); }