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
| Name | Required | Description | Default |
|---|---|---|---|
| html | No | ||
| options | No | ||
| url | Yes |
Implementation Reference
- src/index.ts:302-321 (handler)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'], }, },
- src/client.ts:61-80 (helper)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); } }
- src/types.ts:113-129 (schema)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>;
- src/types.ts:254-257 (schema)Type definition for PdfResponse returned by generatePdf.export interface PdfResponse { pdf: Buffer; filename: string; }