From dac9ca4814573bc64735ab28d6613054d2129d4f Mon Sep 17 00:00:00 2001 From: Shahzad Bin Shahjahan <147025839+shzd-tridz@users.noreply.github.com> Date: Thu, 27 Nov 2025 11:14:59 +0530 Subject: [PATCH] Implement iframe printing for POS Invoice Refactor printing logic to use iframe for rendering HTML and triggering print dialog. --- urypos/src/stores/invoiceData.js | 67 +++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/urypos/src/stores/invoiceData.js b/urypos/src/stores/invoiceData.js index e076fa2..f7aec62 100644 --- a/urypos/src/stores/invoiceData.js +++ b/urypos/src/stores/invoiceData.js @@ -387,20 +387,61 @@ export const useInvoiceDataStore = defineStore("invoiceData", { }; } } else { - const sendObj = { - doctype: "POS Invoice", - name: invoiceNo, - print_format: this.print_format, - }; - this.call - .post("ury.ury.api.ury_print.print_pos_page", sendObj) - .then((result) => { - this.notification.createNotification("Print Successful"); - window.location.reload(); + // Socket printing using iframe technique + try { + const printHTML = { + doc: "POS Invoice", + name: invoiceNo, + print_format: this.print_format, + _lang: "en", + }; + const result = await this.call.get( + "frappe.www.printview.get_html_and_style", + printHTML + ); + + if (!result?.message?.html) { + this.isPrinting = false; + this.alert.createAlert( + "Message", + "Error while getting the HTML document to print", + "OK" + ); + return; + } - return result.message; - }) - .catch((error) => console.error(error)); + // Create iframe for printing + const iframe = document.createElement('iframe'); + iframe.id = 'print_content'; + document.body.appendChild(iframe); + + // Write HTML to iframe and print + iframe.contentWindow.document.open(); + iframe.contentWindow.document.write(result.message.html); + iframe.contentWindow.document.close(); + + // Trigger print dialog + iframe.contentWindow.print(); + + // Clean up: remove iframe after a short delay to allow print dialog to open + setTimeout(() => { + if (iframe.parentNode) { + iframe.parentNode.removeChild(iframe); + } + }, 100); + + this.notification.createNotification("Print Successful"); + this.isPrinting = false; + window.location.reload(); + } catch (error) { + console.error(error); + this.isPrinting = false; + if (error._server_messages) { + const messages = JSON.parse(error._server_messages); + const message = JSON.parse(messages[0]); + this.alert.createAlert("Message", message.message, "OK"); + } + } } } catch (e) { if (e?.custom) {