generate_pdf_from_markdown
Convert Markdown content to PDF format with customizable output location and filename for document creation and sharing.
Instructions
Generate a PDF from Markdown content
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| markdown_content | Yes | Markdown content to convert to PDF | |
| output_dir | No | Output directory (optional, defaults to Downloads) | /root/Downloads |
| output_filename | Yes | Name of the output PDF file (without path) |
Implementation Reference
- index.ts:291-322 (handler)Executes the generate_pdf_from_markdown tool: validates output path, converts Markdown to HTML, uses Puppeteer to generate PDF.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}` } ] }; }
- index.ts:135-157 (registration)Registers the generate_pdf_from_markdown tool with name, description, and input schema in the tools array.{ 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"] } }
- index.ts:138-155 (schema)Input schema defining parameters for markdown_content, output_filename, and 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:340-366 (helper)Helper function that converts Markdown to HTML using regex for PDF generation via Puppeteer.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> `; }