Implementation:Puppeteer Puppeteer Page Pdf
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Document_Generation |
| Last Updated | 2026-02-11 23:00 GMT |
Overview
Concrete tool for generating PDF documents from rendered page content, provided by the puppeteer-core library.
Description
The Page.pdf() method generates a PDF of the current page. It uses the browser's built-in print functionality, rendering the page with print media styles. PDF options types and paper format constants are defined in packages/puppeteer-core/src/common/PDFOptions.ts.
The method is implemented by both CDP (using Page.printToPDF) and BiDi (using the print command) backends.
Usage
Call this method after the page has fully loaded. The browser must be running in headless mode. Use waitUntil: 'networkidle0' in the preceding goto() call to ensure all content is loaded before PDF generation.
Code Reference
Source Location
- Repository: puppeteer
- File: packages/puppeteer-core/src/api/Page.ts (abstract declaration)
- Lines: 2771
- PDF Options Types: packages/puppeteer-core/src/common/PDFOptions.ts (lines 1-274)
Signature
abstract class Page {
abstract pdf(options?: PDFOptions): Promise<Uint8Array>;
}
Import
// Accessed through a Page instance
const page = await browser.newPage();
await page.pdf(options);
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| options | PDFOptions | No | PDF generation configuration |
| options.path | string | No | File path to save the PDF |
| options.format | PaperFormat | No | Paper format: 'A4', 'Letter', 'Legal', 'Tabloid', etc. (default: 'Letter') |
| options.width | string or number | No | Custom page width (overrides format) |
| options.height | string or number | No | Custom page height (overrides format) |
| options.margin | PDFMargin | No | Margins: {top, right, bottom, left} as CSS values |
| options.printBackground | boolean | No | Include CSS background colors/images (default: false) |
| options.landscape | boolean | No | Landscape orientation (default: false) |
| options.scale | number | No | Scale factor 0.1-2.0 (default: 1) |
| options.displayHeaderFooter | boolean | No | Show header and footer (default: false) |
| options.headerTemplate | string | No | HTML template for page header |
| options.footerTemplate | string | No | HTML template for page footer |
| options.pageRanges | string | No | Page ranges to print, e.g. '1-5, 8' |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Promise<Uint8Array> | PDF file content as binary data |
Usage Examples
Basic PDF Generation
const puppeteer = require('puppeteer');
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com', {waitUntil: 'networkidle0'});
await page.pdf({path: 'page.pdf', format: 'A4'});
await browser.close();
PDF With Custom Margins And Background
await page.pdf({
path: 'styled.pdf',
format: 'A4',
printBackground: true,
margin: {
top: '1cm',
right: '1cm',
bottom: '1cm',
left: '1cm',
},
});
await page.pdf({
path: 'report.pdf',
format: 'Letter',
displayHeaderFooter: true,
headerTemplate: '<div style="font-size:10px; text-align:center; width:100%;">Report Title</div>',
footerTemplate: '<div style="font-size:10px; text-align:center; width:100%;"><span class="pageNumber"></span> / <span class="totalPages"></span></div>',
margin: {top: '2cm', bottom: '2cm'},
});