feat: support timeout for page.pdf() call (#7508)
This commit is contained in:
parent
a0b1f6b401
commit
f90af6639d
@ -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'];
|
||||
@ -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.
|
||||
|
@ -156,6 +156,11 @@ export interface PDFOptions {
|
||||
* @defaultValue false
|
||||
*/
|
||||
omitBackground?: boolean;
|
||||
/**
|
||||
* Timeout in milliseconds
|
||||
* @defaultValue 30000
|
||||
*/
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2748,6 +2748,7 @@ export class Page extends EventEmitter {
|
||||
preferCSSPageSize = false,
|
||||
margin = {},
|
||||
omitBackground = false,
|
||||
timeout = 30000,
|
||||
} = options;
|
||||
|
||||
let paperWidth = 8.5;
|
||||
@ -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,
|
||||
@ -2790,6 +2791,12 @@ export class Page extends EventEmitter {
|
||||
preferCSSPageSize,
|
||||
});
|
||||
|
||||
const result = await helper.waitWithTimeout(
|
||||
printCommandPromise,
|
||||
'Page.printToPDF',
|
||||
timeout
|
||||
);
|
||||
|
||||
if (omitBackground) {
|
||||
await this._resetDefaultBackgroundColor();
|
||||
}
|
||||
|
11
test/assets/pdf.html
Normal file
11
test/assets/pdf.html
Normal file
@ -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>
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user