JAVASCRIPT   148

print.js

Guest on 30th July 2021 02:54:33 PM

  1. import Browser from './browser'
  2. import Modal from './modal'
  3.  
  4. const Print = {
  5.   send: (params, printFrame) => {
  6.     // Append iframe element to document body
  7.     document.getElementsByTagName('body')[0].appendChild(printFrame)
  8.  
  9.     // Get iframe element
  10.     let iframeElement = document.getElementById(params.frameId)
  11.  
  12.     // Wait for iframe to load all content
  13.     if (params.type === 'pdf' && (Browser.isIE() || Browser.isEdge())) {
  14.       iframeElement.setAttribute('onload', finishPrint(iframeElement, params))
  15.     } else {
  16.       printFrame.onload = () => {
  17.         if (params.type === 'pdf') {
  18.           finishPrint(iframeElement, params)
  19.         } else {
  20.           // Get iframe element document
  21.           let printDocument = (iframeElement.contentWindow || iframeElement.contentDocument)
  22.           if (printDocument.document) printDocument = printDocument.document
  23.  
  24.           // Inject printable html into iframe body
  25.           printDocument.body.innerHTML = params.htmlData
  26.  
  27.           // If printing image, wait for it to load inside the iframe (skip firefox)
  28.           if (params.type === 'image') {
  29.             loadImageAndFinishPrint(printDocument.getElementById('printableImage'), iframeElement, params)
  30.           } else {
  31.             finishPrint(iframeElement, params)
  32.           }
  33.         }
  34.       }
  35.     }
  36.   }
  37. }
  38.  
  39. function finishPrint (iframeElement, params) {
  40.   iframeElement.focus()
  41.  
  42.   // If Edge or IE, try catch with execCommand
  43.   if (Browser.isEdge() || (Browser.isIE())) {
  44.     try {
  45.       iframeElement.contentWindow.document.execCommand('print', false, null)
  46.     } catch (e) {
  47.       iframeElement.contentWindow.print()
  48.     }
  49.   }
  50.  
  51.   // Other browsers
  52.   if (!Browser.isIE() && !Browser.isEdge()) {
  53.     iframeElement.contentWindow.print()
  54.   }
  55.  
  56.   // Remove embed on IE (just because it isn't 100% hidden when using h/w = 0)
  57.   if (Browser.isIE() && params.type === 'pdf') {
  58.     setTimeout(() => {
  59.       iframeElement.parentNode.removeChild(iframeElement)
  60.     }, 2000)
  61.   }
  62.  
  63.   // If we are showing a feedback message to user, remove it
  64.   if (params.showModal) {
  65.     Modal.close()
  66.   }
  67.   if (params.onLoadingEnd) {
  68.     params.onLoadingEnd()
  69.   }
  70. }
  71.  
  72. function loadImageAndFinishPrint (img, iframeElement, params) {
  73.   if (typeof img.naturalWidth === 'undefined' || img.naturalWidth === 0) {
  74.     setTimeout(() => {
  75.       loadImageAndFinishPrint(img, iframeElement, params)
  76.     }, 500)
  77.   } else {
  78.     finishPrint(iframeElement, params)
  79.   }
  80. }
  81.  
  82. export default Print

Raw Paste


Login or Register to edit or fork this paste. It's free.