changeset 2163:0a2b450cec3a

Added a Print button in the PDF viewer toolbar
author Alain Mazy <am@orthanc.team>
date Thu, 03 Oct 2024 14:23:14 +0200
parents 4f224edac348
children 952674a812c2
files Applications/StoneWebViewer/NEWS Applications/StoneWebViewer/WebApplication/index.html Applications/StoneWebViewer/WebApplication/pdf-viewer.js
diffstat 3 files changed, 28 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Applications/StoneWebViewer/NEWS	Thu Oct 03 09:53:54 2024 +0200
+++ b/Applications/StoneWebViewer/NEWS	Thu Oct 03 14:23:14 2024 +0200
@@ -2,7 +2,7 @@
 ===============================
 
 * Remember the previous layout when re-opening the viewer.
-
+* Added a Print button in the PDF viewer toolbar.
 
 Version 2.6 (2024-08-31)
 ========================
--- a/Applications/StoneWebViewer/WebApplication/index.html	Thu Oct 03 09:53:54 2024 +0200
+++ b/Applications/StoneWebViewer/WebApplication/index.html	Thu Oct 03 14:23:14 2024 +0200
@@ -923,6 +923,10 @@
                     data-toggle="tooltip" data-title="Show next page">
               <i class="fa fa-chevron-circle-right"></i>
             </button>
+            <button class="btn btn-primary" @click="Print()"
+                    data-toggle="tooltip" data-title="Print">
+              <i class="fa fa-print"></i>
+            </button>
           </div>
         </div>
       </div>
--- a/Applications/StoneWebViewer/WebApplication/pdf-viewer.js	Thu Oct 03 09:53:54 2024 +0200
+++ b/Applications/StoneWebViewer/WebApplication/pdf-viewer.js	Thu Oct 03 14:23:14 2024 +0200
@@ -92,6 +92,29 @@
     });
   },
   methods: {
+    Print: function() {
+      if (this.pdfDoc !== null) {
+        if (0) { // works on Chrome but with a popup that is blocked by default !
+          const blob = new Blob([this.pdf], { type: 'application/pdf'});
+          const blobUrl = URL.createObjectURL(blob);
+
+          let w = window.open(blobUrl, '_blank');
+          w.print();
+        } else {
+          // Let's open a new window with the pdf
+          // First we need to convert the pdf from a byte array to a binary string and then to b64
+          let binaryStringPdf = '';
+          for (let i = 0; i < this.pdf.length; i++) {
+            binaryStringPdf += String.fromCharCode(this.pdf[i]);
+          }
+
+          const htmlContent = '<html><body style="margin: 0;"><embed width="100%" height="100%" src="data:application/pdf;base64,' + btoa(binaryStringPdf) + '" type="application/pdf" /></body></html>';
+
+          let w = window.open('', '_blank');
+          w.document.write(htmlContent);
+        }
+      }
+    },
     NextPage: function() {
       if (this.pdfDoc !== null &&
           this.currentPage < this.pdfDoc.numPages) {