Skip to content

Commit

Permalink
feat: support timeout for page.pdf() call (#7508)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikli2009 committed Sep 14, 2021
1 parent a0b1f6b commit f90af66
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/api.md
Expand Up @@ -1712,7 +1712,6 @@ await page.evaluate(() => matchMedia('print').matches);

> **NOTE** This does not affect WebSockets and WebRTC PeerConnections (see https://crbug.com/563644). To set the page offline, you can use [page.setOfflineMode(enabled)](#pagesetofflinemodeenabled).

```js
const puppeteer = require('puppeteer');
const slow3G = puppeteer.networkConditions['Slow 3G'];
Expand Down Expand Up @@ -2086,6 +2085,7 @@ Page is guaranteed to have a main frame which persists during navigations.
- `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`.
- `timeout` <[number]> Maximum time in milliseconds, defaults to 30 seconds, pass `0` to disable timeout.
- returns: <[Promise]<[Buffer]>> Promise which resolves with PDF buffer.

> **NOTE** Generating a pdf is currently only supported in Chrome headless.
Expand Down Expand Up @@ -2328,7 +2328,7 @@ await page.setGeolocation({ latitude: 59.95, longitude: 30.31667 });
#### page.setOfflineMode(enabled)

- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- `enabled` <[boolean]> When `true`, enables offline mode for the page.
- returns: <[Promise]>

> **NOTE** while this method sets the network connection to offline, it does not change the parameters used in [page.emulateNetworkConditions(networkConditions)](#pageemulatenetworkconditionsnetworkconditions).
Expand Down
5 changes: 5 additions & 0 deletions src/common/PDFOptions.ts
Expand Up @@ -156,6 +156,11 @@ export interface PDFOptions {
* @defaultValue false
*/
omitBackground?: boolean;
/**
* Timeout in milliseconds
* @defaultValue 30000
*/
timeout?: number;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/common/Page.ts
Expand Up @@ -2748,6 +2748,7 @@ export class Page extends EventEmitter {
preferCSSPageSize = false,
margin = {},
omitBackground = false,
timeout = 30000,
} = options;

let paperWidth = 8.5;
Expand All @@ -2772,7 +2773,7 @@ export class Page extends EventEmitter {
await this._setTransparentBackgroundColor();
}

const result = await this._client.send('Page.printToPDF', {
const printCommandPromise = this._client.send('Page.printToPDF', {
transferMode: 'ReturnAsStream',
landscape,
displayHeaderFooter,
Expand All @@ -2790,6 +2791,12 @@ export class Page extends EventEmitter {
preferCSSPageSize,
});

const result = await helper.waitWithTimeout(
printCommandPromise,
'Page.printToPDF',
timeout
);

if (omitBackground) {
await this._resetDefaultBackgroundColor();
}
Expand Down
11 changes: 11 additions & 0 deletions test/assets/pdf.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PDF</title>
</head>
<body>
<div>PDF Content</div>
</body>
</html>
11 changes: 11 additions & 0 deletions test/page.spec.ts
Expand Up @@ -1639,6 +1639,17 @@ describe('Page', function () {
}
expect(size).toBeGreaterThan(0);
});

it('should respect timeout', async () => {
const { isHeadless, page, server, puppeteer } = getTestState();
if (!isHeadless) return;

await page.goto(server.PREFIX + '/pdf.html');

let error = null;
await page.pdf({ timeout: 1 }).catch((_error) => (error = _error));
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
});
});

describe('Page.title', function () {
Expand Down

0 comments on commit f90af66

Please sign in to comment.