playwright_save_as_pdf
Convert web pages to PDF files with customizable formatting options including page size, margins, and background graphics.
Instructions
Save the current page as a PDF file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| outputPath | Yes | Directory path where PDF will be saved | |
| filename | No | Name of the PDF file (default: page.pdf) | |
| format | No | Page format (e.g. 'A4', 'Letter') | |
| printBackground | No | Whether to print background graphics | |
| margin | No | Page margins |
Implementation Reference
- src/tools/browser/output.ts:13-49 (handler)Implements the core logic: generates PDF options from args, calls page.pdf(), registers the saved PDF as an MCP resource, and returns a success response with the resource URI.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); let resourceLink: Awaited<ReturnType<typeof registerFileResource>> | undefined; let savedLocation = options.path; try { resourceLink = await registerFileResource({ filePath: options.path, name: filename, mimeType: "application/pdf", server: this.server, }); if (resourceLink?.uri) { savedLocation = resourceLink.uri; } } catch (error) { console.warn("Failed to register PDF as resource:", error); } return { ...createSuccessResponse(`Saved page as PDF: ${savedLocation}`), ...(resourceLink ? { resourceLinks: [resourceLink] } : {}), }; }); }
- src/tools.ts:455-477 (schema)Defines the tool name, description, and input schema for validation including required outputPath and optional PDF options.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:649-650 (registration)Dispatches tool calls matching 'playwright_save_as_pdf' to the SaveAsPdfTool instance's execute method in the main handleToolCall switch statement.case "playwright_save_as_pdf": return await saveAsPdfTool.execute(args, context);
- src/tools.ts:514-514 (registration)Includes the tool in BROWSER_TOOLS array, which determines when to ensure browser context is available before execution."playwright_save_as_pdf",
- src/toolHandler.ts:34-34 (registration)Imports the SaveAsPdfTool class required for instantiation and execution.import { SaveAsPdfTool } from "./tools/browser/output.js";