Nestjs Reportes Genera Pdfs Desde Node Full - -mega-
return this.instances.pop()!;
<table> <thead> <tr><th>Item</th><th>Qty</th><th>Price</th><th>Total</th></tr> </thead> <tbody> #each items <tr> <td>this.name</td> <td>this.qty</td> <td>$this.price</td> <td>$multiply this.qty this.price</td> </tr> /each </tbody> </table>
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-
✅ Reuse browser instance across requests ✅ Stream large PDFs instead of buffering ✅ Set proper timeouts for slow renders ✅ Use Docker with pre-installed Chrome
static async acquire(): Promise<Browser> if (this.instances.length < this.max) const browser = await puppeteer.launch( headless: true ); this.instances.push(browser); return this
async generateWithTimeout(data: any, timeoutMs = 30000) return Promise.race([ this.generateReport(data), new Promise((_, reject) => setTimeout(() => reject(new Error('PDF generation timeout')), timeoutMs) ), ]);
static release(browser: Browser) this.instances.push(browser); #each items <
<!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>
res.set( 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename=invoice-$data.invoiceNumber.pdf`, 'Content-Length': pdfBuffer.length, ); res.send(pdfBuffer);
handlebars.registerHelper('multiply', (a, b) => a * b); // pdf.controller.ts import Controller, Post, Body, Res, Get, Query from '@nestjs/common'; import Response from 'express'; import PdfService from './pdf.service'; @Controller('reports') export class PdfController { constructor(private readonly pdfService: PdfService) {}