Skip to main content
Glama
Theorhd
by Theorhd

generate_pdf_from_text

Convert plain text content into PDF documents with customizable formatting options like fonts, margins, and file naming. Automatically saves to specified directories for organized document creation.

Instructions

Generate a PDF from plain text using PDFKit

Input Schema

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

Implementation Reference

  • The handler logic for executing the 'generate_pdf_from_text' tool. It uses PDFKit to generate a PDF from text content, applies formatting options, validates and saves to the output path, and returns a success message.
    case "generate_pdf_from_text": {
      const { text_content, output_filename, output_dir = DEFAULT_OUTPUT_DIR, options = {} } = args as any;
      
      const outputPath = validateOutputPath(path.join(output_dir, output_filename));
      
      await fs.mkdir(path.dirname(outputPath), { recursive: true });
      
      const doc = new PDFDocument({
        margins: options.margins || { top: 50, left: 50, right: 50, bottom: 50 }
      });
      
      const stream = createWriteStream(outputPath);
      doc.pipe(stream);
      
      doc.font(options.font || 'Helvetica')
         .fontSize(options.fontSize || 12);
      
      doc.text(text_content);
      
      doc.end();
      
      return new Promise((resolve) => {
        stream.on('finish', () => {
          resolve({
            content: [
              {
                type: "text",
                text: `PDF successfully generated from text: ${outputPath}`
              }
            ]
          });
        });
      });
    }
  • The tool definition including name, description, and detailed inputSchema with properties, defaults, and required fields for the generate_pdf_from_text tool.
    {
      name: "generate_pdf_from_text",
      description: "Generate a PDF from plain text using PDFKit",
      inputSchema: {
        type: "object",
        properties: {
          text_content: {
            type: "string",
            description: "Text 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
          },
          options: {
            type: "object",
            description: "PDF formatting options",
            properties: {
              fontSize: { type: "number", default: 12 },
              font: { type: "string", default: "Helvetica" },
              margins: {
                type: "object",
                properties: {
                  top: { type: "number", default: 50 },
                  left: { type: "number", default: 50 },
                  right: { type: "number", default: 50 },
                  bottom: { type: "number", default: 50 }
                }
              }
            }
          }
        },
        required: ["text_content", "output_filename"]
      }
    },
  • index.ts:175-177 (registration)
    Registration of the ListToolsRequestSchema handler, which returns the list of available tools including 'generate_pdf_from_text'.
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return { tools };
    });
  • Helper function used by the tool handler to validate that the output path is within allowed user directories for security.
    function validateOutputPath(outputPath: string): string {
      const resolvedPath = path.resolve(outputPath);
      const isAllowed = ALLOWED_DIRS.some(allowedDir => 
        resolvedPath.startsWith(path.resolve(allowedDir))
      );
      
      if (!isAllowed) {
        throw new Error(`Output path must be within allowed directories: ${ALLOWED_DIRS.join(", ")}`);
      }
      
      return resolvedPath;
    }

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