UNPKG

32.7 kBJavaScriptView Raw
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() {
11return /******/ (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
83"use strict";
84/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(2);
85/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
86
87
88
89var Print = {
90 send: function send(params, printFrame) {
91 // Append iframe element to document body
92 document.getElementsByTagName('body')[0].appendChild(printFrame);
93
94 // Get iframe element
95 var iframeElement = document.getElementById(params.frameId);
96
97 // Wait for iframe to load all content
98 if (params.type === 'pdf' && (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge())) {
99 iframeElement.setAttribute('onload', finishPrint(iframeElement, params));
100 } else {
101 printFrame.onload = function () {
102 if (params.type === 'pdf') {
103 finishPrint(iframeElement, params);
104 } else {
105 // Get iframe element document
106 var printDocument = iframeElement.contentWindow || iframeElement.contentDocument;
107 if (printDocument.document) printDocument = printDocument.document;
108
109 // Inject printable html into iframe body
110 printDocument.body.innerHTML = params.htmlData;
111
112 // Add custom style
113 if (params.type !== 'pdf' && params.style !== null) {
114 // Create style element
115 var style = document.createElement('style');
116 style.innerHTML = params.style;
117
118 // Append style element to iframe's head
119 printDocument.head.appendChild(style);
120 }
121
122 // If printing image, wait for it to load inside the iframe
123 if (params.type === 'image') {
124 loadIframeImages(printDocument, params).then(function () {
125 finishPrint(iframeElement, params);
126 });
127 } else {
128 finishPrint(iframeElement, params);
129 }
130 }
131 };
132 }
133 }
134};
135
136function finishPrint(iframeElement, params) {
137 iframeElement.focus();
138
139 // If Edge or IE, try catch with execCommand
140 if (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE()) {
141 try {
142 iframeElement.contentWindow.document.execCommand('print', false, null);
143 } catch (e) {
144 iframeElement.contentWindow.print();
145 }
146 } else {
147 // Other browsers
148 iframeElement.contentWindow.print();
149 }
150
151 // If we are showing a feedback message to user, remove it
152 if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
153
154 // Check for a finished loading hook function
155 if (params.onLoadingEnd) params.onLoadingEnd();
156
157 // If preloading pdf files, clean blob url
158 if (params.showModal || params.onLoadingStart) window.URL.revokeObjectURL(params.printable);
159}
160
161function loadIframeImages(printDocument, params) {
162 var promises = [];
163
164 params.printable.forEach(function (image, index) {
165 return promises.push(loadIframeImage(printDocument, index));
166 });
167
168 return Promise.all(promises);
169}
170
171function loadIframeImage(printDocument, index) {
172 return new Promise(function (resolve) {
173 var image = printDocument ? printDocument.getElementById('printableImage' + index) : null;
174
175 if (!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0) {
176 setTimeout(function () {
177 loadIframeImage(printDocument, index);
178 }, 500);
179 } else {
180 resolve();
181 }
182 });
183}
184
185/* harmony default export */ __webpack_exports__["a"] = (Print);
186
187/***/ }),
188/* 1 */
189/***/ (function(module, __webpack_exports__, __webpack_require__) {
190
191"use strict";
192/* harmony export (immutable) */ __webpack_exports__["a"] = addWrapper;
193/* harmony export (immutable) */ __webpack_exports__["b"] = capitalizePrint;
194/* harmony export (immutable) */ __webpack_exports__["c"] = collectStyles;
195/* harmony export (immutable) */ __webpack_exports__["d"] = loopNodesCollectStyles;
196/* harmony export (immutable) */ __webpack_exports__["e"] = addHeader;
197function addWrapper(htmlData, params) {
198 var bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;';
199 return '<div style="' + bodyStyle + '">' + htmlData + '</div>';
200}
201
202function capitalizePrint(string) {
203 return string.charAt(0).toUpperCase() + string.slice(1);
204}
205
206function collectStyles(element, params) {
207 var win = document.defaultView || window;
208
209 var styles = win.getComputedStyle;
210
211 // String variable to hold styling for each element
212 var elementStyle = '';
213
214 // Optional - include margin and padding
215 if (params.honorMarginPadding) params.targetStyles.push('margin', 'padding');
216
217 // Optional - include color
218 if (params.honorColor) params.targetStyles.push('color');
219
220 // Loop over computed styles
221 styles = win.getComputedStyle(element, '');
222
223 Object.keys(styles).map(function (key) {
224 // Check if style should be processed
225 if (params.targetStyles === ['*'] || params.targetStyle.indexOf(styles[key]) !== -1 || targetStylesMatch(params.targetStyles, styles[key])) {
226 elementStyle += styles[key] + ':' + styles.getPropertyValue(styles[key]) + ';';
227 }
228 });
229
230 // Print friendly defaults
231 elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;';
232
233 return elementStyle;
234}
235
236function targetStylesMatch(styles, value) {
237 for (var i = 0; i < styles.length; i++) {
238 if (value.indexOf(styles[i]) !== -1) return true;
239 }
240 return false;
241}
242
243function loopNodesCollectStyles(elements, params) {
244 for (var n = 0; n < elements.length; n++) {
245 var currentElement = elements[n];
246
247 // Check if we are skiping this element
248 if (params.ignoreElements.indexOf(currentElement.getAttribute('id')) !== -1) {
249 currentElement.parentNode.removeChild(currentElement);
250 continue;
251 }
252
253 // Form Printing - check if is element Input
254 var tag = currentElement.tagName;
255 if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
256 // Save style to variable
257 var textStyle = collectStyles(currentElement, params);
258
259 // Remove INPUT element and insert a text node
260 var parent = currentElement.parentNode;
261
262 // Get text value
263 var textNode = tag === 'SELECT' ? document.createTextNode(currentElement.options[currentElement.selectedIndex].text) : document.createTextNode(currentElement.value);
264
265 // Create text element
266 var textElement = document.createElement('div');
267 textElement.appendChild(textNode);
268
269 // Add style to text
270 textElement.setAttribute('style', textStyle);
271
272 // Add text
273 parent.appendChild(textElement);
274
275 // Remove input
276 parent.removeChild(currentElement);
277 } else {
278 // Get all styling for print element
279 currentElement.setAttribute('style', collectStyles(currentElement, params));
280 }
281
282 // Check if more elements in tree
283 var children = currentElement.children;
284
285 if (children && children.length) {
286 loopNodesCollectStyles(children, params);
287 }
288 }
289}
290
291function addHeader(printElement, header, headerStyle) {
292 // Create header element
293 var headerElement = document.createElement('h1');
294
295 // Create header text node
296 var headerNode = document.createTextNode(header);
297
298 // Build and style
299 headerElement.appendChild(headerNode);
300 headerElement.setAttribute('style', headerStyle);
301
302 printElement.insertBefore(headerElement, printElement.childNodes[0]);
303}
304
305/***/ }),
306/* 2 */
307/***/ (function(module, __webpack_exports__, __webpack_require__) {
308
309"use strict";
310var Browser = {
311 // Firefox 1.0+
312 isFirefox: function isFirefox() {
313 return typeof InstallTrigger !== 'undefined';
314 },
315 // Internet Explorer 6-11
316 isIE: function isIE() {
317 return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode;
318 },
319 // Edge 20+
320 isEdge: function isEdge() {
321 return !Browser.isIE() && !!window.StyleMedia;
322 },
323 // Chrome 1+
324 isChrome: function isChrome() {
325 return !!window.chrome && !!window.chrome.webstore;
326 },
327 // At least Safari 3+: "[object HTMLElementConstructor]"
328 isSafari: function isSafari() {
329 return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || navigator.userAgent.toLowerCase().indexOf('safari') !== -1;
330 }
331};
332
333/* harmony default export */ __webpack_exports__["a"] = (Browser);
334
335/***/ }),
336/* 3 */
337/***/ (function(module, __webpack_exports__, __webpack_require__) {
338
339"use strict";
340var Modal = {
341 show: function show(params) {
342 // Build modal
343 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;';
344
345 // Create wrapper
346 var printModal = document.createElement('div');
347 printModal.setAttribute('style', modalStyle);
348 printModal.setAttribute('id', 'printJS-Modal');
349
350 // Create content div
351 var contentDiv = document.createElement('div');
352 contentDiv.setAttribute('style', 'display:table-cell; vertical-align:middle; padding-bottom:100px;');
353
354 // Add close button (requires print.css)
355 var closeButton = document.createElement('div');
356 closeButton.setAttribute('class', 'printClose');
357 closeButton.setAttribute('id', 'printClose');
358 contentDiv.appendChild(closeButton);
359
360 // Add spinner (requires print.css)
361 var spinner = document.createElement('span');
362 spinner.setAttribute('class', 'printSpinner');
363 contentDiv.appendChild(spinner);
364
365 // Add message
366 var messageNode = document.createTextNode(params.modalMessage);
367 contentDiv.appendChild(messageNode);
368
369 // Add contentDiv to printModal
370 printModal.appendChild(contentDiv);
371
372 // Append print modal element to document body
373 document.getElementsByTagName('body')[0].appendChild(printModal);
374
375 // Add event listener to close button
376 document.getElementById('printClose').addEventListener('click', function () {
377 Modal.close();
378 });
379 },
380 close: function close() {
381 var printFrame = document.getElementById('printJS-Modal');
382
383 printFrame.parentNode.removeChild(printFrame);
384 }
385};
386
387/* harmony default export */ __webpack_exports__["a"] = (Modal);
388
389/***/ }),
390/* 4 */
391/***/ (function(module, __webpack_exports__, __webpack_require__) {
392
393"use strict";
394Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
395/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__js_init__ = __webpack_require__(7);
396
397
398var printjs = __WEBPACK_IMPORTED_MODULE_0__js_init__["a" /* default */].init;
399
400if (typeof window !== 'undefined') {
401 window.printJS = printjs;
402}
403
404/* harmony default export */ __webpack_exports__["default"] = (printjs);
405
406/***/ }),
407/* 5 */
408/***/ (function(module, __webpack_exports__, __webpack_require__) {
409
410"use strict";
411/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
412/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
413
414
415
416/* harmony default export */ __webpack_exports__["a"] = ({
417 print: function print(params, printFrame) {
418 // Get HTML printable element
419 var printElement = document.getElementById(params.printable);
420
421 // Check if element exists
422 if (!printElement) {
423 window.console.error('Invalid HTML element id: ' + params.printable);
424
425 return false;
426 }
427
428 // Make a copy of the printElement to prevent DOM changes
429 var printableElement = document.createElement('div');
430 printableElement.appendChild(printElement.cloneNode(true));
431
432 // Add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles
433 printableElement.setAttribute('style', 'height:0; overflow:hidden;');
434 printableElement.setAttribute('id', 'printJS-html');
435 printElement.parentNode.appendChild(printableElement);
436
437 // Update printableElement variable with newly created DOM element
438 printableElement = document.getElementById('printJS-html');
439
440 // Get main element styling
441 if (params.scanStyles === true) {
442 printableElement.setAttribute('style', __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["c" /* collectStyles */])(printableElement, params) + 'margin:0 !important;');
443 }
444
445 // Get all children elements
446 var elements = printableElement.children;
447
448 // Get styles for all children elements
449 if (params.scanStyles === true) {
450 __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["d" /* loopNodesCollectStyles */])(elements, params);
451 }
452
453 // Add header if any
454 if (params.header) {
455 __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement, params.header, params.headerStyle);
456 }
457
458 // Remove DOM printableElement
459 printableElement.parentNode.removeChild(printableElement);
460
461 // Store html data
462 params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(printableElement.innerHTML, params);
463
464 // Print html element contents
465 __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
466 }
467});
468
469/***/ }),
470/* 6 */
471/***/ (function(module, __webpack_exports__, __webpack_require__) {
472
473"use strict";
474/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
475/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
476
477
478
479/* harmony default export */ __webpack_exports__["a"] = ({
480 print: function print(params, printFrame) {
481 // Check if we are printing one image or multiple images
482 if (params.printable.constructor !== Array) {
483 // Create array with one image
484 params.printable = [params.printable];
485 }
486
487 // Create printable element (container)
488 var printableElement = document.createElement('div');
489 printableElement.setAttribute('style', 'width:100%');
490
491 // Load images and append
492 loadImagesAndAppendToPrintableElement(printableElement, params).then(function () {
493 // Check if we are adding a header
494 if (params.header) __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement, params.header, params.headerStyle);
495
496 // Store html data
497 params.htmlData = printableElement.outerHTML;
498
499 // Print image
500 __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
501 });
502 }
503});
504
505function loadImagesAndAppendToPrintableElement(printableElement, params) {
506 var promises = [];
507
508 params.printable.forEach(function (image, index) {
509 // Create the image element
510 var img = document.createElement('img');
511
512 // Set image src with image file url
513 img.src = image;
514
515 // Load image
516 promises.push(loadImageAndAppendToPrintableElement(printableElement, params, img, index));
517 });
518
519 return Promise.all(promises);
520}
521
522function loadImageAndAppendToPrintableElement(printableElement, params, img, index) {
523 return new Promise(function (resolve) {
524 img.onload = function () {
525 // Create image wrapper
526 var imageWrapper = document.createElement('div');
527 imageWrapper.setAttribute('style', params.imageStyle);
528
529 img.setAttribute('style', 'width:100%;');
530 img.setAttribute('id', 'printableImage' + index);
531
532 // Append image to wrapper element
533 imageWrapper.appendChild(img);
534
535 // Append wrapper element to printable element
536 printableElement.appendChild(imageWrapper);
537
538 resolve();
539 };
540 });
541}
542
543/***/ }),
544/* 7 */
545/***/ (function(module, __webpack_exports__, __webpack_require__) {
546
547"use strict";
548/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(2);
549/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
550/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__pdf__ = __webpack_require__(9);
551/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__html__ = __webpack_require__(5);
552/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4__image__ = __webpack_require__(6);
553/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5__json__ = __webpack_require__(8);
554
555
556var _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; };
557
558
559
560
561
562
563
564
565var printTypes = ['pdf', 'html', 'image', 'json'];
566
567/* harmony default export */ __webpack_exports__["a"] = ({
568 init: function init() {
569 var params = {
570 printable: null,
571 fallbackPrintable: null,
572 type: 'pdf',
573 header: null,
574 headerStyle: 'font-weight: 300;',
575 maxWidth: 800,
576 font: 'TimesNewRoman',
577 font_size: '12pt',
578 honorMarginPadding: true,
579 honorColor: false,
580 properties: null,
581 gridHeaderStyle: 'font-weight: bold; padding: 5px; border: 1px solid #dddddd;',
582 gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;',
583 showModal: false,
584 onLoadingStart: null,
585 onLoadingEnd: null,
586 modalMessage: 'Retrieving Document...',
587 frameId: 'printJS',
588 htmlData: '',
589 documentTitle: 'Document',
590 targetStyle: ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height'],
591 targetStyles: ['border', 'box', 'break', 'text-decoration'],
592 ignoreElements: [],
593 imageStyle: 'width:100%;',
594 repeatTableHeader: true,
595 css: null,
596 style: null,
597 scanStyles: true
598 };
599
600 // Check if a printable document or object was supplied
601 var args = arguments[0];
602 if (args === undefined) throw new Error('printJS expects at least 1 attribute.');
603
604 // Process parameters
605 switch (typeof args === 'undefined' ? 'undefined' : _typeof(args)) {
606 case 'string':
607 params.printable = encodeURI(args);
608 params.fallbackPrintable = params.printable;
609 params.type = arguments[1] || params.type;
610 break;
611 case 'object':
612 params.printable = args.printable;
613 params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable;
614 params.type = typeof args.type !== 'undefined' ? args.type : params.type;
615 params.frameId = typeof args.frameId !== 'undefined' ? args.frameId : params.frameId;
616 params.header = typeof args.header !== 'undefined' ? args.header : params.header;
617 params.headerStyle = typeof args.headerStyle !== 'undefined' ? args.headerStyle : params.headerStyle;
618 params.maxWidth = typeof args.maxWidth !== 'undefined' ? args.maxWidth : params.maxWidth;
619 params.font = typeof args.font !== 'undefined' ? args.font : params.font;
620 params.font_size = typeof args.font_size !== 'undefined' ? args.font_size : params.font_size;
621 params.honorMarginPadding = typeof args.honorMarginPadding !== 'undefined' ? args.honorMarginPadding : params.honorMarginPadding;
622 params.properties = typeof args.properties !== 'undefined' ? args.properties : params.properties;
623 params.gridHeaderStyle = typeof args.gridHeaderStyle !== 'undefined' ? args.gridHeaderStyle : params.gridHeaderStyle;
624 params.gridStyle = typeof args.gridStyle !== 'undefined' ? args.gridStyle : params.gridStyle;
625 params.showModal = typeof args.showModal !== 'undefined' ? args.showModal : params.showModal;
626 params.onLoadingStart = typeof args.onLoadingStart !== 'undefined' ? args.onLoadingStart : params.onLoadingStart;
627 params.onLoadingEnd = typeof args.onLoadingEnd !== 'undefined' ? args.onLoadingEnd : params.onLoadingEnd;
628 params.modalMessage = typeof args.modalMessage !== 'undefined' ? args.modalMessage : params.modalMessage;
629 params.documentTitle = typeof args.documentTitle !== 'undefined' ? args.documentTitle : params.documentTitle;
630 params.targetStyle = typeof args.targetStyle !== 'undefined' ? args.targetStyle : params.targetStyle;
631 params.targetStyles = typeof args.targetStyles !== 'undefined' ? args.targetStyles : params.targetStyles;
632 params.ignoreElements = typeof args.ignoreElements !== 'undefined' ? args.ignoreElements : params.ignoreElements;
633 params.imageStyle = typeof args.imageStyle !== 'undefined' ? args.imageStyle : params.imageStyle;
634 params.repeatTableHeader = typeof args.repeatTableHeader !== 'undefined' ? args.repeatTableHeader : params.repeatTableHeader;
635 params.css = typeof args.css !== 'undefined' ? args.css : params.css;
636 params.style = typeof args.style !== 'undefined' ? args.style : params.style;
637 params.scanStyles = typeof args.scanStyles !== 'undefined' ? args.scanStyles : params.scanStyles;
638 break;
639 default:
640 throw new Error('Unexpected argument type! Expected "string" or "object", got ' + (typeof args === 'undefined' ? 'undefined' : _typeof(args)));
641 }
642
643 // Validate printable
644 if (!params.printable) throw new Error('Missing printable information.');
645
646 // Validate type
647 if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) {
648 throw new Error('Invalid print type. Available types are: pdf, html, image and json.');
649 }
650
651 // Check if we are showing a feedback message to the user (useful for large files)
652 if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].show(params);
653
654 // Check for a print start hook function
655 if (params.onLoadingStart) params.onLoadingStart();
656
657 // To prevent duplication and issues, remove any used printFrame from the DOM
658 var usedFrame = document.getElementById(params.frameId);
659
660 if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
661
662 // Create a new iframe or embed element (IE prints blank pdf's if we use iframe)
663 var printFrame = void 0;
664
665 // Create iframe element
666 printFrame = document.createElement('iframe');
667
668 // Hide iframe
669 printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute;');
670
671 // Set iframe element id
672 printFrame.setAttribute('id', params.frameId);
673
674 // For non pdf printing, pass an html document string to srcdoc (force onload callback)
675 if (params.type !== 'pdf') {
676 printFrame.srcdoc = '<html><head><title>' + params.documentTitle + '</title>';
677
678 // Attach css files
679 if (params.css !== null) {
680 // Add support for single file
681 if (!Array.isArray(params.css)) params.css = [params.css];
682
683 // Create link tags for each css file
684 params.css.forEach(function (file) {
685 printFrame.srcdoc += '<link rel="stylesheet" href="' + file + '">';
686 });
687 }
688
689 printFrame.srcdoc += '</head><body></body></html>';
690 }
691
692 // Check printable type
693 switch (params.type) {
694 case 'pdf':
695 // Check browser support for pdf and if not supported we will just open the pdf file instead
696 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()) {
697 console.info('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.');
698 var win = window.open(params.fallbackPrintable, '_blank');
699 win.focus();
700 // Make sure there is no loading modal opened
701 if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
702 if (params.onLoadingEnd) params.onLoadingEnd();
703 } else {
704 __WEBPACK_IMPORTED_MODULE_2__pdf__["a" /* default */].print(params, printFrame);
705 }
706 break;
707 case 'image':
708 __WEBPACK_IMPORTED_MODULE_4__image__["a" /* default */].print(params, printFrame);
709 break;
710 case 'html':
711 __WEBPACK_IMPORTED_MODULE_3__html__["a" /* default */].print(params, printFrame);
712 break;
713 case 'json':
714 __WEBPACK_IMPORTED_MODULE_5__json__["a" /* default */].print(params, printFrame);
715 break;
716 }
717 }
718});
719
720/***/ }),
721/* 8 */
722/***/ (function(module, __webpack_exports__, __webpack_require__) {
723
724"use strict";
725/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(1);
726/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
727var _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; };
728
729
730
731
732/* harmony default export */ __webpack_exports__["a"] = ({
733 print: function print(params, printFrame) {
734 // Check if we received proper data
735 if (_typeof(params.printable) !== 'object') {
736 throw new Error('Invalid javascript data object (JSON).');
737 }
738
739 // Check if the repeatTableHeader is boolean
740 if (typeof params.repeatTableHeader !== 'boolean') {
741 throw new Error('Invalid value for repeatTableHeader attribute (JSON).');
742 }
743
744 // Check if properties were provided
745 if (!params.properties || _typeof(params.properties) !== 'object') throw new Error('Invalid properties array for your JSON data.');
746
747 // Variable to hold the html string
748 var htmlData = '';
749
750 // Check if there is a header on top of the table
751 if (params.header) htmlData += '<h1 style="' + params.headerStyle + '">' + params.header + '</h1>';
752
753 // Build the printable html data
754 htmlData += jsonToHTML(params);
755
756 // Store the data
757 params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(htmlData, params);
758
759 // Print the json data
760 __WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
761 }
762});
763
764function jsonToHTML(params) {
765 // Get the row and column data
766 var data = params.printable;
767 var properties = params.properties;
768
769 // Create a html table
770 var htmlData = '<table style="border-collapse: collapse; width: 100%;">';
771
772 // Check if the header should be repeated
773 if (params.repeatTableHeader) {
774 htmlData += '<thead>';
775 }
776
777 // Add the table header row
778 htmlData += '<tr>';
779
780 // Add the table header columns
781 for (var a = 0; a < properties.length; a++) {
782 htmlData += '<th style="width:' + 100 / properties.length + '%; ' + params.gridHeaderStyle + '">' + __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["b" /* capitalizePrint */])(properties[a]) + '</th>';
783 }
784
785 // Add the closing tag for the table header row
786 htmlData += '</tr>';
787
788 // If the table header is marked as repeated, add the closing tag
789 if (params.repeatTableHeader) {
790 htmlData += '</thead>';
791 }
792
793 // Create the table body
794 htmlData += '<tbody>';
795
796 // Add the table data rows
797 for (var i = 0; i < data.length; i++) {
798 // Add the row starting tag
799 htmlData += '<tr>';
800
801 // Print selected properties only
802 for (var n = 0; n < properties.length; n++) {
803 var stringData = data[i];
804
805 // Support nested objects
806 var property = properties[n].split('.');
807 if (property.length > 1) {
808 for (var p = 0; p < property.length; p++) {
809 stringData = stringData[property[p]];
810 }
811 } else {
812 stringData = stringData[properties[n]];
813 }
814
815 // Add the row contents and styles
816 htmlData += '<td style="width:' + 100 / properties.length + '%;' + params.gridStyle + '">' + stringData + '</td>';
817 }
818
819 // Add the row closing tag
820 htmlData += '</tr>';
821 }
822
823 // Add the table and body closing tags
824 htmlData += '</tbody></table>';
825
826 return htmlData;
827}
828
829/***/ }),
830/* 9 */
831/***/ (function(module, __webpack_exports__, __webpack_require__) {
832
833"use strict";
834/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__print__ = __webpack_require__(0);
835
836
837/* harmony default export */ __webpack_exports__["a"] = ({
838 print: function print(params, printFrame) {
839 // Format pdf url
840 params.printable = params.printable.indexOf('http') !== -1 ? params.printable : window.location.origin + (params.printable.charAt(0) !== '/' ? '/' + params.printable : params.printable);
841
842 // If showing a loading modal or using a hook function, we will preload the pdf file
843 if (params.showModal || params.onLoadingStart) {
844 // Get the file through a http request
845 var req = new window.XMLHttpRequest();
846 req.responseType = 'arraybuffer';
847
848 req.addEventListener('load', function () {
849 // Pass response data to a blob and create a local object url
850 var localPdf = new window.Blob([req.response], { type: 'application/pdf' });
851 localPdf = window.URL.createObjectURL(localPdf);
852
853 // Pass the url to the printable parameter (replacing the original pdf file url)
854 // This will prevent a second request to the file (server) once the iframe loads
855 params.printable = localPdf;
856
857 send(params, printFrame);
858 });
859
860 req.open('GET', params.printable, true);
861 req.send();
862 } else {
863 send(params, printFrame);
864 }
865 }
866});
867
868function send(params, printFrame) {
869 // Set iframe src with pdf document url
870 printFrame.setAttribute('src', params.printable);
871
872 __WEBPACK_IMPORTED_MODULE_0__print__["a" /* default */].send(params, printFrame);
873}
874
875/***/ }),
876/* 10 */
877/***/ (function(module, exports, __webpack_require__) {
878
879module.exports = __webpack_require__(4);
880
881
882/***/ })
883/******/ ]);
884});
\No newline at end of file