savePDF
Prints the page of the current browsing context to a PDF file on your OS.
savePDF(filepath, options?) writes the PDF and returns the same bytes as a Buffer.
filepath is resolved from the execution directory and must end with .pdf.
A WebDriver BiDi session (browser.isBidi) renders the PDF with
browsingContext.print.
That is how Chrome, Edge, and Firefox print, headed and headless. Headed and headless Chrome
both return a non-empty PDF on this path. Only options you set are sent, so the browser keeps
its defaults for the rest. context is the current top-level browsing context. If the browser
rejects the command with unsupported operation, that error is returned as-is. The command
does not fall back to Classic printPage.
A WebDriver Classic session renders the PDF with
printPage. Chrome's Classic endpoint was
limited to headless mode
(crbug 753118).
That limit is specific to Classic printPage. Current Chrome can also print from a headed
Classic session; an older Chrome that still refuses it should use a BiDi session or run
Classic Chrome headless.
Lengths are centimeters. Defaults match both protocols: portrait, scale 1, background
false, shrink-to-fit true, page 21.59 Ă— 27.94 cm, and margins of 1 cm.
pageRanges entries are page numbers or range strings such as '1-3'.
Usage​
await browser.savePDF(filepath, { orientation, scale, background, width, height, top, bottom, left, right, shrinkToFit, pageRanges })
Parameters​
| Name | Type | Details |
|---|---|---|
filepath | string | path to the generated PDF (.pdf suffix is required) relative to the execution directory |
optionsoptional | PDFPrintOptions | print options, in centimeters |
options.orientationoptional | string | page orientation: portrait or landscape. Default: portrait |
options.scaleoptional | number | page scale from 0.1 to 2. Default: 1 |
options.backgroundoptional | boolean | include the page background. Default: false |
options.widthoptional | number | page width in cm. Default: 21.59 |
options.heightoptional | number | page height in cm. Default: 27.94 |
options.topoptional | number | top margin in cm. Default: 1 |
options.bottomoptional | number | bottom margin in cm. Default: 1 |
options.leftoptional | number | left margin in cm. Default: 1 |
options.rightoptional | number | right margin in cm. Default: 1 |
options.shrinkToFitoptional | boolean | shrink the page to fit. Default: true |
options.pageRangesoptional | Array<string, number> | pages to include, as numbers or range strings such as '1-3'. Default: [] |
Example​
it('should save a PDF of the browser view', async () => {
const pdf = await browser.savePDF('./some/path/page.pdf');
console.log(pdf.subarray(0, 5).toString()); // outputs: "%PDF-"
});
it('should save a landscape PDF with a custom margin', async () => {
await browser.savePDF('./some/path/landscape.pdf', {
orientation: 'landscape',
left: 2
});
});
Returns​
- <Buffer>
return: PDF buffer
Throws​
- Error: if
filepathis not a string ending in.pdf, or iforientationis notportraitorlandscape