Skip to main content
Glama

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
NameRequiredDescriptionDefault
templateNameYesTemplate to use: invoice, report, or letter
dataYesTemplate 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.
outputPathYesAbsolute path for the output PDF file
pageSizeNoPage size. Defaults to A4.

Implementation Reference

  • 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)
        );
      }
    }
  • 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)
          );
        }
      }
    );
  • 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(),

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/AryanBV/pdf-toolkit-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server