Skip to content

Commit

Permalink
feat(page): add omitBackground option for page.pdf method (#6981)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rowe committed Mar 18, 2021
1 parent b8d72cb commit dc8ab6d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/api.md
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/common/PDFOptions.ts
Expand Up @@ -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;
}

/**
Expand Down
38 changes: 32 additions & 6 deletions src/common/Page.ts
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1810,6 +1826,7 @@ export class Page extends EventEmitter {
preferCSSPageSize = false,
margin = {},
path = null,
omitBackground = false,
} = options;

let paperWidth = 8.5;
Expand All @@ -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,
Expand All @@ -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);
}

Expand Down

0 comments on commit dc8ab6d

Please sign in to comment.