Skip to main content
Glama

generate_pdf

Convert web pages or HTML content into PDFs using the Browserless MCP Server. Customize output with headers, footers, background printing, layout, and margins for precise document formatting.

Instructions

Generate PDF from URL or HTML content

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
htmlNo
optionsNo
urlYes

Implementation Reference

  • MCP server handler for 'generate_pdf' tool: calls BrowserlessClient.generatePdf and formats response as MCP binary content with PDF data.
    case 'generate_pdf': { const result = await this.client!.generatePdf(args as any); if (result.success && result.data) { return { content: [ { type: 'text', text: `PDF generated successfully. Filename: ${result.data.filename}`, }, { type: 'binary', mimeType: 'application/pdf', data: result.data.pdf.toString('base64'), }, ], }; } else { throw new Error(result.error || 'Failed to generate PDF'); } }
  • src/index.ts:52-80 (registration)
    Registration of 'generate_pdf' tool in MCP server's tool list, including input schema definition.
    name: 'generate_pdf', description: 'Generate PDF from URL or HTML content', inputSchema: { type: 'object', properties: { url: { type: 'string' }, html: { type: 'string' }, options: { type: 'object', properties: { displayHeaderFooter: { type: 'boolean' }, printBackground: { type: 'boolean' }, format: { type: 'string' }, landscape: { type: 'boolean' }, margin: { type: 'object', properties: { top: { type: 'string' }, bottom: { type: 'string' }, left: { type: 'string' }, right: { type: 'string' }, }, }, }, }, }, required: ['url'], }, },
  • BrowserlessClient.generatePdf method: proxies PDF generation request to Browserless /pdf endpoint, returns Buffer-wrapped response.
    async generatePdf(request: PdfRequest): Promise<BrowserlessResponse<PdfResponse>> { try { const response: AxiosResponse<Buffer> = await this.httpClient.post('/pdf', request, { responseType: 'arraybuffer', headers: { 'Content-Type': 'application/json', }, }); return { success: true, data: { pdf: Buffer.from(response.data), filename: `document-${Date.now()}.pdf`, }, }; } catch (error) { return this.handleError(error); } }
  • Zod schema defining PdfRequest type for input validation in client.generatePdf.
    export const PdfRequestSchema = z.object({ url: z.string().optional(), html: z.string().optional(), options: PdfOptionsSchema.optional(), addScriptTag: z.array(ScriptTagSchema).optional(), addStyleTag: z.array(StyleTagSchema).optional(), cookies: z.array(CookieSchema).optional(), headers: z.record(z.string()).optional(), viewport: ViewportSchema.optional(), waitForEvent: WaitForEventSchema.optional(), waitForFunction: WaitForFunctionSchema.optional(), waitForSelector: WaitForSelectorSchema.optional(), waitForTimeout: z.number().optional(), }); export type PdfRequest = z.infer<typeof PdfRequestSchema>;
  • Type definition for PdfResponse returned by generatePdf.
    export interface PdfResponse { pdf: Buffer; filename: 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/Lizzard-Solutions/browserless-mcp'

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