feat(page): add omitBackground option for page.pdf method (#6981)

This commit is contained in:
Daniel Rowe 2021-03-18 12:57:32 -07:00 committed by GitHub
parent b8d72cb039
commit dc8ab6d8ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 6 deletions

View File

@ -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.

View File

@ -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;
}
/**

View File

@ -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<void> {
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);
}