UNPKG

safeer-pdf-generator

Version:

Framework-agnostic PDF generation library with chunking, merging, S3 upload, and email delivery

75 lines (64 loc) 2.35 kB
import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { generatePdf } from '@safeersoft/@safeersoft/pdf-reporter'; @Injectable() export class SimplifiedPdfService { private readonly logger = new Logger(SimplifiedPdfService.name); constructor(private readonly configService: ConfigService) {} async generatePdf(params: { title: string; data: any[]; columns: any[]; emailTo?: string; userInfo?: any; }) { try { this.logger.log(`Generating PDF: ${params.title}`); const result = await generatePdf({ title: params.title, data: params.data, columns: params.columns, userInfo: params.userInfo, // S3 Upload (auto-configured from environment) s3: { bucket: this.configService.get('S3_BUCKET_NAME'), region: this.configService.get('AWS_REGION'), accessKeyId: this.configService.get('AWS_ACCESS_KEY_ID'), secretAccessKey: this.configService.get('AWS_SECRET_ACCESS_KEY'), }, // Email (optional, auto-configured from environment) email: params.emailTo ? { to: params.emailTo, smtp: { host: this.configService.get('SMTP_HOST'), port: parseInt(this.configService.get('SMTP_PORT', '587')), secure: this.configService.get('SMTP_SECURE') === 'true', auth: { user: this.configService.get('SMTP_USER'), pass: this.configService.get('SMTP_PASS'), }, }, } : undefined, // Custom logging to integrate with NestJS logging: { debug: (msg: string) => this.logger.debug(msg), info: (msg: string) => this.logger.log(msg), warn: (msg: string) => this.logger.warn(msg), error: (msg: string) => this.logger.error(msg), }, }); this.logger.log(`PDF generated successfully in ${result.durationMs}ms`); return { success: true, url: result.s3?.url, fileName: result.fileName, size: result.sizeBytes, processingTime: result.durationMs, emailSent: !!params.emailTo, }; } catch (error) { this.logger.error(`PDF generation failed: ${error.message}`); throw error; } } }