Skip to main content
Glama

generate_pdf_from_html

Convert HTML content to PDF files with customizable formatting options and automatic saving to specified directories.

Instructions

Generate a PDF from HTML content using Puppeteer

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
html_contentYesHTML content to convert to PDF
optionsNoPDF generation options
output_dirNoOutput directory (optional, defaults to Downloads)/root/Downloads
output_filenameYesName of the output PDF file (without path)

Implementation Reference

  • Handler for the 'generate_pdf_from_html' tool. Validates output path, launches Puppeteer, renders HTML to PDF with options, saves file, and returns success message.
    case "generate_pdf_from_html": { const { html_content, output_filename, output_dir = DEFAULT_OUTPUT_DIR, options = {} } = args as any; const outputPath = validateOutputPath(path.join(output_dir, output_filename)); await fs.mkdir(path.dirname(outputPath), { recursive: true }); const browser = await puppeteer.launch({ headless: true }); const page = await browser.newPage(); await page.setContent(html_content, { waitUntil: 'networkidle0' }); const pdfOptions = { path: outputPath, format: options.format || 'A4', margin: options.margin || { top: '1cm', right: '1cm', bottom: '1cm', left: '1cm' }, printBackground: true }; await page.pdf(pdfOptions as any); await browser.close(); return { content: [ { type: "text", text: `PDF successfully generated from HTML: ${outputPath}` } ] }; }
  • Input schema defining parameters for generate_pdf_from_html: html_content (required), output_filename (required), output_dir (optional), options (PDF format, margins).
    inputSchema: { type: "object", properties: { html_content: { type: "string", description: "HTML content to convert to PDF" }, output_filename: { type: "string", description: "Name of the output PDF file (without path)" }, output_dir: { type: "string", description: "Output directory (optional, defaults to Downloads)", default: DEFAULT_OUTPUT_DIR }, options: { type: "object", description: "PDF generation options", properties: { format: { type: "string", default: "A4" }, margin: { type: "object", properties: { top: { type: "string", default: "1cm" }, right: { type: "string", default: "1cm" }, bottom: { type: "string", default: "1cm" }, left: { type: "string", default: "1cm" } } } } } }, required: ["html_content", "output_filename"] }
  • index.ts:56-94 (registration)
    Registration of the 'generate_pdf_from_html' tool in the tools array, including name, description, and input schema. Used by the ListTools handler.
    { name: "generate_pdf_from_html", description: "Generate a PDF from HTML content using Puppeteer", inputSchema: { type: "object", properties: { html_content: { type: "string", description: "HTML content to convert to PDF" }, output_filename: { type: "string", description: "Name of the output PDF file (without path)" }, output_dir: { type: "string", description: "Output directory (optional, defaults to Downloads)", default: DEFAULT_OUTPUT_DIR }, options: { type: "object", description: "PDF generation options", properties: { format: { type: "string", default: "A4" }, margin: { type: "object", properties: { top: { type: "string", default: "1cm" }, right: { type: "string", default: "1cm" }, bottom: { type: "string", default: "1cm" }, left: { type: "string", default: "1cm" } } } } } }, required: ["html_content", "output_filename"] } },

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/Theorhd/Pdftools-mcp'

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