Skip to main content

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​

NameTypeDetails
filepathstringpath to the generated PDF (.pdf suffix is required) relative to the execution directory
options
optional
PDFPrintOptionsprint options, in centimeters
options.orientation
optional
stringpage orientation: portrait or landscape. Default: portrait
options.scale
optional
numberpage scale from 0.1 to 2. Default: 1
options.background
optional
booleaninclude the page background. Default: false
options.width
optional
numberpage width in cm. Default: 21.59
options.height
optional
numberpage height in cm. Default: 27.94
options.top
optional
numbertop margin in cm. Default: 1
options.bottom
optional
numberbottom margin in cm. Default: 1
options.left
optional
numberleft margin in cm. Default: 1
options.right
optional
numberright margin in cm. Default: 1
options.shrinkToFit
optional
booleanshrink the page to fit. Default: true
options.pageRanges
optional
Array<string, number>pages to include, as numbers or range strings such as '1-3'. Default: []

Example​

savePDF.js
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 filepath is not a string ending in .pdf, or if orientation is not portrait or landscape

Welcome! How can I help?

WebdriverIO AI Copilot