mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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).
|
> **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
|
```js
|
||||||
const puppeteer = require('puppeteer');
|
const puppeteer = require('puppeteer');
|
||||||
const slow3G = puppeteer.networkConditions['Slow 3G'];
|
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.
|
- `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`.
|
- `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.
|
- 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.
|
||||||
@ -2328,7 +2328,7 @@ await page.setGeolocation({ latitude: 59.95, longitude: 30.31667 });
|
|||||||
|
|
||||||
#### page.setOfflineMode(enabled)
|
#### 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]>
|
- returns: <[Promise]>
|
||||||
|
|
||||||
> **NOTE** while this method sets the network connection to offline, it does not change the parameters used in [page.emulateNetworkConditions(networkConditions)](#pageemulatenetworkconditionsnetworkconditions).
|
> **NOTE** while this method sets the network connection to offline, it does not change the parameters used in [page.emulateNetworkConditions(networkConditions)](#pageemulatenetworkconditionsnetworkconditions).
|
||||||
|
@ -156,6 +156,11 @@ export interface PDFOptions {
|
|||||||
* @defaultValue false
|
* @defaultValue false
|
||||||
*/
|
*/
|
||||||
omitBackground?: boolean;
|
omitBackground?: boolean;
|
||||||
|
/**
|
||||||
|
* Timeout in milliseconds
|
||||||
|
* @defaultValue 30000
|
||||||
|
*/
|
||||||
|
timeout?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2748,6 +2748,7 @@ export class Page extends EventEmitter {
|
|||||||
preferCSSPageSize = false,
|
preferCSSPageSize = false,
|
||||||
margin = {},
|
margin = {},
|
||||||
omitBackground = false,
|
omitBackground = false,
|
||||||
|
timeout = 30000,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
let paperWidth = 8.5;
|
let paperWidth = 8.5;
|
||||||
@ -2772,7 +2773,7 @@ export class Page extends EventEmitter {
|
|||||||
await this._setTransparentBackgroundColor();
|
await this._setTransparentBackgroundColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this._client.send('Page.printToPDF', {
|
const printCommandPromise = this._client.send('Page.printToPDF', {
|
||||||
transferMode: 'ReturnAsStream',
|
transferMode: 'ReturnAsStream',
|
||||||
landscape,
|
landscape,
|
||||||
displayHeaderFooter,
|
displayHeaderFooter,
|
||||||
@ -2790,6 +2791,12 @@ export class Page extends EventEmitter {
|
|||||||
preferCSSPageSize,
|
preferCSSPageSize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const result = await helper.waitWithTimeout(
|
||||||
|
printCommandPromise,
|
||||||
|
'Page.printToPDF',
|
||||||
|
timeout
|
||||||
|
);
|
||||||
|
|
||||||
if (omitBackground) {
|
if (omitBackground) {
|
||||||
await this._resetDefaultBackgroundColor();
|
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);
|
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 () {
|
describe('Page.title', function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user