playwright-core
Prints a browser page to PDF through page.pdf, using print CSS media.
- Downloads / mo
- 712.5M
- Stars
- 100.4K
- Last release
- 1.65.1
- License
- Apache-2.0
Strengths
- Header and footer templates. Set displayHeaderFooter with HTML templates for the print header and footer.
Limitations
- PDF needs headless Chromium. Launch headless Chromium for PDF output, because other engines reject page.pdf.
- Templates ignore page styles. Style header and footer templates inline, because page styles do not reach them.
Quickstart
import { chromium } from 'playwright-core'; const browser = await chromium.launch({ channel: 'chrome', headless: true }); const page = await browser.newPage(); await page.setContent('<h1>Hello</h1>'); await page.pdf({ path: 'output.pdf' }); await browser.close();
playwright-core examples
async execute(args: any, context: ToolContext): Promise<ToolResponse> { return this.safeExecute(context, async (page) => { const filename = args.filename || 'page.pdf'; const options = { path: path.resolve(args.outputPath || '.', filename), format: args.format || 'A4', printBackground: args.printBackground !== false, margin: args.margin || { top: '1cm', right: '1cm', bottom: '1cm', left: '1cm' } }; await page.pdf(options); return createSuccessResponse(`Saved page as PDF: ${options.path}`); }); }