Nestjs Reportes Genera Pdfs Desde Node Full -mega- [ Exclusive | 2025 ]

// pdf.module.ts import Module from '@nestjs/common'; import PdfService from './pdf.service'; import PdfController from './pdf.controller'; @Module( providers: [PdfService], controllers: [PdfController], exports: [PdfService], ) export class PdfModule {} // pdf.service.ts import Injectable, Logger from '@nestjs/common'; import * as puppeteer from 'puppeteer'; import * as handlebars from 'handlebars'; import * as fs from 'fs/promises'; import join from 'path'; @Injectable() export class PdfService private readonly logger = new Logger(PdfService.name); private browser: puppeteer.Browser;

return this.instances.pop()!;

(in main.ts or before use):

FROM ghcr.io/puppeteer/puppeteer:20.0.0 WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . NestJs Reportes Genera PDFs desde Node Full -Mega-

res.setHeader('Content-Type', 'application/pdf'); res.setHeader('Content-Disposition', 'attachment; filename=large-report.pdf');

// 2. Compile with Handlebars const template = handlebars.compile(htmlTemplate); const html = template(data);

npm install @nestjs/core @nestjs/common puppeteer handlebars @types/puppeteer Create a dedicated module: // pdf

// 3. Generate PDF const page = await this.browser.newPage(); await page.setContent(html, waitUntil: 'networkidle0' );

async onModuleInit() // Launch browser once (reuse across requests) this.browser = await puppeteer.launch( headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'], );

await page.close(); return Buffer.from(pdf); catch (error) this.logger.error(`PDF generation failed: $error.message`); throw new Error('Could not generate PDF'); Compile with Handlebars const template = handlebars

} For reports >50MB, stream directly to response:

// Option 2: Inline preview @Post('preview') async previewReport(@Body() data: any, @Res() res: Response) const pdfBuffer = await this.pdfService.generateReport('dashboard', data); res.set( 'Content-Type': 'application/pdf' ); res.send(pdfBuffer); // Opens in browser

<!DOCTYPE html> <html> <head> <style> body font-family: 'Helvetica', sans-serif; .header background: #2c3e50; color: white; padding: 20px; .invoice-title font-size: 28px; table width: 100%; border-collapse: collapse; margin: 20px 0; th, td border: 1px solid #ddd; padding: 10px; text-align: left; th background: #f2f2f2; .total font-size: 20px; font-weight: bold; text-align: right; </style> </head> <body> <div class="header"> <div class="invoice-title">INVOICE #invoiceNumber</div> <div>Date: date</div> </div> <h3>Bill To:</h3> <p>customer.name<br>customer.address</p>

Now go generate those reports! 📄🚀

| Library | Use Case | Install | |---------|----------|---------| | puppeteer | Complex HTML/CSS/JS rendering (Chrome) | npm install puppeteer | | @react-pdf/renderer | React-style declarative PDFs | npm install @react-pdf/renderer | | pdfmake | Simple tables & text | npm install pdfmake | | wkhtmltopdf | Legacy HTML→PDF | (Requires OS binary) |