Skip to main content
Glama

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
NameRequiredDescriptionDefault
outputPathYesDirectory path where PDF will be saved
filenameNoName of the PDF file (default: page.pdf)
formatNoPage format (e.g. 'A4', 'Letter')
printBackgroundNoWhether to print background graphics
marginNoPage margins

Implementation Reference

  • 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] } : {}),
        };
      });
    }
  • 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"],
      },
    },
  • 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",
  • Imports the SaveAsPdfTool class required for instantiation and execution.
    import { SaveAsPdfTool } from "./tools/browser/output.js";

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv1.0.0

TDQS

C2.9/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It states the action ('Save') but lacks critical details: whether this requires specific permissions, if it overwrites existing files, what happens on failure, or typical performance characteristics. The description is too minimal for a mutation tool with no annotation coverage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's function without unnecessary words. It's appropriately sized for a straightforward tool and front-loads the core purpose effectively.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a mutation tool with no annotations and no output schema, the description is inadequate. It doesn't explain what the tool returns (e.g., success confirmation, file path), error conditions, or behavioral nuances. Given the complexity of PDF generation with multiple parameters, more context is needed for effective use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all 5 parameters. The description adds no parameter-specific information beyond what's in the schema, meeting the baseline score of 3 for high schema coverage without additional value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Save') and resource ('current page as a PDF file'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'playwright_screenshot' which captures images rather than PDFs, preventing a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., needing an active page context), compare it to 'playwright_screenshot' for different output formats, or specify scenarios where PDF generation is preferred over other capture methods.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.