> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/microsoft/playwright-mcp/llms.txt
> Use this file to discover all available pages before exploring further.

# PDF Generation

> Save web pages as PDF documents

<Warning>
  PDF generation requires the `pdf` capability. Start the server with `--caps=pdf` to enable this tool.
</Warning>

Playwright MCP provides PDF generation capabilities, allowing you to save web pages as PDF documents. This is useful for creating reports, archiving web content, or generating printable versions of web pages.

## Enabling PDF Capability

To use PDF generation tools, start the MCP server with the pdf capability:

```bash theme={null}
npx @playwright/mcp-server --caps=pdf
```

You can combine it with other capabilities:

```bash theme={null}
npx @playwright/mcp-server --caps=vision,pdf,testing
```

## browser\_pdf\_save

Save the current page as a PDF document.

<ParamField path="filename" type="string">
  File name to save the PDF to. Defaults to `page-{timestamp}.pdf` if not specified. Prefer relative file names to stay within the output directory.
</ParamField>

**Read-only**: Yes

```javascript theme={null}
// Example: Save page as PDF with custom filename
{
  "tool": "browser_pdf_save",
  "arguments": {
    "filename": "report.pdf"
  }
}
```

```javascript theme={null}
// Example: Save with default filename (page-{timestamp}.pdf)
{
  "tool": "browser_pdf_save",
  "arguments": {}
}
```

## Use Cases

### Generate Reports

Create PDF reports from web dashboards or analytics pages:

```javascript theme={null}
// Navigate to dashboard
{ "tool": "browser_navigate", "arguments": { "url": "https://example.com/dashboard" } }

// Wait for data to load
{ "tool": "browser_wait_for", "arguments": { "time": 2 } }

// Save as PDF
{ "tool": "browser_pdf_save", "arguments": { "filename": "monthly-report.pdf" } }
```

### Archive Web Content

Preserve web content as PDF for archival purposes:

```javascript theme={null}
// Navigate to article
{ "tool": "browser_navigate", "arguments": { "url": "https://blog.example.com/article" } }

// Save article as PDF
{ "tool": "browser_pdf_save", "arguments": { "filename": "article-archive.pdf" } }
```

### Batch PDF Generation

Generate multiple PDFs from different pages:

```javascript theme={null}
// Page 1
{ "tool": "browser_navigate", "arguments": { "url": "https://example.com/page1" } }
{ "tool": "browser_pdf_save", "arguments": { "filename": "page1.pdf" } }

// Page 2
{ "tool": "browser_navigate", "arguments": { "url": "https://example.com/page2" } }
{ "tool": "browser_pdf_save", "arguments": { "filename": "page2.pdf" } }

// Page 3
{ "tool": "browser_navigate", "arguments": { "url": "https://example.com/page3" } }
{ "tool": "browser_pdf_save", "arguments": { "filename": "page3.pdf" } }
```

### Create Printable Versions

Generate printer-friendly PDFs of web content:

```javascript theme={null}
// Navigate to content
{ "tool": "browser_navigate", "arguments": { "url": "https://docs.example.com" } }

// Click print-friendly version button if available
{ "tool": "browser_click", "arguments": { "element": "Print view button", "ref": "ref_123" } }

// Save as PDF
{ "tool": "browser_pdf_save", "arguments": { "filename": "documentation.pdf" } }
```

## PDF Formatting Tips

<AccordionGroup>
  <Accordion title="Wait for Content to Load">
    Always wait for dynamic content to fully load before generating PDFs. Use `browser_wait_for` with appropriate time or text conditions.
  </Accordion>

  <Accordion title="Consider Page Width">
    The PDF will reflect the current browser viewport size. Use `browser_resize` to set an appropriate width for PDF generation:

    ```javascript theme={null}
    { "tool": "browser_resize", "arguments": { "width": 1200, "height": 800 } }
    ```
  </Accordion>

  <Accordion title="Handle Print Stylesheets">
    Many websites have special CSS for printing. The PDF will use these print stylesheets if they exist, which may differ from the on-screen view.
  </Accordion>

  <Accordion title="Scroll to Load Lazy Images">
    Some pages use lazy loading for images. Scroll through the page before generating the PDF to ensure all images are loaded:

    ```javascript theme={null}
    // Scroll to bottom to trigger lazy loading
    { "tool": "browser_evaluate", "arguments": { "function": "() => window.scrollTo(0, document.body.scrollHeight)" } }
    { "tool": "browser_wait_for", "arguments": { "time": 1 } }
    // Scroll back to top
    { "tool": "browser_evaluate", "arguments": { "function": "() => window.scrollTo(0, 0)" } }
    // Generate PDF
    { "tool": "browser_pdf_save", "arguments": { "filename": "complete.pdf" } }
    ```
  </Accordion>
</AccordionGroup>

## File Naming

When specifying filenames for PDFs:

* **Relative paths**: Preferred. Will be saved in the output directory.
  ```javascript theme={null}
  { "filename": "report.pdf" }
  { "filename": "reports/monthly-report.pdf" }
  ```

* **Absolute paths**: Also supported, but relative paths are recommended.
  ```javascript theme={null}
  { "filename": "/home/user/documents/report.pdf" }
  ```

* **Default naming**: If no filename is provided, a timestamped name is used.
  ```javascript theme={null}
  // Creates: page-1709740800000.pdf
  { }
  ```

<Tip>
  Use descriptive filenames that indicate the content and date, e.g., `analytics-report-2024-03.pdf`
</Tip>

## Combining with Other Tools

PDF generation works well with other Playwright MCP tools:

```javascript theme={null}
// Complete workflow: Navigate, interact, and generate PDF

// 1. Navigate to page
{ "tool": "browser_navigate", "arguments": { "url": "https://example.com/report" } }

// 2. Set date range
{ "tool": "browser_click", "arguments": { "element": "Date picker", "ref": "ref_100" } }
{ "tool": "browser_select_option", "arguments": { "element": "Month dropdown", "ref": "ref_101", "values": ["March"] } }

// 3. Wait for report to generate
{ "tool": "browser_wait_for", "arguments": { "text": "Report complete" } }

// 4. Resize for optimal PDF layout
{ "tool": "browser_resize", "arguments": { "width": 1400, "height": 900 } }

// 5. Save as PDF
{ "tool": "browser_pdf_save", "arguments": { "filename": "march-report.pdf" } }
```

## Common Issues

<Warning>
  If you get an error about PDF capability not being enabled, make sure you started the server with `--caps=pdf`.
</Warning>

<AccordionGroup>
  <Accordion title="Missing Content in PDF">
    If content is missing from the PDF:

    * Wait longer for dynamic content to load
    * Scroll through the page to trigger lazy-loaded elements
    * Check if the page uses JavaScript to render content
  </Accordion>

  <Accordion title="PDF Layout Issues">
    If the PDF layout looks wrong:

    * Adjust the browser viewport size with `browser_resize`
    * Check if the site has print-specific CSS that affects layout
    * Some interactive elements may not render well in PDF
  </Accordion>

  <Accordion title="File Not Found">
    If the PDF file isn't where you expect:

    * Use relative paths to save in the output directory
    * Check the tool response for the actual save location
    * Verify you have write permissions to the target directory
  </Accordion>
</AccordionGroup>

## Related Tools

<CardGroup cols={2}>
  <Card title="Take Screenshot" icon="camera" href="/tools/core-automation#browser_take_screenshot">
    Capture page as image instead of PDF
  </Card>

  <Card title="Browser Resize" icon="arrows-alt" href="/tools/core-automation#browser_resize">
    Adjust viewport size for optimal PDF layout
  </Card>

  <Card title="Wait For" icon="clock" href="/tools/core-automation#browser_wait_for">
    Wait for content to load before generating PDF
  </Card>
</CardGroup>
