Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,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 @@ -1720,18 +1736,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 @@ -1790,6 +1806,7 @@ export class Page extends EventEmitter {
preferCSSPageSize = false,
margin = {},
path = null,
omitBackground = false,
} = options;

let paperWidth = 8.5;
Expand All @@ -1810,6 +1827,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 @@ -1827,6 +1848,11 @@ export class Page extends EventEmitter {
pageRanges,
preferCSSPageSize,
});

if (omitBackground) {
await this._resetDefaultBackgroundColor();
}

return await helper.readProtocolStream(this._client, result.stream, path);
}

Expand Down