generate_pdf_from_html
Convert HTML content to PDF files with customizable formatting options like page size and margins. Specify output filename and directory for automatic saving.
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 | |
| output_filename | Yes | Name of the output PDF file (without path) | |
| output_dir | No | Output directory (optional, defaults to Downloads) | /root/Downloads |
| options | No | PDF generation options |
Implementation Reference
- index.ts:184-219 (handler)The handler function for the 'generate_pdf_from_html' tool. It validates the output path, launches a headless Puppeteer browser, loads the HTML content into a page, generates the PDF with specified options, and returns a success message with the file path.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 definition for the 'generate_pdf_from_html' tool, specifying parameters like html_content (required), output_filename (required), output_dir (optional), and options for PDF formatting.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)Tool registration in the tools array, including name, description, and input schema. This is returned by ListToolsRequestHandler.{ 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"] } },
- index.ts:30-41 (helper)Helper function to validate that the output path is within allowed user directories (Downloads, Documents, Desktop). Used in the handler.function validateOutputPath(outputPath: string): string { const resolvedPath = path.resolve(outputPath); const isAllowed = ALLOWED_DIRS.some(allowedDir => resolvedPath.startsWith(path.resolve(allowedDir)) ); if (!isAllowed) { throw new Error(`Output path must be within allowed directories: ${ALLOWED_DIRS.join(", ")}`); } return resolvedPath; }