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. - `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units.
- `left` <[string]|[number]> Left 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. - `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. - returns: <[Promise]<[Buffer]>> Promise which resolves with PDF buffer.
> **NOTE** Generating a pdf is currently only supported in Chrome headless. > **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. * @defaultValue the empty string, which means the PDF will not be written to disk.
*/ */
path?: string; 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); 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 { url(): string {
return this.mainFrame().url(); return this.mainFrame().url();
} }
@ -1737,18 +1753,18 @@ export class Page extends EventEmitter {
} }
const shouldSetDefaultBackground = const shouldSetDefaultBackground =
options.omitBackground && format === 'png'; options.omitBackground && format === 'png';
if (shouldSetDefaultBackground) if (shouldSetDefaultBackground) {
await this._client.send('Emulation.setDefaultBackgroundColorOverride', { await this._setTransparentBackgroundColor();
color: { r: 0, g: 0, b: 0, a: 0 }, }
});
const result = await this._client.send('Page.captureScreenshot', { const result = await this._client.send('Page.captureScreenshot', {
format, format,
quality: options.quality, quality: options.quality,
clip, clip,
captureBeyondViewport: true, captureBeyondViewport: true,
}); });
if (shouldSetDefaultBackground) if (shouldSetDefaultBackground) {
await this._client.send('Emulation.setDefaultBackgroundColorOverride'); await this._resetDefaultBackgroundColor();
}
if (options.fullPage && this._viewport) if (options.fullPage && this._viewport)
await this.setViewport(this._viewport); await this.setViewport(this._viewport);
@ -1810,6 +1826,7 @@ export class Page extends EventEmitter {
preferCSSPageSize = false, preferCSSPageSize = false,
margin = {}, margin = {},
path = null, path = null,
omitBackground = false,
} = options; } = options;
let paperWidth = 8.5; let paperWidth = 8.5;
@ -1830,6 +1847,10 @@ export class Page extends EventEmitter {
const marginBottom = convertPrintParameterToInches(margin.bottom) || 0; const marginBottom = convertPrintParameterToInches(margin.bottom) || 0;
const marginRight = convertPrintParameterToInches(margin.right) || 0; const marginRight = convertPrintParameterToInches(margin.right) || 0;
if (omitBackground) {
await this._setTransparentBackgroundColor();
}
const result = await this._client.send('Page.printToPDF', { const result = await this._client.send('Page.printToPDF', {
transferMode: 'ReturnAsStream', transferMode: 'ReturnAsStream',
landscape, landscape,
@ -1847,6 +1868,11 @@ export class Page extends EventEmitter {
pageRanges, pageRanges,
preferCSSPageSize, preferCSSPageSize,
}); });
if (omitBackground) {
await this._resetDefaultBackgroundColor();
}
return await helper.readProtocolStream(this._client, result.stream, path); return await helper.readProtocolStream(this._client, result.stream, path);
} }