import { buildInvoicePartyDetailsSection, buildPurchasePartyDetailsSection } from '../../../shared/party-details-section/pdf-party-details.section'; import { buildInvoiceTableData, getAvailablePageWidth } from '../../../shared/table-section/pdf-table.section'; import { buildInvoiceTotalsAndNotesSection, buildSignatureSection } from '../../../shared/totals-section/pdf-totals.section'; import { toStr } from '../../../shared/header-footer-section/pdf-shared.utils'; import { buildFooter, buildDocumentHeaderSection, buildPageNumberHeader, buildDocumentHeadingSection, buildLogoWatermarkBackground, buildWatermark, deriveHeaderDataFromInvoice, normalizeHeaderStyle, resolvePageMargins, resolvePageOrientation, resolvePageSize } from '../../../shared/header-footer-section/pdf-header-footer.section'; const PAGE_MARGINS: [number, number, number, number] = [20, 20, 20, 20]; const CELL_PADDING_LEFT = 4; const CELL_PADDING_RIGHT = 4; const BORDER_WIDTH = 0.5; const TABLE_BORDER_COLOR = '#d3d3d3'; export function CreateBillPDFService(PDFInvoiceData: any = {}, PrintConfigData: any = {}, printCopies: string[] = []) { const dd = buildPdfDocDefinition(PDFInvoiceData, PrintConfigData, printCopies); // pdfMake.createPdf(dd).open(); return dd; } export function DownloadBillPDFService(filename: string = 'bill.pdf', PDFInvoiceData: any = {}, PrintConfigData: any = {}, printCopies: string[] = []) { const dd = buildPdfDocDefinition(PDFInvoiceData, PrintConfigData, printCopies); // pdfMake.createPdf(dd).download(filename); return dd; } export function GetBillPdfDataUrl(PDFInvoiceData: any = {}, PrintConfigData: any = {}, printCopies: string[] = []) { const dd = buildPdfDocDefinition(PDFInvoiceData, PrintConfigData, printCopies); return dd; // return new Promise((resolve, reject) => { // let settled = false; // const done = (value: string) => { // if (!settled) { // settled = true; // resolve(value); // } // }; // const fail = (error: any) => { // if (!settled) { // settled = true; // reject(error); // } // }; // try { // if (typeof pdfDoc.getDataUrl === 'function') { // const result = pdfDoc.getDataUrl((dataUrl: string) => done(dataUrl)); // if (result && typeof result.then === 'function') { // result.then((dataUrl: string) => done(dataUrl)).catch(fail); // } // return; // } // if (typeof pdfDoc.getBlob === 'function') { // const result = pdfDoc.getBlob((blob: Blob) => done(URL.createObjectURL(blob))); // if (result && typeof result.then === 'function') { // result.then((blob: Blob) => done(URL.createObjectURL(blob))).catch(fail); // } // return; // } // fail(new Error('No supported preview API found on pdf document instance.')); // } catch (error) { // fail(error); // } // }); } function buildPdfDocDefinition(PDFInvoiceData: any = {}, PrintConfigData: any = {}, printCopies: string[] = []) { const PrintConfig = normalizePrintConfig(PDFInvoiceData, PrintConfigData); const requestedPageOrientation = resolvePageOrientation(PrintConfig?.PageOrientation); const pageSize = resolvePageSize(PrintConfig?.PSize, requestedPageOrientation); const basePageMargins = resolvePageMargins(PrintConfig?.pageMargins); const copyLabels = normalizeCopyLabels(printCopies); const watermark = buildWatermark(PrintConfig); const background = buildLogoWatermarkBackground(PrintConfig, PDFInvoiceData); const pageHeader = buildPageNumberHeader(PrintConfig); const pageFooter = buildFooter(PrintConfig); const pageMargins: [number, number, number, number] = pageHeader !== null ? [basePageMargins[0], Math.max(basePageMargins[1], 18), basePageMargins[2], basePageMargins[3]] : basePageMargins; const availableWidth = getAvailablePageWidth(pageSize, requestedPageOrientation, pageMargins); const content: any[] = []; copyLabels.forEach((copyLabel: string, index: number) => { const invoiceHeaderContent = buildDocumentHeaderSection(PrintConfig, PDFInvoiceData); const invoiceHeadingContent = buildDocumentHeadingSection(PDFInvoiceData, PrintConfig, availableWidth); const invoicePartyDetailsContent = buildPurchasePartyDetailsSection(PDFInvoiceData, PrintConfig?.Customer, PrintConfig); const invoiceTotalsAndNotesContent = buildInvoiceTotalsAndNotesSection(PDFInvoiceData, availableWidth, PrintConfigData); const signatureContent = buildSignatureSection(PrintConfig, PDFInvoiceData, availableWidth); const tableData = buildInvoiceTableData(PDFInvoiceData, PrintConfig, availableWidth); if (index > 0) { content.push({ text: '', pageBreak: 'before' }); } if (copyLabel) { content.push({ text: copyLabel, alignment: 'right', bold: true, fontSize: 8, margin: [0, 0, 0, 2] }); } content.push( ...invoiceHeaderContent, ...invoiceHeadingContent, ...invoicePartyDetailsContent, { table: { lineHeight: 2, headerRows: tableData.headerRows.length, widths: tableData.widths, body: tableData.body }, layout: { paddingLeft: () => CELL_PADDING_LEFT, paddingRight: () => CELL_PADDING_RIGHT, vLineWidth: () => BORDER_WIDTH, hLineWidth: () => BORDER_WIDTH, vLineColor: () => TABLE_BORDER_COLOR, hLineColor: () => TABLE_BORDER_COLOR } }, ...invoiceTotalsAndNotesContent, ...signatureContent ); }); const dd = { pageSize: pageSize, ...(typeof pageSize === 'string' ? { pageOrientation: requestedPageOrientation } : {}), pageMargins: pageMargins, content: content, info: { title: 'invoice', subject: Array.isArray(printCopies) ? printCopies.join(', ') : '' }, ...(background ? { background } : {}), ...(watermark ? { watermark } : {}), ...(pageHeader !== null ? { header: pageHeader } : {}), ...(pageFooter !== null ? { footer: pageFooter } : {}) }; return dd; } function normalizeCopyLabels(printCopies: string[] = []) { const labels = Array.isArray(printCopies) ? printCopies .map((entry: any) => toStr(entry).trim()) .filter((entry: string) => !!entry) : []; return labels.length > 0 ? labels : ['']; } function normalizePrintConfig(PDFInvoiceData: any, PrintConfigData: any) { const config = PrintConfigData || {}; const itemsHeader = Array.isArray(config?.TableConfig) ? config.TableConfig : []; const headerData = config?.headerData ? config.headerData : deriveHeaderDataFromInvoice(PDFInvoiceData); const pageMargins = resolvePageMargins(config?.Margin ?? PAGE_MARGINS); const watermarkText = toStr(config?.WatermarkText); const watermark = !!config?.Watermark; const watermarkEnabled = !!config?.WatermarkEnabled; const footerText = toStr(config?.Footer?.Text); const headerStyles = normalizeHeaderStyle(config?.headerStyles ?? { OrgNameFsize: config?.OrgNameFsize, OrgNameClr: config?.OrgNameClr, AdrsFsize: config?.AdrsFsize, AdrsClr: config?.AdrsClr }); const fixedTo = normalizeFixedTo(config?.fixedTo); return { ...config, itemsHeader, headerData, pageMargins, watermarkText, watermark, watermarkEnabled, footerText, headerStyles, fixedTo }; } function normalizeFixedTo(value: any) { const parsed = Number(value); if (!Number.isFinite(parsed) || parsed < 0) { return 2; } return Math.floor(parsed); }