From d8dfd8a6f27acc29c49dfdfa972f6838ba206ba6 Mon Sep 17 00:00:00 2001 From: AllenTJ7 <163137620+AllenTJ7@users.noreply.github.com> Date: Tue, 20 May 2025 13:50:04 +0530 Subject: [PATCH] pdf changes --- .../payments/phonepe/invoice/invoiceService.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/functions/src/payments/phonepe/invoice/invoiceService.ts b/functions/src/payments/phonepe/invoice/invoiceService.ts index eb8a1e5..c1edf9b 100644 --- a/functions/src/payments/phonepe/invoice/invoiceService.ts +++ b/functions/src/payments/phonepe/invoice/invoiceService.ts @@ -4,10 +4,18 @@ import * as os from 'os'; import * as path from 'path'; import { format } from 'date-fns'; import { sendEmailWithAttachmentUtil } from "../../../utils/emailService"; -import * as pdfMake from 'pdfmake/build/pdfmake'; -import * as pdfFonts from 'pdfmake/build/vfs_fonts'; -(pdfMake as any).vfs = (pdfFonts as any).pdfMake ? (pdfFonts as any).pdfMake.vfs : pdfFonts.vfs; +let pdfMake: any; +let pdfFonts: any; + +function initPdfLibraries() { + if (!pdfMake) { + pdfMake = require('pdfmake/build/pdfmake'); + pdfFonts = require('pdfmake/build/vfs_fonts'); + pdfMake.vfs = pdfFonts.pdfMake ? pdfFonts.pdfMake.vfs : pdfFonts.vfs; + } + return pdfMake; +} const admin = getAdmin(); const logger = getLogger(); @@ -45,6 +53,7 @@ export interface EmailOptions { export class InvoiceService { async generateInvoice(data: InvoiceData): Promise { try { + const pdfMake = initPdfLibraries(); const tempFilePath = path.join(os.tmpdir(), `invoice_${data.invoiceNumber}.pdf`); const hasGst = data.gstNumber && data.gstNumber.length > 0; @@ -255,7 +264,7 @@ export class InvoiceService { const pdfDoc = pdfMake.createPdf(docDefinition); await new Promise((resolve, reject) => { - pdfDoc.getBuffer((buffer) => { + pdfDoc.getBuffer((buffer: Buffer) => { fs.writeFile(tempFilePath, buffer, (err) => { if (err) reject(err); else resolve();