Skip to main content
Glama

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
NameRequiredDescriptionDefault
filenameNopage.pdf
formatNoA4
marginNo
outputPathYes
printBackgroundNo

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 )
  • 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"}) """

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dragons96/mcp-undetected-chromedriver'

If you have feedback or need assistance with the MCP directory API, please join our Discord server