pdf_create_from_template
Generate PDF documents from predefined templates for invoices, reports, or letters by providing structured data matching template fields.
Instructions
Create a PDF from a named template (invoice, report, or letter). Pass structured data matching the template's expected fields.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| templateName | Yes | Template to use: invoice, report, or letter | |
| data | Yes | Template data. Invoice: companyName, clientName, invoiceNumber, items[{description, quantity, unitPrice}], taxRate, dueDate, notes, paymentTerms. Report: title, author, date, subtitle, sections[{heading, body}]. Letter: senderName, senderAddress, recipientName, recipientAddress, subject, body, closing, signatureName. | |
| outputPath | Yes | Absolute path for the output PDF file | |
| pageSize | No | Page size. Defaults to A4. |
Implementation Reference
- src/tools/create.ts:729-764 (handler)The handler function for pdf_create_from_template, which validates inputs, resolves templates, and renders the PDF.
async ({ templateName, data, outputPath, pageSize }) => { try { const BLOCKED_KEYS = ['__proto__', 'constructor', 'prototype']; if (Object.keys(data).some(k => BLOCKED_KEYS.includes(k))) { return toolError('Input contains prohibited key names.'); } const resolvedOutput = await validateOutputPath(outputPath); const builder = templateRegistry[templateName as TemplateName]; if (!builder) { return toolError( `Unknown template: ${templateName}. Available templates: invoice, report, letter.` ); } const docDefinition = builder(data as Record<string, unknown>) as unknown as PdfDocDefinition; if (pageSize) { docDefinition.pageSize = pageSize; } const result = await renderDocDefinition(docDefinition, resolvedOutput); return toolSuccess({ outputPath: result.outputPath, template: templateName, pageCount: result.pageCount, pageSize: pageSize ?? "A4", fileSize: result.fileSize, }); } catch (error) { return toolError( error instanceof Error ? error.message : String(error) ); } } - src/tools/create.ts:697-765 (registration)Registration of the pdf_create_from_template tool with its description and input schema.
server.registerTool( "pdf_create_from_template", { description: "Create a PDF from a named template (invoice, report, or letter). Pass structured data matching the template's expected fields.", inputSchema: z .object({ templateName: z .enum(["invoice", "report", "letter"]) .describe("Template to use: invoice, report, or letter"), data: z .record(z.unknown()) .describe( "Template data. Invoice: companyName, clientName, invoiceNumber, items[{description, quantity, unitPrice}], taxRate, dueDate, notes, paymentTerms. Report: title, author, date, subtitle, sections[{heading, body}]. Letter: senderName, senderAddress, recipientName, recipientAddress, subject, body, closing, signatureName." ), outputPath: z .string() .max(4096) .describe("Absolute path for the output PDF file"), pageSize: z .enum(["A4", "LETTER", "LEGAL"]) .optional() .describe("Page size. Defaults to A4."), }) .strict(), annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async ({ templateName, data, outputPath, pageSize }) => { try { const BLOCKED_KEYS = ['__proto__', 'constructor', 'prototype']; if (Object.keys(data).some(k => BLOCKED_KEYS.includes(k))) { return toolError('Input contains prohibited key names.'); } const resolvedOutput = await validateOutputPath(outputPath); const builder = templateRegistry[templateName as TemplateName]; if (!builder) { return toolError( `Unknown template: ${templateName}. Available templates: invoice, report, letter.` ); } const docDefinition = builder(data as Record<string, unknown>) as unknown as PdfDocDefinition; if (pageSize) { docDefinition.pageSize = pageSize; } const result = await renderDocDefinition(docDefinition, resolvedOutput); return toolSuccess({ outputPath: result.outputPath, template: templateName, pageCount: result.pageCount, pageSize: pageSize ?? "A4", fileSize: result.fileSize, }); } catch (error) { return toolError( error instanceof Error ? error.message : String(error) ); } } ); - src/tools/create.ts:702-721 (schema)Input schema validation for the pdf_create_from_template tool using Zod.
inputSchema: z .object({ templateName: z .enum(["invoice", "report", "letter"]) .describe("Template to use: invoice, report, or letter"), data: z .record(z.unknown()) .describe( "Template data. Invoice: companyName, clientName, invoiceNumber, items[{description, quantity, unitPrice}], taxRate, dueDate, notes, paymentTerms. Report: title, author, date, subtitle, sections[{heading, body}]. Letter: senderName, senderAddress, recipientName, recipientAddress, subject, body, closing, signatureName." ), outputPath: z .string() .max(4096) .describe("Absolute path for the output PDF file"), pageSize: z .enum(["A4", "LETTER", "LEGAL"]) .optional() .describe("Page size. Defaults to A4."), }) .strict(),