Skip to main content
Glama
Theorhd
by Theorhd

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
NameRequiredDescriptionDefault
html_contentYesHTML content to convert to PDF
output_filenameYesName of the output PDF file (without path)
output_dirNoOutput directory (optional, defaults to Downloads)/root/Downloads
optionsNoPDF generation options

Implementation Reference

  • 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}`
          }
        ]
      };
    }
  • 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"]
      }
    },
  • 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;
    }

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