JAVASCRIPT   74

init.js

Guest on 30th July 2021 02:51:09 PM

  1. 'use strict'
  2.  
  3. import Browser from './browser'
  4. import Modal from './modal'
  5. import Pdf from './pdf'
  6. import Html from './html'
  7. import Image from './image'
  8. import Json from './json'
  9.  
  10. let printTypes = ['pdf', 'html', 'image', 'json']
  11.  
  12. export default {
  13.   init () {
  14.     let params = {
  15.       printable: null,
  16.       fallbackPrintable: null,
  17.       type: 'pdf',
  18.       header: null,
  19.       headerStyle: 'font-weight: 300;',
  20.       maxWidth: 800,
  21.       font: 'TimesNewRoman',
  22.       font_size: '12pt',
  23.       honorMarginPadding: true,
  24.       honorColor: false,
  25.       properties: null,
  26.       gridHeaderStyle: 'font-weight: bold;',
  27.       gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;',
  28.       showModal: false,
  29.       onLoadingStart: function () {},
  30.       onLoadingEnd: function () {},
  31.       modalMessage: 'Retrieving Document...',
  32.       frameId: 'printJS',
  33.       htmlData: '',
  34.       documentTitle: 'Document'
  35.     }
  36.  
  37.     // Check if a printable document or object was supplied
  38.     let args = arguments[0]
  39.     if (args === undefined) {
  40.       throw new Error('printJS expects at least 1 attribute.')
  41.     }
  42.  
  43.     switch (typeof args) {
  44.       case 'string':
  45.         params.printable = encodeURI(args)
  46.         params.fallbackPrintable = params.printable
  47.         params.type = arguments[1] || params.type
  48.         break
  49.  
  50.       case 'object':
  51.         params.printable = args.printable
  52.         params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable
  53.         params.type = typeof args.type !== 'undefined' ? args.type : params.type
  54.         params.frameId = typeof args.frameId !== 'undefined' ? args.frameId : params.frameId
  55.         params.header = typeof args.header !== 'undefined' ? args.header : params.header
  56.         params.headerStyle = typeof args.headerStyle !== 'undefined' ? args.headerStyle : params.headerStyle
  57.         params.maxWidth = typeof args.maxWidth !== 'undefined' ? args.maxWidth : params.maxWidth
  58.         params.font = typeof args.font !== 'undefined' ? args.font : params.font
  59.         params.font_size = typeof args.font_size !== 'undefined' ? args.font_size : params.font_size
  60.         params.honorMarginPadding = typeof args.honorMarginPadding !== 'undefined' ? args.honorMarginPadding : params.honorMarginPadding
  61.         params.properties = typeof args.properties !== 'undefined' ? args.properties : params.properties
  62.         params.gridHeaderStyle = typeof args.gridHeaderStyle !== 'undefined' ? args.gridHeaderStyle : params.gridHeaderStyle
  63.         params.gridStyle = typeof args.gridStyle !== 'undefined' ? args.gridStyle : params.gridStyle
  64.         params.showModal = typeof args.showModal !== 'undefined' ? args.showModal : params.showModal
  65.         params.onLoadingStart = typeof args.onLoadingStart !== 'undefined' ? args.onLoadingStart : params.onLoadingStart
  66.         params.onLoadingEnd = typeof args.onLoadingEnd !== 'undefined' ? args.onLoadingEnd : params.onLoadingEnd
  67.         params.modalMessage = typeof args.modalMessage !== 'undefined' ? args.modalMessage : params.modalMessage
  68.         params.documentTitle = typeof args.documentTitle !== 'undefined' ? args.documentTitle : params.documentTitle
  69.         break
  70.       default:
  71.         throw new Error('Unexpected argument type! Expected "string" or "object", got ' + typeof args)
  72.     }
  73.  
  74.     if (!params.printable) {
  75.       throw new Error('Missing printable information.')
  76.     }
  77.  
  78.     if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) {
  79.       throw new Error('Invalid print type. Available types are: pdf, html, image and json.')
  80.     }
  81.  
  82.     // Check if we are showing a feedback message to the user (useful for large files)
  83.     if (params.showModal) {
  84.       Modal.show(params)
  85.     }
  86.     if (params.onLoadingStart) {
  87.       params.onLoadingStart()
  88.     }
  89.  
  90.     // To prevent duplication and issues, remove printFrame from the DOM, if it exists
  91.     let usedFrame = document.getElementById(params.frameId)
  92.  
  93.     if (usedFrame) {
  94.       usedFrame.parentNode.removeChild(usedFrame)
  95.     }
  96.  
  97.     // Create a new iframe or embed element (IE prints blank pdf's if we use iframe)
  98.     let printFrame
  99.  
  100.     // Create iframe element
  101.     printFrame = document.createElement('iframe')
  102.  
  103.     // Hide iframe
  104.     printFrame.setAttribute('style', 'display:none;')
  105.  
  106.     // Set element id
  107.     printFrame.setAttribute('id', params.frameId)
  108.  
  109.     // For non pdf printing, pass an empty html document to srcdoc (force onload callback)
  110.     if (params.type !== 'pdf') {
  111.       printFrame.srcdoc = '<html><head><title>' + params.documentTitle + '</title></head><body></body></html>'
  112.     }
  113.  
  114.     // Check printable type
  115.     switch (params.type) {
  116.       case 'pdf':
  117.         // Check browser support for pdf and if not supported we will just open the pdf file instead
  118.         if (Browser.isFirefox() || Browser.isEdge() || Browser.isIE()) {
  119.           console.log('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.')
  120.           let win = window.open(params.fallbackPrintable, '_blank')
  121.           win.focus()
  122.           // Make sure there is no loading modal opened
  123.           if (params.showModal) Modal.close()
  124.           if (params.onLoadingEnd) params.onLoadingEnd()
  125.         } else {
  126.           Pdf.print(params, printFrame)
  127.         }
  128.         break
  129.       case 'image':
  130.         Image.print(params, printFrame)
  131.         break
  132.       case 'html':
  133.         Html.print(params, printFrame)
  134.         break
  135.       case 'json':
  136.         Json.print(params, printFrame)
  137.         break
  138.       default:
  139.         throw new Error('Invalid print type. Available types are: pdf, html, image and json.')
  140.     }
  141.   }
  142. }

Raw Paste


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