playwright_save_as_pdf
Convert web pages to PDF files with specified format, background settings, and margins. Save the generated PDF to a designated output path for easy access and sharing.
Instructions
Save the current page as a PDF file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | No | Name of the PDF file (default: page.pdf) | |
| format | No | Page format (e.g. 'A4', 'Letter') | |
| margin | No | Page margins | |
| outputPath | Yes | Directory path where PDF will be saved | |
| printBackground | No | Whether to print background graphics |
Implementation Reference
- src/tools/browser/output.ts:8-31 (handler)The SaveAsPdfTool class provides the core handler logic for saving the current Playwright page as a PDF file using page.pdf() with configurable options.export class SaveAsPdfTool extends BrowserToolBase { /** * Execute the save as PDF tool */ 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}`); }); } }
- src/tools.ts:411-434 (schema)Tool definition including name, description, and input schema for validation.{ name: "playwright_save_as_pdf", description: "Save the current page as a PDF file", inputSchema: { type: "object", properties: { outputPath: { type: "string", description: "Directory path where PDF will be saved" }, filename: { type: "string", description: "Name of the PDF file (default: page.pdf)" }, format: { type: "string", description: "Page format (e.g. 'A4', 'Letter')" }, printBackground: { type: "boolean", description: "Whether to print background graphics" }, margin: { type: "object", description: "Page margins", properties: { top: { type: "string" }, right: { type: "string" }, bottom: { type: "string" }, left: { type: "string" } } } }, required: ["outputPath"], }, },
- src/toolHandler.ts:548-549 (registration)Switch case in handleToolCall that routes the tool call to the SaveAsPdfTool instance.case "playwright_save_as_pdf": return await saveAsPdfTool.execute(args, context);
- src/tools.ts:471-471 (registration)Inclusion in BROWSER_TOOLS array for conditional browser launch handling."playwright_save_as_pdf",
- src/toolHandler.ts:347-347 (helper)Instantiation of the SaveAsPdfTool instance in initializeTools.if (!saveAsPdfTool) saveAsPdfTool = new SaveAsPdfTool(server);