Generate PDF files from web pages with customizable formatting options including page size, orientation, and margins.
Instructions
Generate a PDF of the current page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | A4 | |
| landscape | No | ||
| printBackground | No | ||
| margin | No | ||
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tools/media.ts:93-117 (handler)Handler function that executes the PDF generation using Puppeteer's page.pdf() method, converts to base64, and returns the result.async ({ format, landscape, printBackground, margin, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { const pdfData = await page.pdf({ format: (format ?? 'A4') as PdfFormat, landscape: landscape ?? false, printBackground: printBackground ?? true, margin: margin ?? undefined, }); const base64Data = Buffer.from(pdfData).toString('base64'); return handleResult(ok({ data: base64Data, size: pdfData.length, })); } catch (error) { return handleResult(err(normalizeError(error))); }
- src/tools/media.ts:89-119 (registration)Registration of the 'pdf' tool on the MCP server with name, description, input schema, and handler function.server.tool( 'pdf', 'Generate a PDF of the current page', pdfSchema.shape, async ({ format, landscape, printBackground, margin, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { const pdfData = await page.pdf({ format: (format ?? 'A4') as PdfFormat, landscape: landscape ?? false, printBackground: printBackground ?? true, margin: margin ?? undefined, }); const base64Data = Buffer.from(pdfData).toString('base64'); return handleResult(ok({ data: base64Data, size: pdfData.length, })); } catch (error) { return handleResult(err(normalizeError(error))); } } );
- src/schemas.ts:158-169 (schema)Zod schema defining the input parameters for the pdf tool, including format, margins, etc.export const pdfSchema = z.object({ format: z.enum(['Letter', 'Legal', 'Tabloid', 'Ledger', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6']).optional().default('A4'), landscape: z.boolean().optional().default(false), printBackground: z.boolean().optional().default(true), margin: z.object({ top: z.string().optional(), right: z.string().optional(), bottom: z.string().optional(), left: z.string().optional(), }).optional(), tabId: tabIdSchema, });
- src/server.ts:27-27 (registration)Invocation of registerMediaTools which registers the pdf tool among others.registerMediaTools(server);
- src/types.ts:78-84 (schema)TypeScript interface for the PDF tool result./** * PDF result */ export interface PdfResult { /** Base64-encoded PDF data */ data: string; }