browser_save_as_pdf
Convert and save the current browser page as a PDF with customizable options like format, margin, and filename. Ideal for automation, testing, or generating printable content from advanced websites.
Instructions
Save the current page as a PDF
Args:
outputPath: The path to save the PDF to - required
filename: The name of the PDF file - optional, default is "page.pdf"
format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID")
printBackground: Whether to print the background - optional, default is True
margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"})
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | No | page.pdf | |
| format | No | A4 | |
| margin | No | ||
| outputPath | Yes | ||
| printBackground | No |
Implementation Reference
- Implementation of the browser_save_as_pdf tool handler. This async function handles saving the current browser page as a PDF file. It uses Chrome's print_page functionality with customizable options for format, margins, and background printing. The @mcp.tool() decorator registers it as an MCP tool.@mcp.tool() async def browser_save_as_pdf( outputPath: str, filename: str = "page.pdf", format: str = 'A4', printBackground: bool = True, margin: dict = None, ): """Save the current page as a PDF Args: outputPath: The path to save the PDF to - required filename: The name of the PDF file - optional, default is "page.pdf" format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID") printBackground: Whether to print the background - optional, default is True margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"}) """ assert outputPath, "Output path is required" margin = margin or {"top": 0, "right": 0, "bottom": 0, "left": 0} async def save_as_pdf_handler(driver: uc.Chrome): # 确保输出路径存在 os.makedirs(outputPath, exist_ok=True) # 构建完整文件路径 full_path = os.path.join(outputPath, filename) # 设置打印选项 print_options = PrintOptions() print_options.orientation = 'portrait' print_options.scale = 1.0 print_options.background = printBackground print_options.margin_left = margin.get('left', 0) print_options.margin_right = margin.get('right', 0) print_options.margin_top = margin.get('top', 0) print_options.margin_bottom = margin.get('bottom', 0) # 保存PDF文件 data = driver.print_page(print_options) with open(full_path, 'wb') as f: f.write(base64.b64decode(data)) return await create_success_response(f"Saved page as PDF to {full_path}") return await tool.safe_execute( ToolContext(webdriver=await ensure_browser()), save_as_pdf_handler )
- src/mcp_server_undetected_chromedriver/server.py:467-467 (registration)The @mcp.tool() decorator registers the browser_save_as_pdf function as an MCP tool.@mcp.tool()
- Docstring defining the input parameters and their descriptions for the browser_save_as_pdf tool."""Save the current page as a PDF Args: outputPath: The path to save the PDF to - required filename: The name of the PDF file - optional, default is "page.pdf" format: The format of the PDF - optional, default is "A4" (e.g. "A4", "LETTER", "LEGAL", "TABLOID") printBackground: Whether to print the background - optional, default is True margin: The margin of the PDF - optional, default is None (e.g. {"top": "1cm", "right": "1cm", "bottom": "1cm", "left": "1cm"}) """