Skip to main content
Glama

pdf

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
NameRequiredDescriptionDefault
formatNoA4
landscapeNo
printBackgroundNo
marginNo
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • 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))); }
  • 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))); } } );
  • 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);
  • TypeScript interface for the PDF tool result.
    /** * PDF result */ export interface PdfResult { /** Base64-encoded PDF data */ data: string; }

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/andytango/puppeteer-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server