From 502a429e3e87495dfd93da3cbeb19ca93614c536 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 23 Aug 2024 13:39:32 +0200 Subject: [PATCH 1/2] Use `HTMLCanvasElement.toBlob()` unconditionally in `PDFPrintService` The fallback is very old code, and according to the MDN compatibility data `HTMLCanvasElement.toBlob()` should be available in all browsers that we support now: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#browser_compatibility --- web/pdf_print_service.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 78ea670a7..7017345a0 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -190,14 +190,9 @@ class PDFPrintService { useRenderedPage() { this.throwIfInactive(); const img = document.createElement("img"); - const scratchCanvas = this.scratchCanvas; - if ("toBlob" in scratchCanvas) { - scratchCanvas.toBlob(function (blob) { - img.src = URL.createObjectURL(blob); - }); - } else { - img.src = scratchCanvas.toDataURL(); - } + this.scratchCanvas.toBlob(blob => { + img.src = URL.createObjectURL(blob); + }); const wrapper = document.createElement("div"); wrapper.className = "printedPage"; From 6a1b1ae6a42fcc421d513852b9164be04097b96b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 23 Aug 2024 18:39:28 +0200 Subject: [PATCH 2/2] Revoke the blob-URLs used during printing in `PDFPrintService` --- web/pdf_print_service.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 7017345a0..45106ca30 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -199,10 +199,18 @@ class PDFPrintService { wrapper.append(img); this.printContainer.append(wrapper); - return new Promise(function (resolve, reject) { - img.onload = resolve; - img.onerror = reject; - }); + const { promise, resolve, reject } = Promise.withResolvers(); + img.onload = resolve; + img.onerror = reject; + + promise + .catch(() => { + // Avoid "Uncaught promise" messages in the console. + }) + .then(() => { + URL.revokeObjectURL(img.src); + }); + return promise; } performPrint() {