pdf_create
Convert text content into PDF files with automatic formatting for A4, Letter, or Legal page sizes. Add document metadata and control layout settings to create professional PDFs.
Instructions
Create a new PDF from text content with automatic line wrapping and page overflow. Supports A4, Letter, and Legal page sizes.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| outputPath | Yes | Absolute path for the output PDF file | |
| content | Yes | Text content for the PDF. Use \n for line breaks. | |
| title | No | PDF document title metadata | |
| author | No | PDF document author metadata | |
| pageSize | No | Page size. Defaults to A4. | |
| fontSize | No | Font size in points (6–72). Defaults to 12. | |
| margin | No | Page margin in points (0–500). Defaults to 50. |
Implementation Reference
- src/tools/create.ts:96-206 (handler)Implementation and registration of the 'pdf_create' tool, including its input schema definition and the handler function that processes the PDF creation logic using @pdfme/pdf-lib.
server.registerTool( "pdf_create", { description: "Create a new PDF from text content with automatic line wrapping and page overflow. Supports A4, Letter, and Legal page sizes.", inputSchema: z .object({ outputPath: z .string() .max(4096) .describe("Absolute path for the output PDF file"), content: z .string() .min(1) .max(10_000_000) .describe( "Text content for the PDF. Use \\n for line breaks." ), title: z.string().max(1000).optional().describe("PDF document title metadata"), author: z .string() .max(1000) .optional() .describe("PDF document author metadata"), pageSize: z .enum(["A4", "Letter", "Legal"]) .optional() .describe("Page size. Defaults to A4."), fontSize: z .number() .min(6) .max(72) .optional() .describe("Font size in points (6–72). Defaults to 12."), margin: z .number() .min(0) .max(500) .optional() .describe("Page margin in points (0–500). Defaults to 50."), }) .strict(), annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async ({ outputPath, content, title, author, pageSize, fontSize, margin }) => { try { const resolvedOutput = await validateOutputPath(outputPath); const size = pageSize ?? "A4"; const fSize = fontSize ?? 12; const marg = margin ?? 50; const [pageWidth, pageHeight] = pdfLib.PageSizes[size as keyof typeof pdfLib.PageSizes]; const lineHeight = fSize * 1.2; const pdfDoc = await createNewPdf(); if (title) pdfDoc.setTitle(title); if (author) pdfDoc.setAuthor(author); const font = await pdfDoc.embedFont(pdfLib.StandardFonts.Helvetica); const maxWidth = pageWidth - 2 * marg; // Parse escape sequences (LLMs send literal \n and \t) let textContent = content.replace(/\\n/g, "\n"); textContent = textContent.replace(/\\t/g, "\t"); const wrappedLines = wrapText(textContent, font, fSize, maxWidth); let page = pdfDoc.addPage([pageWidth, pageHeight]); let currentY = pageHeight - marg; for (const line of wrappedLines) { if (currentY - fSize < marg) { page = pdfDoc.addPage([pageWidth, pageHeight]); currentY = pageHeight - marg; } if (line !== "") { page.drawText(line, { x: marg, y: currentY, size: fSize, font, }); } currentY -= lineHeight; } await savePdf(pdfDoc, resolvedOutput); const fileSize = await getFileSize(resolvedOutput); return toolSuccess({ outputPath: resolvedOutput, pageCount: pdfDoc.getPageCount(), title: title ?? null, pageSize: size, fileSize, }); } catch (error) { return toolError( error instanceof Error ? error.message : String(error) ); } } );