Skip to main content
Glama

generate_pdf_from_text

Convert plain text content into PDF documents with customizable formatting options including fonts, margins, and file naming.

Instructions

Generate a PDF from plain text using PDFKit

Input Schema

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

Implementation Reference

  • Handler implementation for the generate_pdf_from_text tool. Parses input arguments, validates and creates output directory, generates PDF using PDFKit by creating a document, setting fonts and margins, adding text, and streaming to file. Returns 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}` } ] }); }); }); }
  • Schema definition for generate_pdf_from_text tool, including input schema with required text_content and output_filename, optional output_dir and formatting options.
    { 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 ListToolsRequestSchema handler that returns the tools array, making generate_pdf_from_text discoverable.
    server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools }; });
  • Helper function validateOutputPath ensures the output path for generated PDFs is restricted to user-allowed directories (Downloads, Documents, Desktop) 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