Skip to main content
Glama
andytango
by andytango

pdf

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

Implementation Reference

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

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