Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

print-js

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

print-js - npm Package Compare versions

Comparing version 1.0.19 to 1.0.20

2

bower.json
{
"name": "print.js",
"name": "print-js",
"description": "A tiny javascript library to help printing from the web.",

@@ -4,0 +4,0 @@ "main": "src/print.js",

@@ -1,1 +0,739 @@

!function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={i:i,l:!1,exports:{}};return e[i].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,i){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:i})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="./",t(t.s=10)}([function(e,t,n){"use strict";function i(e,t){if(e.focus(),a.a.isIE()||a.a.isEdge())try{e.contentWindow.document.execCommand("print",!1,null)}catch(t){e.contentWindow.print()}else e.contentWindow.print();t.showModal&&d.a.close()}function o(e){void 0===e.print?setTimeout(function(){o()},1e3):(l.send(),setTimeout(function(){e.parentNode.removeChild(e)},2e3))}function r(e,t,n){void 0===e.naturalWidth||0===e.naturalWidth?setTimeout(function(){r(e,t,n)},500):i(t,n)}var a=n(1),d=n(3),l={send:function(e,t){document.getElementsByTagName("body")[0].appendChild(t);var n=document.getElementById(e.frameId);a.a.isIE()&&"pdf"===e.type?o(n):t.onload=function(){if("pdf"===e.type)i(n,e);else{var t=n.contentWindow||n.contentDocument;t.document&&(t=t.document),t.body.innerHTML=e.htmlData,"image"===e.type?r(t.getElementById("printableImage"),n,e):i(n,e)}}}};t.a=l},function(e,t,n){"use strict";var i={isFirefox:function(){return"undefined"!=typeof InstallTrigger},isIE:function(){return!!document.documentMode},isEdge:function(){return!i.isIE()&&!!window.StyleMedia},isChrome:function(){return!!window.chrome&&!!window.chrome.webstore},isSafari:function(){return Object.prototype.toString.call(window.HTMLElement).indexOf("Constructor")>0||-1!==navigator.userAgent.toLowerCase().indexOf("safari")}};t.a=i},function(e,t,n){"use strict";function i(e,t){return'<div style="font-family:'+t.font+" !important; font-size: "+t.font_size+' !important; width:100%;">'+e+"</div>"}function o(e){return e.charAt(0).toUpperCase()+e.slice(1)}function r(e,t){var n=document.defaultView||window,i=[],o="";if(n.getComputedStyle){i=n.getComputedStyle(e,"");var r=["border","float","box","break","text-decoration"],a=["clear","display","width","min-width","height","min-height","max-height"];t.honorMarginPadding&&r.push("margin","padding"),t.honorColor&&r.push("color");for(var d=0;d<i.length;d++)for(var l=0;l<a.length;l++)-1===i[d].indexOf(r[l])&&0!==i[d].indexOf(a[l])||(o+=i[d]+":"+i.getPropertyValue(i[d])+";")}else if(e.currentStyle){i=e.currentStyle;for(var s in i)-1!==i.indexOf("border")&&-1!==i.indexOf("color")&&(o+=s+":"+i[s]+";")}return o+="max-width: "+t.maxWidth+"px !important;"+t.font_size+" !important;"}function a(e,t){for(var n=0;n<e.length;n++){var i=e[n],o=i.tagName;if("INPUT"===o||"TEXTAREA"===o||"SELECT"===o){var d=r(i,t),l=i.parentNode,s="SELECT"===o?document.createTextNode(i.options[i.selectedIndex].text):document.createTextNode(i.value),c=document.createElement("div");c.appendChild(s),c.setAttribute("style",d),l.appendChild(c),l.removeChild(i)}else i.setAttribute("style",r(i,t));var p=i.children;p&&p.length&&a(p,t)}}function d(e,t){var n=document.createElement("h1"),i=document.createTextNode(t);n.appendChild(i),n.setAttribute("style","font-weight:300;"),e.insertBefore(n,e.childNodes[0])}t.a=i,t.b=o,t.c=r,t.d=a,t.e=d},function(e,t,n){"use strict";var i={show:function(e){var t=document.createElement("div");t.setAttribute("style","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;"),t.setAttribute("id","printJS-Modal");var n=document.createElement("div");n.setAttribute("style","display:table-cell; vertical-align:middle; padding-bottom:100px;");var o=document.createElement("div");o.setAttribute("class","printClose"),o.setAttribute("id","printClose"),n.appendChild(o);var r=document.createElement("span");r.setAttribute("class","printSpinner"),n.appendChild(r);var a=document.createTextNode(e.modalMessage);n.appendChild(a),t.appendChild(n),document.getElementsByTagName("body")[0].appendChild(t),document.getElementById("printClose").addEventListener("click",function(){i.close()})},close:function(){var e=document.getElementById("printJS-Modal");e.parentNode.removeChild(e)}};t.a=i},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=n(7),o=i.a.init;"undefined"!=typeof window&&(window.printJS=o),t.default=o},function(e,t,n){"use strict";var i=n(2),o=n(0);t.a={print:function(e,t){var r=document.getElementById(e.printable);if(!r)return window.console.error("Invalid HTML element id: "+e.printable),!1;var a=document.createElement("div");a.appendChild(r.cloneNode(!0)),a.setAttribute("style","display:none;"),a.setAttribute("id","printJS-html"),r.parentNode.appendChild(a),a=document.getElementById("printJS-html"),a.setAttribute("style",n.i(i.c)(a,e)+"margin:0 !important;");var d=a.children;n.i(i.d)(d,e),e.header&&n.i(i.e)(a,e.header),a.parentNode.removeChild(a),e.htmlData=n.i(i.a)(a.innerHTML,e),o.a.send(e,t)}}},function(e,t,n){"use strict";var i=n(2),o=n(0);t.a={print:function(e,t){var r=document.createElement("img");r.src=e.printable,r.onload=function(){r.setAttribute("style","width:100%;"),r.setAttribute("id","printableImage");var a=document.createElement("div");a.setAttribute("style","width:100%"),a.appendChild(r),e.header&&n.i(i.e)(a),e.htmlData=a.outerHTML,o.a.send(e,t)}}}},function(e,t,n){"use strict";var i=n(1),o=n(3),r=n(9),a=n(5),d=n(6),l=n(8),s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},c=["pdf","html","image","json"];t.a={init:function(){var e={printable:null,type:"pdf",header:null,maxWidth:800,font:"TimesNewRoman",font_size:"12pt",honorMarginPadding:!0,honorColor:!1,properties:null,showModal:!1,modalMessage:"Retrieving Document...",frameId:"printJS",border:!0,htmlData:""},t=arguments[0];if(void 0===t)throw new Error("printJS expects at least 1 attribute.");switch(void 0===t?"undefined":s(t)){case"string":e.printable=encodeURI(t),e.type=arguments[1]||e.type;break;case"object":e.printable=t.printable,e.type=void 0!==t.type?t.type:e.type,e.frameId=void 0!==t.frameId?t.frameId:e.frameId,e.header=void 0!==t.header?t.header:e.header,e.maxWidth=void 0!==t.maxWidth?t.maxWidth:e.maxWidth,e.font=void 0!==t.font?t.font:e.font,e.font_size=void 0!==t.font_size?t.font_size:e.font_size,e.honorMarginPadding=void 0!==t.honorMarginPadding?t.honorMarginPadding:e.honorMarginPadding,e.properties=void 0!==t.properties?t.properties:e.properties,e.showModal=void 0!==t.showModal?t.showModal:e.showModal,e.modalMessage=void 0!==t.modalMessage?t.modalMessage:e.modalMessage;break;default:throw new Error('Unexpected argument type! Expected "string" or "object", got '+(void 0===t?"undefined":s(t)))}if(!e.printable)throw new Error("Missing printable information.");if(!e.type||"string"!=typeof e.type||-1===c.indexOf(e.type.toLowerCase()))throw new Error("Invalid print type. Available types are: pdf, html, image and json.");e.showModal&&o.a.show(e);var n=document.getElementById(e.frameId);n&&n.parentNode.removeChild(n);var p=void 0;switch(i.a.isIE()&&"pdf"===e.type?(p=document.createElement("embed"),p.setAttribute("type","application/pdf"),p.setAttribute("style","width:0px;height:0px;")):(p=document.createElement("iframe"),p.setAttribute("style","display:none;")),p.setAttribute("id",e.frameId),"pdf"!==e.type&&(i.a.isChrome()||i.a.isSafari())&&(p.srcdoc="<html><head></head><body></body></html>"),e.type){case"pdf":if(i.a.isFirefox()||i.a.isIE()||i.a.isEdge()){window.open(e.printable,"_blank").focus(),e.showModal&&o.a.close()}else r.a.print(e,p);break;case"image":d.a.print(e,p);break;case"html":a.a.print(e,p);break;case"json":l.a.print(e,p);break;default:throw new Error("Invalid print type. Available types are: pdf, html, image and json.")}}}},function(e,t,n){"use strict";function i(e){var t=e.printable,i=e.properties,r='<div style="display:flex; flex-direction: column;">';r+='<div style="flex:1; display:flex;">';for(var a=0;a<i.length;a++)r+='<div style="flex:1; padding:5px;">'+n.i(o.b)(i[a].displayName||i[a])+"</div>";r+="</div>";for(var d=0;d<t.length;d++){r+='<div style="flex:1; display:flex;',r+=e.border?"border:1px solid lightgray;":"",r+='">';for(var l=0;l<i.length;l++)r+='<div style="flex:1; padding:5px;">'+t[d][i[l].field||i[l]]+"</div>";r+="</div>"}return r+="</div>"}var o=n(2),r=n(0),a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.a={print:function(e,t){if("object"!==a(e.printable))throw new Error("Invalid javascript data object (JSON).");if(!e.properties||"object"!==a(e.properties))throw new Error("Invalid properties array for your JSON data.");var d="";e.header&&(d+='<h1 style="font-weight:300;">'+e.header+"</h1>"),d+=i(e),e.htmlData=n.i(o.a)(d,e),r.a.send(e,t)}}},function(e,t,n){"use strict";function i(e,t){t.setAttribute("src",e.printable),r.a.send(e,t)}var o=n(1),r=n(0);t.a={print:function(e,t){if(e.showModal&&!o.a.isIE()){var n=document.createElement("img");n.src=e.printable;new Promise(function(e,t){function i(){n.complete&&(window.clearInterval(o),e("PrintJS: PDF loaded."))}var o=setInterval(i,100)}).then(function(n){i(e,t)})}else i(e,t)}}},function(e,t,n){e.exports=n(4)}]);
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "./";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 10);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(1);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
var Print = {
send: function send(params, printFrame) {
// Append iframe element to document body
document.getElementsByTagName('body')[0].appendChild(printFrame);
// Get iframe element
var iframeElement = document.getElementById(params.frameId);
// Wait for iframe to load all content
if (params.type === 'pdf' && (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge())) {
iframeElement.setAttribute('onload', finishPrint(iframeElement, params));
} else {
printFrame.onload = function () {
if (params.type === 'pdf') {
finishPrint(iframeElement, params);
} else {
// Get iframe element document
var printDocument = iframeElement.contentWindow || iframeElement.contentDocument;
if (printDocument.document) printDocument = printDocument.document;
// Inject printable html into iframe body
printDocument.body.innerHTML = params.htmlData;
// If printing image, wait for it to load inside the iframe (skip firefox)
if (params.type === 'image') {
loadImageAndFinishPrint(printDocument.getElementById('printableImage'), iframeElement, params);
} else {
finishPrint(iframeElement, params);
}
}
};
}
}
};
function finishPrint(iframeElement, params) {
iframeElement.focus();
// If Edge or IE, try catch with execCommand
if (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE()) {
try {
iframeElement.contentWindow.document.execCommand('print', false, null);
} catch (e) {
iframeElement.contentWindow.print();
}
}
// Other browsers
if (!__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE() && !__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isEdge()) {
iframeElement.contentWindow.print();
}
// Remove embed on IE (just because it isn't 100% hidden when using h/w = 0)
if (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE() && params.type === 'pdf') {
setTimeout(function () {
iframeElement.parentNode.removeChild(iframeElement);
}, 2000);
}
// If we are showing a feedback message to user, remove it
if (params.showModal) {
__WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
}
}
function loadImageAndFinishPrint(img, iframeElement, params) {
if (typeof img.naturalWidth === 'undefined' || img.naturalWidth === 0) {
setTimeout(function () {
loadImageAndFinishPrint(img, iframeElement, params);
}, 500);
} else {
finishPrint(iframeElement, params);
}
}
/* harmony default export */ __webpack_exports__["a"] = (Print);
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
var Browser = {
// Firefox 1.0+
isFirefox: function isFirefox() {
return typeof InstallTrigger !== 'undefined';
},
// Internet Explorer 6-11
isIE: function isIE() {
return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode;
},
// Edge 20+
isEdge: function isEdge() {
return !Browser.isIE() && !!window.StyleMedia;
},
// Chrome 1+
isChrome: function isChrome() {
return !!window.chrome && !!window.chrome.webstore;
},
// At least Safari 3+: "[object HTMLElementConstructor]"
isSafari: function isSafari() {
return Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || navigator.userAgent.toLowerCase().indexOf('safari') !== -1;
}
};
/* harmony default export */ __webpack_exports__["a"] = (Browser);
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = addWrapper;
/* harmony export (immutable) */ __webpack_exports__["b"] = capitalizePrint;
/* harmony export (immutable) */ __webpack_exports__["c"] = collectStyles;
/* harmony export (immutable) */ __webpack_exports__["d"] = loopNodesCollectStyles;
/* harmony export (immutable) */ __webpack_exports__["e"] = addHeader;
function addWrapper(htmlData, params) {
var bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;';
return '<div style="' + bodyStyle + '">' + htmlData + '</div>';
}
function capitalizePrint(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function collectStyles(element, params) {
var win = document.defaultView || window;
var style = [];
// String variable to hold styling for each element
var elementStyle = '';
if (win.getComputedStyle) {
// modern browsers
style = win.getComputedStyle(element, '');
// Styles including
var targetStyles = ['border', 'float', 'box', 'break', 'text-decoration'];
// Exact match
var targetStyle = ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height'];
// Optional - include margin and padding
if (params.honorMarginPadding) {
targetStyles.push('margin', 'padding');
}
// Optional - include color
if (params.honorColor) {
targetStyles.push('color');
}
elementStyle += style[i] + ':' + style.getPropertyValue(style[i]) + ';';
// for (let i = 0; i < style.length; i++) {
// for (let s = 0; s < targetStyle.length; s++) {
// if (style[i].indexOf(targetStyles[s]) !== -1 || style[i].indexOf(targetStyle[s]) === 0) {
// elementStyle += style[i] + ':' + style.getPropertyValue(style[i]) + ';'
// }
// }
// }
} else if (element.currentStyle) {
// IE
style = element.currentStyle;
for (var name in style) {
if (style.indexOf('border') !== -1 && style.indexOf('color') !== -1) {
elementStyle += name + ':' + style[name] + ';';
}
}
}
// Print friendly defaults
elementStyle += 'max-width: ' + params.maxWidth + 'px !important;' + params.font_size + ' !important;';
return elementStyle;
}
function loopNodesCollectStyles(elements, params) {
for (var n = 0; n < elements.length; n++) {
var currentElement = elements[n];
// Form Printing - check if is element Input
var tag = currentElement.tagName;
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') {
// Save style to variable
var textStyle = collectStyles(currentElement, params);
// Remove INPUT element and insert a text node
var parent = currentElement.parentNode;
// Get text value
var textNode = tag === 'SELECT' ? document.createTextNode(currentElement.options[currentElement.selectedIndex].text) : document.createTextNode(currentElement.value);
// Create text element
var textElement = document.createElement('div');
textElement.appendChild(textNode);
// Add style to text
textElement.setAttribute('style', textStyle);
// Add text
parent.appendChild(textElement);
// Remove input
parent.removeChild(currentElement);
} else {
// Get all styling for print element
currentElement.setAttribute('style', collectStyles(currentElement, params));
}
// Check if more elements in tree
var children = currentElement.children;
if (children && children.length) {
loopNodesCollectStyles(children, params);
}
}
}
function addHeader(printElement, header) {
// Create header element
var headerElement = document.createElement('h1');
// Create header text node
var headerNode = document.createTextNode(header);
// Build and style
headerElement.appendChild(headerNode);
headerElement.setAttribute('style', 'font-weight:300;');
printElement.insertBefore(headerElement, printElement.childNodes[0]);
}
/***/ }),
/* 3 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
var Modal = {
show: function show(params) {
// Build modal
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;';
// Create wrapper
var printModal = document.createElement('div');
printModal.setAttribute('style', modalStyle);
printModal.setAttribute('id', 'printJS-Modal');
// Create content div
var contentDiv = document.createElement('div');
contentDiv.setAttribute('style', 'display:table-cell; vertical-align:middle; padding-bottom:100px;');
// Add close button (requires print.css)
var closeButton = document.createElement('div');
closeButton.setAttribute('class', 'printClose');
closeButton.setAttribute('id', 'printClose');
contentDiv.appendChild(closeButton);
// Add spinner (requires print.css)
var spinner = document.createElement('span');
spinner.setAttribute('class', 'printSpinner');
contentDiv.appendChild(spinner);
// Add message
var messageNode = document.createTextNode(params.modalMessage);
contentDiv.appendChild(messageNode);
// Add contentDiv to printModal
printModal.appendChild(contentDiv);
// Append print modal element to document body
document.getElementsByTagName('body')[0].appendChild(printModal);
// Add event listener to close button
document.getElementById('printClose').addEventListener('click', function () {
Modal.close();
});
},
close: function close() {
var printFrame = document.getElementById('printJS-Modal');
printFrame.parentNode.removeChild(printFrame);
}
};
/* harmony default export */ __webpack_exports__["a"] = (Modal);
/***/ }),
/* 4 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__js_init__ = __webpack_require__(7);
var printjs = __WEBPACK_IMPORTED_MODULE_0__js_init__["a" /* default */].init;
if (typeof window !== 'undefined') {
window.printJS = printjs;
}
/* harmony default export */ __webpack_exports__["default"] = (printjs);
/***/ }),
/* 5 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
/* harmony default export */ __webpack_exports__["a"] = ({
print: function print(params, printFrame) {
// Get HTML printable element
var printElement = document.getElementById(params.printable);
// Check if element exists
if (!printElement) {
window.console.error('Invalid HTML element id: ' + params.printable);
return false;
}
// Make a copy of the printElement to prevent DOM changes
var printableElement = document.createElement('div');
printableElement.appendChild(printElement.cloneNode(true));
// Add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles
printableElement.setAttribute('style', 'display:none;');
printableElement.setAttribute('id', 'printJS-html');
printElement.parentNode.appendChild(printableElement);
// Update printableElement variable with newly created DOM element
printableElement = document.getElementById('printJS-html');
// Get main element styling
printableElement.setAttribute('style', __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["c" /* collectStyles */])(printableElement, params) + 'margin:0 !important;');
// Get all children elements
var elements = printableElement.children;
// Get styles for all children elements
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["d" /* loopNodesCollectStyles */])(elements, params);
// Add header if any
if (params.header) {
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement, params.header);
}
// Remove DOM printableElement
printableElement.parentNode.removeChild(printableElement);
// Store html data
params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(printableElement.innerHTML, params);
// Print html element contents
__WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
}
});
/***/ }),
/* 6 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
/* harmony default export */ __webpack_exports__["a"] = ({
print: function print(params, printFrame) {
// Create the image element
var img = document.createElement('img');
// Set image src with image file url
img.src = params.printable;
// Load image
img.onload = function () {
img.setAttribute('style', 'width:100%;');
img.setAttribute('id', 'printableImage');
// Create wrapper
var printableElement = document.createElement('div');
printableElement.setAttribute('style', 'width:100%');
printableElement.appendChild(img);
// Check if we are adding a header for the image
if (params.header) {
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["e" /* addHeader */])(printableElement);
}
// Store html data
params.htmlData = printableElement.outerHTML;
// Print image
__WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
};
}
});
/***/ }),
/* 7 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(1);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__modal__ = __webpack_require__(3);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__pdf__ = __webpack_require__(9);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_3__html__ = __webpack_require__(5);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_4__image__ = __webpack_require__(6);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_5__json__ = __webpack_require__(8);
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; };
var printTypes = ['pdf', 'html', 'image', 'json'];
/* harmony default export */ __webpack_exports__["a"] = ({
init: function init() {
var params = {
printable: null,
type: 'pdf',
header: null,
maxWidth: 800,
font: 'TimesNewRoman',
font_size: '12pt',
honorMarginPadding: true,
honorColor: false,
properties: null,
showModal: false,
modalMessage: 'Retrieving Document...',
frameId: 'printJS',
border: true,
htmlData: ''
};
// Check if a printable document or object was supplied
var args = arguments[0];
if (args === undefined) {
throw new Error('printJS expects at least 1 attribute.');
}
switch (typeof args === 'undefined' ? 'undefined' : _typeof(args)) {
case 'string':
params.printable = encodeURI(args);
params.type = arguments[1] || params.type;
break;
case 'object':
params.printable = args.printable;
params.type = typeof args.type !== 'undefined' ? args.type : params.type;
params.frameId = typeof args.frameId !== 'undefined' ? args.frameId : params.frameId;
params.header = typeof args.header !== 'undefined' ? args.header : params.header;
params.maxWidth = typeof args.maxWidth !== 'undefined' ? args.maxWidth : params.maxWidth;
params.font = typeof args.font !== 'undefined' ? args.font : params.font;
params.font_size = typeof args.font_size !== 'undefined' ? args.font_size : params.font_size;
params.honorMarginPadding = typeof args.honorMarginPadding !== 'undefined' ? args.honorMarginPadding : params.honorMarginPadding;
params.properties = typeof args.properties !== 'undefined' ? args.properties : params.properties;
params.showModal = typeof args.showModal !== 'undefined' ? args.showModal : params.showModal;
params.modalMessage = typeof args.modalMessage !== 'undefined' ? args.modalMessage : params.modalMessage;
break;
default:
throw new Error('Unexpected argument type! Expected "string" or "object", got ' + (typeof args === 'undefined' ? 'undefined' : _typeof(args)));
}
if (!params.printable) {
throw new Error('Missing printable information.');
}
if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) {
throw new Error('Invalid print type. Available types are: pdf, html, image and json.');
}
// Check if we are showing a feedback message to the user (useful for large files)
if (params.showModal) {
__WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].show(params);
}
// To prevent duplication and issues, remove printFrame from the DOM, if it exists
var usedFrame = document.getElementById(params.frameId);
if (usedFrame) {
usedFrame.parentNode.removeChild(usedFrame);
}
// Create a new iframe or embed element (IE prints blank pdf's if we use iframe)
var printFrame = void 0;
// Create iframe element
printFrame = document.createElement('iframe');
// Hide iframe
printFrame.setAttribute('style', 'display:none;');
// Set element id
printFrame.setAttribute('id', params.frameId);
// For non pdf printing in Chrome and Safari, pass an empty html document to srcdoc (force onload callback)
if (params.type !== 'pdf' && (__WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isChrome() || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isSafari())) {
printFrame.srcdoc = '<html><head></head><body></body></html>';
}
// Check printable type
switch (params.type) {
case 'pdf':
// Check browser support for pdf and if not supported we will just open the pdf file instead
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()) {
console.log('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.');
var win = window.open(params.printable, '_blank');
win.focus();
// Make sure there is no loading modal opened
if (params.showModal) __WEBPACK_IMPORTED_MODULE_1__modal__["a" /* default */].close();
} else {
__WEBPACK_IMPORTED_MODULE_2__pdf__["a" /* default */].print(params, printFrame);
}
break;
case 'image':
__WEBPACK_IMPORTED_MODULE_4__image__["a" /* default */].print(params, printFrame);
break;
case 'html':
__WEBPACK_IMPORTED_MODULE_3__html__["a" /* default */].print(params, printFrame);
break;
case 'json':
__WEBPACK_IMPORTED_MODULE_5__json__["a" /* default */].print(params, printFrame);
break;
default:
throw new Error('Invalid print type. Available types are: pdf, html, image and json.');
}
}
});
/***/ }),
/* 8 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__functions__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
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; };
/* harmony default export */ __webpack_exports__["a"] = ({
print: function print(params, printFrame) {
// Check if we received proper data
if (_typeof(params.printable) !== 'object') {
throw new Error('Invalid javascript data object (JSON).');
}
// Check if properties were provided
if (!params.properties || _typeof(params.properties) !== 'object') {
throw new Error('Invalid properties array for your JSON data.');
}
// Variable to hold html string
var htmlData = '';
// Check print has header
if (params.header) {
htmlData += '<h1 style="font-weight:300;">' + params.header + '</h1>';
}
// Function to build html templates for json data
htmlData += jsonToHTML(params);
// Store html data
params.htmlData = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["a" /* addWrapper */])(htmlData, params);
// Print json data
__WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
}
});
function jsonToHTML(params) {
var data = params.printable;
var properties = params.properties;
var htmlData = '<div style="display:flex; flex-direction: column;">';
// Header
htmlData += '<div style="flex:1 1 auto; display:flex;">';
for (var a = 0; a < properties.length; a++) {
htmlData += '<div style="flex:1; padding:5px;">' + __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__functions__["b" /* capitalizePrint */])(properties[a]['displayName'] || properties[a]) + '</div>';
}
htmlData += '</div>';
// Create html data
for (var i = 0; i < data.length; i++) {
htmlData += '<div style="flex:1 1 auto; display:flex;';
htmlData += params.border ? 'border:1px solid lightgray;' : '';
htmlData += '">';
for (var n = 0; n < properties.length; n++) {
htmlData += '<div style="flex:1; padding:5px;">' + data[i][properties[n]['field'] || properties[n]] + '</div>';
}
htmlData += '</div>';
}
htmlData += '</div>';
return htmlData;
}
/***/ }),
/* 9 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__browser__ = __webpack_require__(1);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__print__ = __webpack_require__(0);
/* harmony default export */ __webpack_exports__["a"] = ({
print: function print(params, printFrame) {
// If showing feedback to user, pre load pdf files (hacky)
if (params.showModal || __WEBPACK_IMPORTED_MODULE_0__browser__["a" /* default */].isIE()) {
var req = new window.XMLHttpRequest();
req.addEventListener('load', send(params, printFrame));
req.open('GET', window.location.href + params.printable, true);
req.send();
} else {
send(params, printFrame);
}
}
});
function send(params, printFrame) {
// Set iframe src with pdf document url
printFrame.setAttribute('src', params.printable);
__WEBPACK_IMPORTED_MODULE_1__print__["a" /* default */].send(params, printFrame);
}
/***/ }),
/* 10 */
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(4);
/***/ })
/******/ ]);
{
"/src/css/print.js": "/src/css/print.js",
"/../print.js_docs/print.js": "/../print.js_docs/print.js",
"dist/print.min.css": "dist/print.min.css"
}

@@ -5,8 +5,5 @@ {

"description": "A tiny javascript library to help printing from the web.",
"version": "1.0.19",
"version": "1.0.20",
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/crabbly/Print.js"
},
"repository": "https://github.com/crabbly/Print.js",
"license": "MIT",

@@ -26,3 +23,4 @@ "dependences": {},

"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
}
},
"author": "Rodrigo Vieira <rodrigo@crabbly.com>"
}

@@ -18,7 +18,7 @@ export function addWrapper (htmlData, params) {

if (win.getComputedStyle) { // modern browsers
if (win.getComputedStyle) { // Modern browsers
style = win.getComputedStyle(element, '')
// Styles including
let targetStyles = ['border', 'float', 'box', 'break', 'text-decoration']
let targetStyles = ['border', 'box', 'break', 'text-decoration']

@@ -39,4 +39,4 @@ // Exact match

for (let i = 0; i < style.length; i++) {
for (let s = 0; s < targetStyle.length; s++) {
if (style[i].indexOf(targetStyles[s]) !== -1 || style[i].indexOf(targetStyle[s]) === 0) {
for (let s = 0; s < targetStyles.length; s++) {
if (style[i].indexOf(targetStyles[s]) !== -1 || targetStyle.indexOf(style[i]) !== -1) {
elementStyle += style[i] + ':' + style.getPropertyValue(style[i]) + ';'

@@ -43,0 +43,0 @@ }

@@ -42,3 +42,3 @@ import { addWrapper, capitalizePrint } from './functions'

// Header
htmlData += '<div style="flex:1; display:flex;">'
htmlData += '<div style="flex:1 1 auto; display:flex;">'

@@ -53,3 +53,3 @@ for (let a = 0; a < properties.length; a++) {

for (let i = 0; i < data.length; i++) {
htmlData += '<div style="flex:1; display:flex;'
htmlData += '<div style="flex:1 1 auto; display:flex;'
htmlData += params.border ? 'border:1px solid lightgray;' : ''

@@ -56,0 +56,0 @@ htmlData += '">'

@@ -8,5 +8,2 @@ import Browser from './browser'

if (params.showModal || Browser.isIE()) {
let pdfObject = document.createElement('img')
pdfObject.src = params.printable
let req = new window.XMLHttpRequest()

@@ -13,0 +10,0 @@ req.addEventListener('load', send(params, printFrame))

const { mix } = require('laravel-mix')
mix.js('src/index.js', 'src/css/print.js').styles('src/css/print.css', 'dist/print.min.css')
mix.js('src/index.js', '../print.js_docs/print.js').styles('src/css/print.css', 'dist/print.min.css')

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc