Skip to main content
Glama

generate_pdf

Convert web pages or HTML content into PDF documents using browser automation. Specify URL or HTML with options for headers, formatting, and margins.

Instructions

Generate PDF from URL or HTML content

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYes
htmlNo
optionsNo

Implementation Reference

  • Core handler function that executes the PDF generation logic by sending an HTTP POST request to the Browserless /pdf endpoint and returning the PDF buffer.
    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 definition for PdfRequest input validation, including options for PDF generation.
    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>;
  • src/index.ts:51-79 (registration)
    MCP tool registration for 'generate_pdf', including description and input schema.
    { 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'], },
  • Server-side MCP CallTool handler for generate_pdf, which delegates to client.generatePdf and formats the response as MCP content.
    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'); } }
  • TypeScript interface for the PDF response containing the buffer and filename.
    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