print.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory();
  4. else if(typeof define === 'function' && define.amd)
  5. define("print-js", [], factory);
  6. else if(typeof exports === 'object')
  7. exports["print-js"] = factory();
  8. else
  9. root["print-js"] = factory();
  10. })(this, function() {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // identity function for calling harmony imports with the correct context
  47. /******/ __webpack_require__.i = function(value) { return value; };
  48. /******/
  49. /******/ // define getter function for harmony exports
  50. /******/ __webpack_require__.d = function(exports, name, getter) {
  51. /******/ if(!__webpack_require__.o(exports, name)) {
  52. /******/ Object.defineProperty(exports, name, {
  53. /******/ configurable: false,
  54. /******/ enumerable: true,
  55. /******/ get: getter
  56. /******/ });
  57. /******/ }
  58. /******/ };
  59. /******/
  60. /******/ // getDefaultExport function for compatibility with non-harmony modules
  61. /******/ __webpack_require__.n = function(module) {
  62. /******/ var getter = module && module.__esModule ?
  63. /******/ function getDefault() { return module['default']; } :
  64. /******/ function getModuleExports() { return module; };
  65. /******/ __webpack_require__.d(getter, 'a', getter);
  66. /******/ return getter;
  67. /******/ };
  68. /******/
  69. /******/ // Object.prototype.hasOwnProperty.call
  70. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  71. /******/
  72. /******/ // __webpack_public_path__
  73. /******/ __webpack_require__.p = "./";
  74. /******/
  75. /******/ // Load entry module and return exports
  76. /******/ return __webpack_require__(__webpack_require__.s = 10);
  77. /******/ })
  78. /************************************************************************/
  79. /******/ ([
  80. /* 0 */
  81. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  82. "use strict";
  83. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(2);
  84. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
  85. var Print = {
  86. send: function send(params, printFrame) {
  87. // Append iframe element to document body
  88. document.getElementsByTagName('body')[0].appendChild(printFrame);
  89. // Get iframe element
  90. var iframeElement = document.getElementById(params.frameId);
  91. // Wait for iframe to load all content
  92. if (params.type === 'pdf' && (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge())) {
  93. iframeElement.setAttribute('onload', finishPrint(iframeElement, params));
  94. } else {
  95. printFrame.onload = function () {
  96. if (params.type === 'pdf') {
  97. finishPrint(iframeElement, params);
  98. } else {
  99. // Get iframe element document
  100. var printDocument = iframeElement.contentWindow || iframeElement.contentDocument;
  101. if (printDocument.document) printDocument = printDocument.document;
  102. // Inject printable html into iframe body
  103. printDocument.body.innerHTML = params.htmlData;
  104. // Add custom style
  105. if (params.type !== 'pdf' && params.style !== null) {
  106. // Create style element
  107. var style = document.createElement('style');
  108. style.innerHTML = params.style;
  109. // Append style element to iframe's head
  110. printDocument.head.appendChild(style);
  111. }
  112. // If printing image, wait for it to load inside the iframe
  113. if (params.type === 'image') {
  114. loadIframeImages(printDocument, params).then(function () {
  115. finishPrint(iframeElement, params);
  116. });
  117. } else {
  118. finishPrint(iframeElement, params);
  119. }
  120. }
  121. };
  122. }
  123. }
  124. };
  125. function finishPrint(iframeElement, params) {
  126. iframeElement.focus();
  127. // If Edge or IE, try catch with execCommand
  128. if (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE()) {
  129. try {
  130. iframeElement.contentWindow.document.execCommand('print', false, null);
  131. } catch (e) {
  132. iframeElement.contentWindow.print();
  133. }
  134. } else {
  135. // Other browsers
  136. iframeElement.contentWindow.print();
  137. }
  138. // If we are showing a feedback message to user, remove it
  139. if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
  140. // Check for a finished loading hook function
  141. if (params.onLoadingEnd) params.onLoadingEnd();
  142. // If preloading pdf files, clean blob url
  143. if (params.showModal || params.onLoadingStart) window.URL.revokeObjectURL(params.printable);
  144. }
  145. function loadIframeImages(printDocument, params) {
  146. var promises = [];
  147. params.printable.forEach(function (image, index) {
  148. return promises.push(loadIframeImage(printDocument, index));
  149. });
  150. return Promise.all(promises);
  151. }
  152. function loadIframeImage(printDocument, index) {
  153. return new Promise(function (resolve) {
  154. var image = printDocument ? printDocument.getElementById('printableImage' + index) : null;
  155. if (!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0) {
  156. setTimeout(function () {
  157. loadIframeImage(printDocument, index);
  158. }, 500);
  159. } else {
  160. resolve();
  161. }
  162. });
  163. }
  164. /* harmony default export */ __webpack_exports__["a"] = (Print);
  165. /***/ }),
  166. /* 1 */
  167. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  168. "use strict";
  169. /* harmony export (immutable) */ __webpack_exports__["a"] = addWrapper;
  170. /* harmony export (immutable) */ __webpack_exports__["b"] = capitalizePrint;
  171. /* harmony export (immutable) */ __webpack_exports__["c"] = collectStyles;
  172. /* harmony export (immutable) */ __webpack_exports__["d"] = loopNodesCollectStyles;
  173. /* harmony export (immutable) */ __webpack_exports__["e"] = addHeader;
  174. function addWrapper(htmlData, params) {
  175. var bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;';
  176. return '<div style="' + bodyStyle + '">' + htmlData + '</div>';
  177. }
  178. function capitalizePrint(obj) {
  179. return obj.charAt(0).toUpperCase() + obj.slice(1);
  180. }
  181. function collectStyles(element, params) {
  182. var win = document.defaultView || window;
  183. // String variable to hold styling for each element
  184. var elementStyle = '';
  185. // Loop over computed styles
  186. var styles = win.getComputedStyle(element, '');
  187. Object.keys(styles).map(function (key) {
  188. // Check if style should be processed
  189. if (params.targetStyles.indexOf('*') !== -1 || params.targetStyle.indexOf(styles[key]) !== -1 || targetStylesMatch(params.targetStyles, styles[key])) {
  190. if (styles.getPropertyValue(styles[key])) elementStyle += styles[key] + ':' + styles.getPropertyValue(styles[key]) + ';';
  191. }
  192. });
  193. // Print friendly defaults
  194. elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;';
  195. return elementStyle;
  196. }
  197. function targetStylesMatch(styles, value) {
  198. for (var i = 0; i < styles.length; i++) {
  199. if (value.indexOf(styles[i]) !== -1) return true;
  200. }
  201. return false;
  202. }
  203. function loopNodesCollectStyles(elements, params) {
  204. for (var n = 0; n < elements.length; n++) {
  205. var currentElement = elements[n];
  206. // Check if we are skiping this element
  207. if (params.ignoreElements.indexOf(currentElement.getAttribute('id')) !== -1) {
  208. currentElement.parentNode.removeChild(currentElement);
  209. continue;
  210. }
  211. // Form Printing - check if is element Input
  212. var tag = currentElement.tagName;
  213. if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
  214. // Save style to variable
  215. var textStyle = collectStyles(currentElement, params);
  216. // Remove INPUT element and insert a text node
  217. var parent = currentElement.parentNode;
  218. // Get text value
  219. var textNode = tag === 'SELECT' ? document.createTextNode(currentElement.options[currentElement.selectedIndex].text) : document.createTextNode(currentElement.value);
  220. // Create text element
  221. var textElement = document.createElement('div');
  222. textElement.appendChild(textNode);
  223. // Add style to text
  224. textElement.setAttribute('style', textStyle);
  225. // Add text
  226. parent.appendChild(textElement);
  227. // Remove input
  228. parent.removeChild(currentElement);
  229. } else {
  230. // Get all styling for print element
  231. currentElement.setAttribute('style', collectStyles(currentElement, params));
  232. }
  233. // Check if more elements in tree
  234. var children = currentElement.children;
  235. if (children && children.length) {
  236. loopNodesCollectStyles(children, params);
  237. }
  238. }
  239. }
  240. function addHeader(printElement, header, headerStyle) {
  241. // Create header element
  242. var headerElement = document.createElement('h1');
  243. // Create header text node
  244. var headerNode = document.createTextNode(header);
  245. // Build and style
  246. headerElement.appendChild(headerNode);
  247. headerElement.setAttribute('style', headerStyle);
  248. printElement.insertBefore(headerElement, printElement.childNodes[0]);
  249. }
  250. /***/ }),
  251. /* 2 */
  252. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  253. "use strict";
  254. var Browser = {
  255. // Firefox 1.0+
  256. isFirefox: function isFirefox() {
  257. return typeof InstallTrigger !== 'undefined';
  258. },
  259. // Internet Explorer 6-11
  260. isIE: function isIE() {
  261. return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode;
  262. },
  263. // Edge 20+
  264. isEdge: function isEdge() {
  265. return !Browser.isIE() && !!window.StyleMedia;
  266. },
  267. // Chrome 1+
  268. isChrome: function isChrome() {
  269. return !!window.chrome && !!window.chrome.webstore;
  270. },
  271. // At least Safari 3+: "[object HTMLElementConstructor]"
  272. isSafari: function isSafari() {
  273. return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || navigator.userAgent.toLowerCase().indexOf('safari') !== -1;
  274. }
  275. };
  276. /* harmony default export */ __webpack_exports__["a"] = (Browser);
  277. /***/ }),
  278. /* 3 */
  279. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  280. "use strict";
  281. var Modal = {
  282. show: function show(params) {
  283. // Build modal
  284. var modalStyle = 'font-family:sans-serif; ' + 'display:table; ' + 'text-align:center; ' + 'font-weight:300; ' + 'font-size:30px; ' + 'left:0; top:0;' + 'position:fixed; ' + 'z-index: 9990;' + 'color: #0460B5; ' + 'width: 100%; ' + 'height: 100%; ' + 'background-color:rgba(255,255,255,.9);' + 'transition: opacity .3s ease;';
  285. // Create wrapper
  286. var printModal = document.createElement('div');
  287. printModal.setAttribute('style', modalStyle);
  288. printModal.setAttribute('id', 'printJS-Modal');
  289. // Create content div
  290. var contentDiv = document.createElement('div');
  291. contentDiv.setAttribute('style', 'display:table-cell; vertical-align:middle; padding-bottom:100px;');
  292. // Add close button (requires print.css)
  293. var closeButton = document.createElement('div');
  294. closeButton.setAttribute('class', 'printClose');
  295. closeButton.setAttribute('id', 'printClose');
  296. contentDiv.appendChild(closeButton);
  297. // Add spinner (requires print.css)
  298. var spinner = document.createElement('span');
  299. spinner.setAttribute('class', 'printSpinner');
  300. contentDiv.appendChild(spinner);
  301. // Add message
  302. var messageNode = document.createTextNode(params.modalMessage);
  303. contentDiv.appendChild(messageNode);
  304. // Add contentDiv to printModal
  305. printModal.appendChild(contentDiv);
  306. // Append print modal element to document body
  307. document.getElementsByTagName('body')[0].appendChild(printModal);
  308. // Add event listener to close button
  309. document.getElementById('printClose').addEventListener('click', function () {
  310. Modal.close();
  311. });
  312. },
  313. close: function close() {
  314. var printFrame = document.getElementById('printJS-Modal');
  315. printFrame.parentNode.removeChild(printFrame);
  316. }
  317. };
  318. /* harmony default export */ __webpack_exports__["a"] = (Modal);
  319. /***/ }),
  320. /* 4 */
  321. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  322. "use strict";
  323. Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
  324. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__js_init__ = __webpack_require__(7);
  325. var printjs = __WEBPACK_IMPORTED_MODULE_0__js_init__["a" /* default */].init;
  326. if (typeof window !== 'undefined') {
  327. window.printJS = printjs;
  328. }
  329. /* harmony default export */ __webpack_exports__["default"] = (printjs);
  330. /***/ }),
  331. /* 5 */
  332. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  333. "use strict";
  334. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
  335. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
  336. /* harmony default export */ __webpack_exports__["a"] = ({
  337. print: function print(params, printFrame) {
  338. // Get HTML printable element
  339. var printElement = document.getElementById(params.printable);
  340. // Check if element exists
  341. if (!printElement) {
  342. window.console.error('Invalid HTML element id: ' + params.printable);
  343. return false;
  344. }
  345. // Make a copy of the printElement to prevent DOM changes
  346. var printableElement = document.createElement('div');
  347. printableElement.appendChild(printElement.cloneNode(true));
  348. // Add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles
  349. printableElement.setAttribute('style', 'height:0; overflow:hidden;');
  350. printableElement.setAttribute('id', 'printJS-html');
  351. printElement.parentNode.appendChild(printableElement);
  352. // Update printableElement variable with newly created DOM element
  353. printableElement = document.getElementById('printJS-html');
  354. // Process html styles
  355. if (params.scanStyles === true) {
  356. // Optional - include margin and padding
  357. if (params.honorMarginPadding) params.targetStyles.push('margin', 'padding');
  358. // Optional - include color
  359. if (params.honorColor) params.targetStyles.push('color');
  360. // Get main element styling
  361. printableElement.setAttribute('style', __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["c" /* collectStyles */])(printableElement, params) + 'margin:0 !important;');
  362. // Get all children elements
  363. var elements = printableElement.children;
  364. // Get styles for all children elements
  365. __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["d" /* loopNodesCollectStyles */])(elements, params);
  366. }
  367. // Add header
  368. if (params.header) {
  369. __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement, params.header, params.headerStyle);
  370. }
  371. // Remove DOM printableElement
  372. printableElement.parentNode.removeChild(printableElement);
  373. // Store html data
  374. params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(printableElement.innerHTML, params);
  375. // Print html element contents
  376. __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
  377. }
  378. });
  379. /***/ }),
  380. /* 6 */
  381. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  382. "use strict";
  383. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
  384. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
  385. /* harmony default export */ __webpack_exports__["a"] = ({
  386. print: function print(params, printFrame) {
  387. // Check if we are printing one image or multiple images
  388. if (params.printable.constructor !== Array) {
  389. // Create array with one image
  390. params.printable = [params.printable];
  391. }
  392. // Create printable element (container)
  393. var printableElement = document.createElement('div');
  394. printableElement.setAttribute('style', 'width:100%');
  395. // Load images and append
  396. loadImagesAndAppendToPrintableElement(printableElement, params).then(function () {
  397. // Check if we are adding a header
  398. if (params.header) __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement, params.header, params.headerStyle);
  399. // Store html data
  400. params.htmlData = printableElement.outerHTML;
  401. // Print image
  402. __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
  403. });
  404. }
  405. });
  406. function loadImagesAndAppendToPrintableElement(printableElement, params) {
  407. var promises = [];
  408. params.printable.forEach(function (image, index) {
  409. // Create the image element
  410. var img = document.createElement('img');
  411. // Set image src with image file url
  412. img.src = image;
  413. // Load image
  414. promises.push(loadImageAndAppendToPrintableElement(printableElement, params, img, index));
  415. });
  416. return Promise.all(promises);
  417. }
  418. function loadImageAndAppendToPrintableElement(printableElement, params, img, index) {
  419. return new Promise(function (resolve) {
  420. img.onload = function () {
  421. // Create image wrapper
  422. var imageWrapper = document.createElement('div');
  423. imageWrapper.setAttribute('style', params.imageStyle);
  424. img.setAttribute('style', 'width:100%;');
  425. img.setAttribute('id', 'printableImage' + index);
  426. // Append image to wrapper element
  427. imageWrapper.appendChild(img);
  428. // Append wrapper element to printable element
  429. printableElement.appendChild(imageWrapper);
  430. resolve();
  431. };
  432. });
  433. }
  434. /***/ }),
  435. /* 7 */
  436. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  437. "use strict";
  438. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(2);
  439. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
  440. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__pdf__ = __webpack_require__(9);
  441. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__html__ = __webpack_require__(5);
  442. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_4__image__ = __webpack_require__(6);
  443. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_5__json__ = __webpack_require__(8);
  444. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  445. var printTypes = ['pdf', 'html', 'image', 'json'];
  446. /* harmony default export */ __webpack_exports__["a"] = ({
  447. init: function init() {
  448. var params = {
  449. printable: null,
  450. fallbackPrintable: null,
  451. type: 'pdf',
  452. header: null,
  453. headerStyle: 'font-weight: 300;',
  454. maxWidth: 800,
  455. font: 'TimesNewRoman',
  456. font_size: '12pt',
  457. honorMarginPadding: true,
  458. honorColor: false,
  459. properties: null,
  460. gridHeaderStyle: 'font-weight: bold; padding: 5px; border: 1px solid #dddddd;',
  461. gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;',
  462. showModal: false,
  463. onLoadingStart: null,
  464. onLoadingEnd: null,
  465. modalMessage: 'Retrieving Document...',
  466. frameId: 'printJS',
  467. htmlData: '',
  468. documentTitle: 'Document',
  469. targetStyle: ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height'],
  470. targetStyles: ['border', 'box', 'break', 'text-decoration'],
  471. ignoreElements: [],
  472. imageStyle: 'width:100%;',
  473. repeatTableHeader: true,
  474. css: null,
  475. style: null,
  476. scanStyles: true
  477. // Check if a printable document or object was supplied
  478. };var args = arguments[0];
  479. if (args === undefined) throw new Error('printJS expects at least 1 attribute.');
  480. // Process parameters
  481. switch (typeof args === 'undefined' ? 'undefined' : _typeof(args)) {
  482. case 'string':
  483. params.printable = encodeURI(args);
  484. params.fallbackPrintable = params.printable;
  485. params.type = arguments[1] || params.type;
  486. break;
  487. case 'object':
  488. params.printable = args.printable;
  489. params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable;
  490. params.type = typeof args.type !== 'undefined' ? args.type : params.type;
  491. params.frameId = typeof args.frameId !== 'undefined' ? args.frameId : params.frameId;
  492. params.header = typeof args.header !== 'undefined' ? args.header : params.header;
  493. params.headerStyle = typeof args.headerStyle !== 'undefined' ? args.headerStyle : params.headerStyle;
  494. params.maxWidth = typeof args.maxWidth !== 'undefined' ? args.maxWidth : params.maxWidth;
  495. params.font = typeof args.font !== 'undefined' ? args.font : params.font;
  496. params.font_size = typeof args.font_size !== 'undefined' ? args.font_size : params.font_size;
  497. params.honorMarginPadding = typeof args.honorMarginPadding !== 'undefined' ? args.honorMarginPadding : params.honorMarginPadding;
  498. params.properties = typeof args.properties !== 'undefined' ? args.properties : params.properties;
  499. params.gridHeaderStyle = typeof args.gridHeaderStyle !== 'undefined' ? args.gridHeaderStyle : params.gridHeaderStyle;
  500. params.gridStyle = typeof args.gridStyle !== 'undefined' ? args.gridStyle : params.gridStyle;
  501. params.showModal = typeof args.showModal !== 'undefined' ? args.showModal : params.showModal;
  502. params.onLoadingStart = typeof args.onLoadingStart !== 'undefined' ? args.onLoadingStart : params.onLoadingStart;
  503. params.onLoadingEnd = typeof args.onLoadingEnd !== 'undefined' ? args.onLoadingEnd : params.onLoadingEnd;
  504. params.modalMessage = typeof args.modalMessage !== 'undefined' ? args.modalMessage : params.modalMessage;
  505. params.documentTitle = typeof args.documentTitle !== 'undefined' ? args.documentTitle : params.documentTitle;
  506. params.targetStyle = typeof args.targetStyle !== 'undefined' ? args.targetStyle : params.targetStyle;
  507. params.targetStyles = typeof args.targetStyles !== 'undefined' ? args.targetStyles : params.targetStyles;
  508. params.ignoreElements = typeof args.ignoreElements !== 'undefined' ? args.ignoreElements : params.ignoreElements;
  509. params.imageStyle = typeof args.imageStyle !== 'undefined' ? args.imageStyle : params.imageStyle;
  510. params.repeatTableHeader = typeof args.repeatTableHeader !== 'undefined' ? args.repeatTableHeader : params.repeatTableHeader;
  511. params.css = typeof args.css !== 'undefined' ? args.css : params.css;
  512. params.style = typeof args.style !== 'undefined' ? args.style : params.style;
  513. params.scanStyles = typeof args.scanStyles !== 'undefined' ? args.scanStyles : params.scanStyles;
  514. break;
  515. default:
  516. throw new Error('Unexpected argument type! Expected "string" or "object", got ' + (typeof args === 'undefined' ? 'undefined' : _typeof(args)));
  517. }
  518. // Validate printable
  519. if (!params.printable) throw new Error('Missing printable information.');
  520. // Validate type
  521. if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) {
  522. throw new Error('Invalid print type. Available types are: pdf, html, image and json.');
  523. }
  524. // Check if we are showing a feedback message to the user (useful for large files)
  525. if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].show(params);
  526. // Check for a print start hook function
  527. if (params.onLoadingStart) params.onLoadingStart();
  528. // To prevent duplication and issues, remove any used printFrame from the DOM
  529. var usedFrame = document.getElementById(params.frameId);
  530. if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
  531. // Create a new iframe or embed element (IE prints blank pdf's if we use iframe)
  532. var printFrame = void 0;
  533. // Create iframe element
  534. printFrame = document.createElement('iframe');
  535. // Hide iframe
  536. printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute;');
  537. // Set iframe element id
  538. printFrame.setAttribute('id', params.frameId);
  539. // For non pdf printing, pass an html document string to srcdoc (force onload callback)
  540. if (params.type !== 'pdf') {
  541. printFrame.srcdoc = '<html><head><title>' + params.documentTitle + '</title>';
  542. // Attach css files
  543. if (params.css !== null) {
  544. // Add support for single file
  545. if (!Array.isArray(params.css)) params.css = [params.css];
  546. // Create link tags for each css file
  547. params.css.forEach(function (file) {
  548. printFrame.srcdoc += '<link rel="stylesheet" href="' + file + '">';
  549. });
  550. }
  551. printFrame.srcdoc += '</head><body></body></html>';
  552. }
  553. // Check printable type
  554. switch (params.type) {
  555. case 'pdf':
  556. // Check browser support for pdf and if not supported we will just open the pdf file instead
  557. if (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isFirefox() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE()) {
  558. console.info('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.');
  559. var win = window.open(params.fallbackPrintable, '_blank');
  560. win.focus();
  561. // Make sure there is no loading modal opened
  562. if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
  563. if (params.onLoadingEnd) params.onLoadingEnd();
  564. } else {
  565. __WEBPACK_IMPORTED_MODULE_2__pdf__["a" /* default */].print(params, printFrame);
  566. }
  567. break;
  568. case 'image':
  569. __WEBPACK_IMPORTED_MODULE_4__image__["a" /* default */].print(params, printFrame);
  570. break;
  571. case 'html':
  572. __WEBPACK_IMPORTED_MODULE_3__html__["a" /* default */].print(params, printFrame);
  573. break;
  574. case 'json':
  575. __WEBPACK_IMPORTED_MODULE_5__json__["a" /* default */].print(params, printFrame);
  576. break;
  577. }
  578. }
  579. });
  580. /***/ }),
  581. /* 8 */
  582. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  583. "use strict";
  584. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
  585. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
  586. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  587. /* harmony default export */ __webpack_exports__["a"] = ({
  588. print: function print(params, printFrame) {
  589. // Check if we received proper data
  590. if (_typeof(params.printable) !== 'object') {
  591. throw new Error('Invalid javascript data object (JSON).');
  592. }
  593. // Check if the repeatTableHeader is boolean
  594. if (typeof params.repeatTableHeader !== 'boolean') {
  595. throw new Error('Invalid value for repeatTableHeader attribute (JSON).');
  596. }
  597. // Check if properties were provided
  598. if (!params.properties || !Array.isArray(params.properties)) throw new Error('Invalid properties array for your JSON data.');
  599. // Variable to hold the html string
  600. var htmlData = '';
  601. // Check if there is a header on top of the table
  602. if (params.header) htmlData += '<h1 style="' + params.headerStyle + '">' + params.header + '</h1>';
  603. // Build the printable html data
  604. htmlData += jsonToHTML(params);
  605. // Store the data
  606. params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(htmlData, params);
  607. // Print the json data
  608. __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
  609. }
  610. });
  611. function jsonToHTML(params) {
  612. // Get the row and column data
  613. var data = params.printable;
  614. var properties = params.properties;
  615. // Create a html table
  616. var htmlData = '<table style="border-collapse: collapse; width: 100%;">';
  617. // Check if the header should be repeated
  618. if (params.repeatTableHeader) {
  619. htmlData += '<thead>';
  620. }
  621. // Add the table header row
  622. htmlData += '<tr>';
  623. // Add the table header columns
  624. for (var a = 0; a < properties.length; a++) {
  625. htmlData += '<th style="width:' + properties.columnSize / properties.length + '%; ' + params.gridHeaderStyle + '">' + __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["b" /* capitalizePrint */])(properties[a].displayName) + '</th>';
  626. }
  627. // Add the closing tag for the table header row
  628. htmlData += '</tr>';
  629. // If the table header is marked as repeated, add the closing tag
  630. if (params.repeatTableHeader) {
  631. htmlData += '</thead>';
  632. }
  633. // Create the table body
  634. htmlData += '<tbody>';
  635. // Add the table data rows
  636. for (var i = 0; i < data.length; i++) {
  637. // Add the row starting tag
  638. htmlData += '<tr>';
  639. // Print selected properties only
  640. for (var n = 0; n < properties.length; n++) {
  641. var stringData = data[i];
  642. // Support nested objects
  643. var property = properties[n].field.split('.');
  644. if (property.length > 1) {
  645. for (var p = 0; p < property.length; p++) {
  646. stringData = stringData[property[p]];
  647. }
  648. } else {
  649. stringData = stringData[properties[n].field];
  650. }
  651. // Add the row contents and styles
  652. htmlData += '<td style="width:' + properties[n].columnSize / properties.length + '%;' + params.gridStyle + '">' + stringData + '</td>';
  653. }
  654. // Add the row closing tag
  655. htmlData += '</tr>';
  656. }
  657. // Add the table and body closing tags
  658. htmlData += '</tbody></table>';
  659. return htmlData;
  660. }
  661. /***/ }),
  662. /* 9 */
  663. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  664. "use strict";
  665. /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__print__ = __webpack_require__(0);
  666. /* harmony default export */ __webpack_exports__["a"] = ({
  667. print: function print(params, printFrame) {
  668. // Format pdf url
  669. params.printable = params.printable.indexOf('http') !== -1 ? params.printable : window.location.origin + (params.printable.charAt(0) !== '/' ? '/' + params.printable : params.printable);
  670. // If showing a loading modal or using a hook function, we will preload the pdf file
  671. if (params.showModal || params.onLoadingStart) {
  672. // Get the file through a http request
  673. var req = new window.XMLHttpRequest();
  674. req.responseType = 'arraybuffer';
  675. req.addEventListener('load', function () {
  676. // Pass response data to a blob and create a local object url
  677. var localPdf = new window.Blob([req.response], { type: 'application/pdf' });
  678. localPdf = window.URL.createObjectURL(localPdf);
  679. // Pass the url to the printable parameter (replacing the original pdf file url)
  680. // This will prevent a second request to the file (server) once the iframe loads
  681. params.printable = localPdf;
  682. send(params, printFrame);
  683. });
  684. req.open('GET', params.printable, true);
  685. req.send();
  686. } else {
  687. send(params, printFrame);
  688. }
  689. }
  690. });
  691. function send(params, printFrame) {
  692. // Set iframe src with pdf document url
  693. printFrame.setAttribute('src', params.printable);
  694. __WEBPACK_IMPORTED_MODULE_0__print__["a" /* default */].send(params, printFrame);
  695. }
  696. /***/ }),
  697. /* 10 */
  698. /***/ (function(module, exports, __webpack_require__) {
  699. module.exports = __webpack_require__(4);
  700. /***/ })
  701. /******/ ]);
  702. });