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
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | PDF formatting options | |
| output_dir | No | Output directory (optional, defaults to Downloads) | /root/Downloads |
| output_filename | Yes | Name of the output PDF file (without path) | |
| text_content | Yes | Text content to convert to PDF |
Implementation Reference
- index.ts:256-289 (handler)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}` } ] }); }); }); }
- index.ts:95-134 (schema)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 }; });
- index.ts:30-41 (helper)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; }