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;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions 'using Puppeteer' which hints at browser-based rendering, but doesn't disclose critical behaviors like whether this is a blocking/long-running operation, error handling, file system impacts, or authentication needs. For a tool that generates files with 4 parameters, this is insufficient.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's appropriately sized for a straightforward conversion tool and front-loads the core functionality. Every word earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 4 parameters, nested objects, no annotations, and no output schema, the description is incomplete. It doesn't explain what happens after generation (where the file goes, return values), error conditions, performance characteristics, or how it differs from sibling tools. The 100% schema coverage helps but doesn't compensate for missing behavioral context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description adds no parameter-specific information beyond what's in the schema. It mentions 'HTML content' and 'PDF' which aligns with parameters but doesn't provide additional context about format requirements, constraints, or usage examples.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'Generate' and resource 'PDF from HTML content', specifying the conversion purpose. It distinguishes from siblings by mentioning 'HTML content' (vs. markdown/text for other tools) and 'using Puppeteer' as the implementation method. However, it doesn't explicitly contrast with sibling tools like 'read_pdf'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'generate_pdf_from_markdown' or 'generate_pdf_from_text'. It doesn't mention prerequisites, constraints, or typical use cases. The only implied usage is converting HTML to PDF, but no context for choosing between sibling tools is given.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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