generate_pdf
Convert webpages to PDF format by providing a URL. Returns base64-encoded PDF data for immediate use or download.
Instructions
[STATELESS] Convert webpage to PDF. Returns base64-encoded PDF data. Creates new browser each time. Cannot capture form fills or JS changes. For persistent PDFs use create_session + crawl(session_id, pdf:true).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | The URL to convert to PDF |
Implementation Reference
- src/handlers/content-handlers.ts:132-159 (handler)Main handler function that executes the generate_pdf tool: calls the service to generate PDF and formats the base64 response as an MCP resource with URI.async generatePDF(options: PDFEndpointOptions) { try { const result: PDFEndpointResponse = await this.service.generatePDF(options); // Response has { success: true, pdf: "base64string" } if (!result.success || !result.pdf) { throw new Error('PDF generation failed - no PDF data in response'); } return { content: [ { type: 'resource', resource: { uri: `data:application/pdf;name=${encodeURIComponent(new URL(String(options.url)).hostname)}.pdf;base64,${result.pdf}`, mimeType: 'application/pdf', blob: result.pdf, }, }, { type: 'text', text: `PDF generated for: ${options.url}`, }, ], }; } catch (error) { throw this.formatError(error, 'generate PDF'); }
- Zod schema defining the input validation for generate_pdf tool (requires url).export const GeneratePdfSchema = createStatelessSchema( z.object({ url: z.string().url(), // Only url is supported - output_path not exposed as MCP needs base64 data }), 'generate_pdf', );
- src/server.ts:837-840 (registration)Tool dispatch/registration in server request handler: switch case that validates args with GeneratePdfSchema and calls contentHandlers.generatePDF.case 'generate_pdf': return await this.validateAndExecute('generate_pdf', args, GeneratePdfSchema, async (validatedArgs) => this.contentHandlers.generatePDF(validatedArgs), );
- src/server.ts:176-188 (registration)Tool specification advertised in listTools response, including name, description, and inputSchema for generate_pdf.name: 'generate_pdf', description: '[STATELESS] Convert webpage to PDF. Returns base64-encoded PDF data. Creates new browser each time. Cannot capture form fills or JS changes. For persistent PDFs use create_session + crawl(session_id, pdf:true).', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to convert to PDF', }, }, required: ['url'], },
- src/crawl4ai-service.ts:159-175 (helper)Service layer method that makes HTTP POST to backend /pdf endpoint with URL, returning base64 PDF data. Called by the handler.async generatePDF(options: PDFEndpointOptions): Promise<PDFEndpointResponse> { // Validate URL if (!validateURL(options.url)) { throw new Error('Invalid URL format'); } try { const response = await this.axiosClient.post('/pdf', { url: options.url, // output_path is omitted to get base64 response }); return response.data; } catch (error) { return handleAxiosError(error); } }