res.set( 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename=invoice-$data.invoiceNumber.pdf`, 'Content-Length': pdfBuffer.length, ); res.send(pdfBuffer);
stream.pipe(res);
// 3. Generate PDF const page = await this.browser.newPage(); await page.setContent(html, waitUntil: 'networkidle0' ); NestJs Reportes Genera PDFs desde Node Full -Mega-
// 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 res.set( 'Content-Type': 'application/pdf'
async generateWithTimeout(data: any, timeoutMs = 30000) return Promise.race([ this.generateReport(data), new Promise((_, reject) => setTimeout(() => reject(new Error('PDF generation timeout')), timeoutMs) ), ]); waitUntil: 'networkidle0' )
npm install @nestjs/core @nestjs/common puppeteer handlebars @types/puppeteer Create a dedicated module:
static async acquire(): Promise<Browser> if (this.instances.length < this.max) const browser = await puppeteer.launch( headless: true ); this.instances.push(browser);