Skip to main content
Glama
Theorhd
by Theorhd

generate_pdf_from_markdown

Convert Markdown content to PDF files with customizable output names and directories for organized document creation.

Instructions

Generate a PDF from Markdown content

Input Schema

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

Implementation Reference

  • Main handler logic for the 'generate_pdf_from_markdown' tool. Converts Markdown to HTML using the markdownToHtml helper, validates the output path, uses Puppeteer to render HTML to PDF, and saves it to the specified location.
    case "generate_pdf_from_markdown": {
      const { markdown_content, output_filename, output_dir = DEFAULT_OUTPUT_DIR } = args as any;
      
      const outputPath = validateOutputPath(path.join(output_dir, output_filename));
      
      const htmlContent = markdownToHtml(markdown_content);
      
      await fs.mkdir(path.dirname(outputPath), { recursive: true });
      
      const browser = await puppeteer.launch({ headless: true });
      const page = await browser.newPage();
      
      await page.setContent(htmlContent, { waitUntil: 'networkidle0' });
      
      await page.pdf({
        path: outputPath,
        format: 'A4',
        margin: { top: '1cm', right: '1cm', bottom: '1cm', left: '1cm' },
        printBackground: true
      });
      
      await browser.close();
      
      return {
        content: [
          {
            type: "text", 
            text: `PDF successfully generated from Markdown: ${outputPath}`
          }
        ]
      };
    }
  • Input schema for the tool, defining required markdown_content and output_filename, optional output_dir.
    inputSchema: {
      type: "object", 
      properties: {
        markdown_content: {
          type: "string",
          description: "Markdown 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
        }
      },
      required: ["markdown_content", "output_filename"]
    }
  • index.ts:135-157 (registration)
    Tool registration in the tools list, including name, description, and input schema. Used by ListToolsRequestHandler.
    {
      name: "generate_pdf_from_markdown",
      description: "Generate a PDF from Markdown content",
      inputSchema: {
        type: "object", 
        properties: {
          markdown_content: {
            type: "string",
            description: "Markdown 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
          }
        },
        required: ["markdown_content", "output_filename"]
      }
    }
  • Helper function to convert Markdown to HTML with basic support for headers, bold, italic, code, and line breaks. Wraps in a styled HTML document for PDF generation.
    function markdownToHtml(markdown: string): string {
      let html = markdown
        .replace(/^### (.*$)/gim, '<h3>$1</h3>')
        .replace(/^## (.*$)/gim, '<h2>$1</h2>')
        .replace(/^# (.*$)/gim, '<h1>$1</h1>')
        .replace(/\*\*(.*?)\*\*/gim, '<strong>$1</strong>')
        .replace(/\*(.*?)\*/gim, '<em>$1</em>')
        .replace(/`(.*?)`/gim, '<code>$1</code>')
        .replace(/\n/gim, '<br>');
        
      return `
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="utf-8">
          <style>
            body { font-family: Arial, sans-serif; line-height: 1.6; margin: 40px; }
            h1, h2, h3 { color: #333; }
            code { background-color: #f4f4f4; padding: 2px 4px; border-radius: 3px; }
          </style>
        </head>
        <body>
          ${html}
        </body>
        </html>
      `;
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool generates a PDF but doesn't describe what happens during generation (e.g., file creation, overwriting behavior, error handling), permissions needed, or rate limits. For a tool that creates files, this is a significant gap in transparency.

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 states the core purpose without unnecessary words. It's appropriately sized and front-loaded, with every word earning its place. No structural issues or redundancy are present.

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?

Given the tool's complexity (file generation with 3 parameters), lack of annotations, and no output schema, the description is incomplete. It doesn't explain what the tool returns (e.g., success/failure, file path), error conditions, or behavioral details. For a tool that creates output files, this leaves significant gaps for an agent.

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 fully documents all three parameters. The description adds no parameter-specific information beyond what's in the schema. This meets the baseline of 3 when the schema does the heavy lifting, but the description doesn't compensate with additional context about parameter interactions or constraints.

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 tool's purpose: 'Generate a PDF from Markdown content' - a specific verb (generate) and resource (PDF from Markdown). It distinguishes from sibling tools like generate_pdf_from_html and generate_pdf_from_text by specifying the input format. However, it doesn't explicitly contrast with read_pdf, which serves a different purpose.

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. It doesn't mention when to choose markdown over HTML or text for PDF generation, nor does it address prerequisites or exclusions. The agent must infer usage from the tool name alone.

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