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
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | ||
| html | No | ||
| options | No |
Implementation Reference
- src/client.ts:61-80 (handler)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); } }
- src/types.ts:113-128 (schema)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'], },
- src/index.ts:302-321 (handler)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'); } }
- src/types.ts:254-257 (schema)TypeScript interface for the PDF response containing the buffer and filename.export interface PdfResponse { pdf: Buffer; filename: string; }