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
| Name | Required | Description | Default |
|---|---|---|---|
| html_content | Yes | HTML content to convert to PDF | |
| options | No | PDF generation options | |
| output_dir | No | Output directory (optional, defaults to Downloads) | /root/Downloads |
| output_filename | Yes | Name of the output PDF file (without path) |
Implementation Reference
- index.ts:184-219 (handler)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}` } ] }; }
- index.ts:59-93 (schema)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"] } },