Nestjs Reportes Genera Pdfs Desde Node Full -mega- ★ Fast & Free
// 3. Generate PDF const page = await this.browser.newPage(); await page.setContent(html, waitUntil: 'networkidle0' );
// 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;
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) {} NestJs Reportes Genera PDFs desde Node Full -Mega-
stream.pipe(res);
FROM ghcr.io/puppeteer/puppeteer:20.0.0 WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . res.set( 'Content-Type': 'application/pdf'
res.set( 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename=invoice-$data.invoiceNumber.pdf`, 'Content-Length': pdfBuffer.length, ); res.send(pdfBuffer);
static async acquire(): Promise<Browser> if (this.instances.length < this.max) const browser = await puppeteer.launch( headless: true ); this.instances.push(browser); static async acquire(): Promise<
} For reports >50MB, stream directly to response:
// Option 1: Download as file @Post('invoice') async generateInvoice(@Body() data: any, @Res() res: Response) const pdfBuffer = await this.pdfService.generateReport('invoice', data);
const pdf = await page.pdf( format: 'A4', printBackground: true, margin: top: '20mm', bottom: '20mm', left: '15mm', right: '15mm' , );