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>
      `;
    }

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