diff --git a/src/Pages/BookingScreen/Components/BookingFunctionality/MobileMultiPdfPrint.jsx b/src/Pages/BookingScreen/Components/BookingFunctionality/MobileMultiPdfPrint.jsx index 248a583..2b47414 100644 --- a/src/Pages/BookingScreen/Components/BookingFunctionality/MobileMultiPdfPrint.jsx +++ b/src/Pages/BookingScreen/Components/BookingFunctionality/MobileMultiPdfPrint.jsx @@ -9,6 +9,7 @@ import { PrintStyleFunction } from '../../../paymentpdfPage/PrintStyleFunction'; import { PDFDocument } from 'pdf-lib'; import { PublicQrCodepost } from '../../../../Features/ConfigMasterPage/ConfigMasterPage'; import { renderToStaticMarkup } from 'react-dom/server'; +import { downloadFile } from '../../../../utils/downloadFile'; const MainHomeUrl = import.meta.env.ENV_MAIN_BASE_URL; const uploadApiUrl = import.meta.env.ENV_IMAGE_UPLOAD_API_URL; @@ -55,12 +56,12 @@ export const MobileMultiPdfPrint = async ({ printerTemplateStyle = 'Style 13', printDatas = {}, PrintOrderDetails = [], - setPrintOrderDetails = () => {}, + setPrintOrderDetails = () => { }, PrintComing = '', emailId = '', dispatch, UserId = '', - onProgress = () => {}, + onProgress = () => { }, returnBlob = false, }) => { try { @@ -136,24 +137,45 @@ export const MobileMultiPdfPrint = async ({ await new Promise((r) => setTimeout(r, 5)); } - // 🔥 MERGE ALL PDFs + // // 🔥 MERGE ALL PDFs + // const combinedPdfBlob = await mergePdfBlobs(allBlobs); + // const base64Pdf = await blobToBase64(combinedPdfBlob); + + // // 🔥 DOWNLOAD + // if (PrintComing === 'download') { + // const blobUrl = createSecureBlobUrl(combinedPdfBlob); + + // const link = document.createElement('a'); + // link.href = blobUrl; + // link.download = 'Receipts.pdf'; + // document.body.appendChild(link); + // link.click(); + + // setTimeout(() => { + // document.body.removeChild(link); + // revokeSecureBlobUrl(blobUrl); + // }, 100); + // } const combinedPdfBlob = await mergePdfBlobs(allBlobs); const base64Pdf = await blobToBase64(combinedPdfBlob); // 🔥 DOWNLOAD + if (PrintComing === 'download') { - const blobUrl = createSecureBlobUrl(combinedPdfBlob); + // ✅ Convert Blob → ArrayBuffer for downloadFile + const buffer = await combinedPdfBlob.arrayBuffer(); - const link = document.createElement('a'); - link.href = blobUrl; - link.download = 'Receipts.pdf'; - document.body.appendChild(link); - link.click(); + downloadFile( + buffer, + "Receipts.pdf", + "application/pdf", + (success) => { + if (!success) { - setTimeout(() => { - document.body.removeChild(link); - revokeSecureBlobUrl(blobUrl); - }, 100); + console.error('Failed to download the file. Please try again.'); + } + } + ); } // 🔥 RETURN BLOB @@ -242,7 +264,7 @@ export const MobileMultiPdfPrint = async ({ setTimeout(() => { try { window.location.href = intentUrl; - } catch {console.log("some errors")} + } catch { console.log("some errors") } }, 500); } @@ -377,7 +399,7 @@ export const MobileMultiPdfPrint = async ({ // revokeSecureBlobUrl(blobUrl); // }, 100); // } - + // // Return blob if requested (after download completes) // if (returnBlob && PrintComing === 'download') { // setPrintOrderDetails([]); @@ -585,7 +607,7 @@ export const MobileMultiCreditPdfPrint = async ({ emailId = '', dispatch, UserId = '', - onProgress = () => {}, + onProgress = () => { }, BATCH_SIZE = 5, }) => { if (!BSCreditCustomerPrint) { @@ -769,7 +791,33 @@ export const MobileMultiCreditPdfPrint = async ({ onProgress(Math.round((processedVisible / total) * 100)); } - // merge blobs - use existing merge function if available, otherwise fallback + // // merge blobs - use existing merge function if available, otherwise fallback + // const combinedPdfBlob = + // typeof mergePdfBlobs === 'function' + // ? await mergePdfBlobs(allBlobs) + // : await mergePdfBlobsFallback(allBlobs); + + // const base64Pdf = await blobToBase64(combinedPdfBlob); + + // // Action handlers + // if (PrintComing === 'download') { + // const blobUrl = + // typeof createSecureBlobUrl === 'function' + // ? createSecureBlobUrl(combinedPdfBlob) + // : URL.createObjectURL(combinedPdfBlob); + // const link = document.createElement('a'); + // link.href = blobUrl; + // link.download = 'Receipts.pdf'; + // document.body.appendChild(link); + // link.click(); + // setTimeout(() => { + // document.body.removeChild(link); + // if (typeof revokeSecureBlobUrl === 'function') + // revokeSecureBlobUrl(blobUrl); + // else URL.revokeObjectURL(blobUrl); + // }, 300); + // return { success: true }; + // } const combinedPdfBlob = typeof mergePdfBlobs === 'function' ? await mergePdfBlobs(allBlobs) @@ -779,24 +827,22 @@ export const MobileMultiCreditPdfPrint = async ({ // Action handlers if (PrintComing === 'download') { - const blobUrl = - typeof createSecureBlobUrl === 'function' - ? createSecureBlobUrl(combinedPdfBlob) - : URL.createObjectURL(combinedPdfBlob); - const link = document.createElement('a'); - link.href = blobUrl; - link.download = 'Receipts.pdf'; - document.body.appendChild(link); - link.click(); - setTimeout(() => { - document.body.removeChild(link); - if (typeof revokeSecureBlobUrl === 'function') - revokeSecureBlobUrl(blobUrl); - else URL.revokeObjectURL(blobUrl); - }, 300); + // ✅ Convert Blob → ArrayBuffer for downloadFile + const buffer = await combinedPdfBlob.arrayBuffer(); + + downloadFile( + buffer, + "Receipts.pdf", + "application/pdf", + (success) => { + if (!success) { + console.error('Failed to download the file. Please try again.'); + } + } + ); + return { success: true }; } - if (PrintComing === 'whatsapp') { const orderIdsArray = PrintOrderDetails.map((r) => r?.receiptNo).filter( Boolean @@ -908,7 +954,7 @@ export const MobileMultiCreditPdfPrint = async ({ ).forEach((el) => { try { el.parentElement.removeChild(el); - } catch (e) {} + } catch (e) { } }); } catch (e) { console.warn('Cleanup error', e);