Generate PDF documents from web pages with configurable format, orientation, margins, and background printing options.
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:89-119 (handler)Full handler function for the 'pdf' tool. Generates a PDF of the page using Puppeteer page.pdf(), encodes it to base64, and returns it as PdfResult.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 input parameters for the pdf tool, including format, landscape, 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/types.ts:81-84 (schema)TypeScript interface for the output of the pdf tool.export interface PdfResult { /** Base64-encoded PDF data */ data: string; }
- src/types.ts:167-168 (schema)Type definition for PDF paper formats used in the pdf tool.export type PdfFormat = 'Letter' | 'Legal' | 'Tabloid' | 'Ledger' | 'A0' | 'A1' | 'A2' | 'A3' | 'A4' | 'A5' | 'A6';