forked from eKoopmans/html2pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html2pdf.min.js
6 lines (6 loc) · 19.2 KB
/
html2pdf.min.js
1
2
3
4
5
6
/**
* html2pdf.js v0.9.2
* Copyright (c) 2020 Erik Koopmans
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("jspdf"),require("html2canvas")):"function"==typeof define&&define.amd?define(["jspdf","html2canvas"],e):t.html2pdf=e(t.jsPDF,t.html2canvas)}(this,function(t,e){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t,e=e&&e.hasOwnProperty("default")?e.default:e;var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},o=function(t){var e=void 0===t?"undefined":r(t);return"undefined"===e?"undefined":"string"===e||t instanceof String?"string":"number"===e||t instanceof Number?"number":"function"===e||t instanceof Function?"function":t&&t.constructor===Array?"array":t&&1===t.nodeType?"element":"object"===e?"object":"unknown"},i=function(t,e){var r=document.createElement(t);if(e.className&&(r.className=e.className),e.innerHTML){r.innerHTML=e.innerHTML;for(var n=r.getElementsByTagName("script"),o=n.length;o-- >0;null)n[o].parentNode.removeChild(n[o])}for(var i in e.style)r.style[i]=e.style[i];return r},a=function(t,e){if("number"===o(t))return 72*t/96/e;var r={};for(var n in t)r[n]=72*t[n]/96/e;return r},s=function(t,e){return Math.floor(t*e/72*96)},u="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},p=function(t,e){return e={exports:{}},t(e,e.exports),e.exports}(function(t,e){t.exports=function(){function t(t){return"function"==typeof t}function e(){var t=setTimeout;return function(){return t(r,1)}}function r(){for(var t=0;t<b;t+=2){var e=j[t],r=j[t+1];e(r),j[t]=void 0,j[t+1]=void 0}b=0}function n(t,e){var r=this,n=new this.constructor(i);void 0===n[L]&&g(n);var o=r._state;if(o){var a=arguments[o-1];S(function(){return v(o,n,a,r._result)})}else f(r,n,t,e);return n}function o(t){if(t&&"object"==typeof t&&t.constructor===this)return t;var e=new this(i);return p(e,t),e}function i(){}function a(t){try{return t.then}catch(t){return N.error=t,N}}function s(e,r,i){r.constructor===e.constructor&&i===n&&r.constructor.resolve===o?function(t,e){e._state===T?h(t,e._result):e._state===z?l(t,e._result):f(e,void 0,function(e){return p(t,e)},function(e){return l(t,e)})}(e,r):i===N?(l(e,N.error),N.error=null):void 0===i?h(e,r):t(i)?function(t,e,r){S(function(t){var n=!1,o=function(t,e,r,n){try{t.call(e,r,n)}catch(t){return t}}(r,e,function(r){n||(n=!0,e!==r?p(t,r):h(t,r))},function(e){n||(n=!0,l(t,e))},t._label);!n&&o&&(n=!0,l(t,o))},t)}(e,r,i):h(e,r)}function p(t,e){t===e?l(t,new TypeError("You cannot resolve a promise with itself")):!function(t){var e=typeof t;return null!==t&&("object"===e||"function"===e)}(e)?h(t,e):s(t,e,a(e))}function c(t){t._onerror&&t._onerror(t._result),d(t)}function h(t,e){t._state===O&&(t._result=e,t._state=T,0!==t._subscribers.length&&S(d,t))}function l(t,e){t._state===O&&(t._state=z,t._result=e,S(c,t))}function f(t,e,r,n){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+T]=r,o[i+z]=n,0===i&&t._state&&S(d,t)}function d(t){var e=t._subscribers,r=t._state;if(0!==e.length){for(var n=void 0,o=void 0,i=t._result,a=0;a<e.length;a+=3)n=e[a],o=e[a+r],n?v(r,n,o,i):o(i);t._subscribers.length=0}}function v(e,r,n,o){var i=t(n),a=void 0,s=void 0,u=void 0,c=void 0;if(i){if((a=function(t,e){try{return t(e)}catch(t){return N.error=t,N}}(n,o))===N?(c=!0,s=a.error,a.error=null):u=!0,r===a)return void l(r,new TypeError("A promises callback cannot return that same promise."))}else a=o,u=!0;r._state!==O||(i&&u?p(r,a):c?l(r,s):e===T?h(r,a):e===z&&l(r,a))}function g(t){t[L]=M++,t._state=void 0,t._result=void 0,t._subscribers=[]}var y=void 0;y=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var m=y,b=0,w=void 0,_=void 0,S=function(t,e){j[b]=t,j[b+1]=e,2===(b+=2)&&(_?_(r):E())};var P="undefined"!=typeof window?window:void 0,C=P||{},k=C.MutationObserver||C.WebKitMutationObserver,A="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),x="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel;var j=new Array(1e3);var E=void 0;E=A?function(){return process.nextTick(r)}:k?function(){var t=0,e=new k(r),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}():x?function(){var t=new MessageChannel;return t.port1.onmessage=r,function(){return t.port2.postMessage(0)}}():void 0===P?function(){try{var t=Function("return this")().require("vertx");return w=t.runOnLoop||t.runOnContext,function(){if(void 0!==w)return function(){w(r)};return e()}()}catch(t){return e()}}():e();var L=Math.random().toString(36).substring(2);var O=void 0,T=1,z=2,N={error:null};var M=0;var I=function(){function t(t,e){this._instanceConstructor=t,this.promise=new t(i),this.promise[L]||g(this.promise),m(e)?(this.length=e.length,this._remaining=e.length,this._result=new Array(this.length),0===this.length?h(this.promise,this._result):(this.length=this.length||0,this._enumerate(e),0===this._remaining&&h(this.promise,this._result))):l(this.promise,new Error("Array Methods must be provided an Array"))}return t.prototype._enumerate=function(t){for(var e=0;this._state===O&&e<t.length;e++)this._eachEntry(t[e],e)},t.prototype._eachEntry=function(t,e){var r=this._instanceConstructor,u=r.resolve;if(u===o){var p=a(t);if(p===n&&t._state!==O)this._settledAt(t._state,e,t._result);else if("function"!=typeof p)this._remaining--,this._result[e]=t;else if(r===F){var c=new r(i);s(c,t,p),this._willSettleAt(c,e)}else this._willSettleAt(new r(function(e){return e(t)}),e)}else this._willSettleAt(u(t),e)},t.prototype._settledAt=function(t,e,r){var n=this.promise;n._state===O&&(this._remaining--,t===z?l(n,r):this._result[e]=r),0===this._remaining&&h(n,this._result)},t.prototype._willSettleAt=function(t,e){var r=this;f(t,void 0,function(t){return r._settledAt(T,e,t)},function(t){return r._settledAt(z,e,t)})},t}();var F=function(){function e(t){this[L]=M++,this._result=this._state=void 0,this._subscribers=[],i!==t&&("function"!=typeof t&&function(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}(),this instanceof e?function(t,e){try{e(function(e){p(t,e)},function(e){l(t,e)})}catch(e){l(t,e)}}(this,t):function(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}())}return e.prototype.catch=function(t){return this.then(null,t)},e.prototype.finally=function(e){var r=this.constructor;return t(e)?this.then(function(t){return r.resolve(e()).then(function(){return t})},function(t){return r.resolve(e()).then(function(){throw t})}):this.then(e,e)},e}();F.prototype.then=n,F.all=function(t){return new I(this,t).promise},F.race=function(t){var e=this;return new e(m(t)?function(r,n){for(var o=t.length,i=0;i<o;i++)e.resolve(t[i]).then(r,n)}:function(t,e){return e(new TypeError("You must pass an array to race."))})},F.resolve=o,F.reject=function(t){var e=new this(i);return l(e,t),e},F._setScheduler=function(t){_=t},F._setAsap=function(t){S=t},F._asap=S;return F.polyfill=function(){var t=void 0;if(void 0!==u)t=u;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(t){throw new Error("polyfill failed because global object is unavailable in this environment")}var e=t.Promise;if(e){var r=null;try{r=Object.prototype.toString.call(e.resolve())}catch(t){}if("[object Promise]"===r&&!e.cast)return}t.Promise=F},F.Promise=F,F}()}).Promise,c=function t(e){var r=n(t.convert(p.resolve()),JSON.parse(JSON.stringify(t.template))),o=t.convert(p.resolve(),r);return o=o.setProgress(1,t,1,[t]),o=o.set(e)};(c.prototype=Object.create(p.prototype)).constructor=c,c.convert=function(t,e){return t.__proto__=e||c.prototype,t},c.template={prop:{src:null,container:null,overlay:null,canvas:null,img:null,pdf:null,pageSize:null},progress:{val:0,state:null,n:0,stack:[]},opt:{filename:"file.pdf",margin:[0,0,0,0],image:{type:"jpeg",quality:.95},enableLinks:!0,html2canvas:{},jsPDF:{}}},c.prototype.from=function(t,e){return this.then(function(){switch(e=e||function(t){switch(o(t)){case"string":return"string";case"element":return"canvas"===t.nodeName.toLowerCase?"canvas":"element";default:return"unknown"}}(t)){case"string":return this.set({src:i("div",{innerHTML:t})});case"element":return this.set({src:t});case"canvas":return this.set({canvas:t});case"img":return this.set({img:t});default:return this.error("Unknown source type.")}})},c.prototype.to=function(t){switch(t){case"container":return this.toContainer();case"canvas":return this.toCanvas();case"img":return this.toImg();case"pdf":return this.toPdf();default:return this.error("Invalid target.")}},c.prototype.toContainer=function(){return this.thenList([function(){return this.prop.src||this.error("Cannot duplicate - no source HTML.")},function(){return this.prop.pageSize||this.setPageSize()}]).then(function(){var t={position:"fixed",overflow:"hidden",zIndex:1e3,left:0,right:0,bottom:0,top:0,backgroundColor:"rgba(0,0,0,0.8)"},e={position:"absolute",width:this.prop.pageSize.inner.width+this.prop.pageSize.unit,left:0,right:0,top:0,height:"auto",margin:"auto",backgroundColor:"white"};t.opacity=0;var r=function t(e,r){for(var n=3===e.nodeType?document.createTextNode(e.nodeValue):e.cloneNode(!1),o=e.firstChild;o;o=o.nextSibling)!0!==r&&1===o.nodeType&&"SCRIPT"===o.nodeName||n.appendChild(t(o,r));return 1===e.nodeType&&("CANVAS"===e.nodeName?(n.width=e.width,n.height=e.height,n.getContext("2d").drawImage(e,0,0)):"TEXTAREA"!==e.nodeName&&"SELECT"!==e.nodeName||(n.value=e.value),n.addEventListener("load",function(){n.scrollTop=e.scrollTop,n.scrollLeft=e.scrollLeft},!0)),n}(this.prop.src,this.opt.html2canvas.javascriptEnabled);this.prop.overlay=i("div",{className:"html2pdf__overlay",style:t}),this.prop.container=i("div",{className:"html2pdf__container",style:e}),this.prop.container.appendChild(r),this.prop.overlay.appendChild(this.prop.container),document.body.appendChild(this.prop.overlay)})},c.prototype.toCanvas=function(){var t=[function(){return document.body.contains(this.prop.container)||this.toContainer()}];return this.thenList(t).then(function(){var t=n({},this.opt.html2canvas);return delete t.onrendered,e(this.prop.container,t)}).then(function(t){(this.opt.html2canvas.onrendered||function(){})(t),this.prop.canvas=t,document.body.removeChild(this.prop.overlay)})},c.prototype.toImg=function(){return this.thenList([function(){return this.prop.canvas||this.toCanvas()}]).then(function(){var t=this.prop.canvas.toDataURL("image/"+this.opt.image.type,this.opt.image.quality);this.prop.img=document.createElement("img"),this.prop.img.src=t})},c.prototype.toPdf=function(){return this.thenList([function(){return this.prop.canvas||this.toCanvas()}]).then(function(){var e=this.prop.canvas,r=this.opt,n=e.height,o=Math.floor(e.width*this.prop.pageSize.inner.ratio),i=Math.ceil(n/o),a=this.prop.pageSize.inner.height,s=document.createElement("canvas"),u=s.getContext("2d");s.width=e.width,s.height=o,this.prop.pdf=this.prop.pdf||new t(r.jsPDF);for(var p=0;p<i;p++){p===i-1&&n%o!=0&&(s.height=n%o,a=s.height*this.prop.pageSize.inner.width/s.width);var c=s.width,h=s.height;u.fillStyle="white",u.fillRect(0,0,c,h),u.drawImage(e,0,p*o,c,h,0,0,c,h),p&&this.prop.pdf.addPage();var l=s.toDataURL("image/"+r.image.type,r.image.quality);this.prop.pdf.addImage(l,r.image.type,r.margin[1],r.margin[0],this.prop.pageSize.inner.width,a)}})},c.prototype.output=function(t,e,r){return"img"===(r=r||"pdf").toLowerCase()||"image"===r.toLowerCase()?this.outputImg(t,e):this.outputPdf(t,e)},c.prototype.outputPdf=function(t,e){return this.thenList([function(){return this.prop.pdf||this.toPdf()}]).then(function(){return this.prop.pdf.output(t,e)})},c.prototype.outputImg=function(t,e){return this.thenList([function(){return this.prop.img||this.toImg()}]).then(function(){switch(t){case void 0:case"img":return this.prop.img;case"datauristring":case"dataurlstring":return this.prop.img.src;case"datauri":case"dataurl":return document.location.href=this.prop.img.src;default:throw'Image output type "'+t+'" is not supported.'}})},c.prototype.save=function(t){return this.thenList([function(){return this.prop.pdf||this.toPdf()}]).set(t?{filename:t}:null).then(function(){this.prop.pdf.save(this.opt.filename)})},c.prototype.set=function(t){if("object"!==o(t))return this;var e=Object.keys(t||{}).map(function(e){if(e in c.template.prop)return function(){this.prop[e]=t[e]};switch(e){case"margin":return this.setMargin.bind(this,t.margin);case"jsPDF":return function(){return this.opt.jsPDF=t.jsPDF,this.setPageSize()};case"pageSize":return this.setPageSize.bind(this,t.pageSize);default:return function(){this.opt[e]=t[e]}}},this);return this.then(function(){return this.thenList(e)})},c.prototype.get=function(t,e){return this.then(function(){var r=t in c.template.prop?this.prop[t]:this.opt[t];return e?e(r):r})},c.prototype.setMargin=function(t){return this.then(function(){switch(o(t)){case"number":t=[t,t,t,t];case"array":if(2===t.length&&(t=[t[0],t[1],t[0],t[1]]),4===t.length)break;default:return this.error("Invalid margin array.")}this.opt.margin=t}).then(this.setPageSize)},c.prototype.setPageSize=function(e){return this.then(function(){(e=e||t.getPageSize(this.opt.jsPDF)).hasOwnProperty("inner")||(e.inner={width:e.width-this.opt.margin[1]-this.opt.margin[3],height:e.height-this.opt.margin[0]-this.opt.margin[2]},e.inner.px={width:s(e.inner.width,e.k),height:s(e.inner.height,e.k)},e.inner.ratio=e.inner.height/e.inner.width),this.prop.pageSize=e})},c.prototype.setProgress=function(t,e,r,n){return null!=t&&(this.progress.val=t),null!=e&&(this.progress.state=e),null!=r&&(this.progress.n=r),null!=n&&(this.progress.stack=n),this.progress.ratio=this.progress.val/this.progress.state,this},c.prototype.updateProgress=function(t,e,r,n){return this.setProgress(t?this.progress.val+t:null,e||null,r?this.progress.n+r:null,n?this.progress.stack.concat(n):null)},c.prototype.then=function(t,e){var r=this;return this.thenCore(t,e,function(t,e){return r.updateProgress(null,null,1,[t]),p.prototype.then.call(this,function(e){return r.updateProgress(null,t),e}).then(t,e).then(function(t){return r.updateProgress(1),t})})},c.prototype.thenCore=function(t,e,r){r=r||p.prototype.then;t&&(t=t.bind(this)),e&&(e=e.bind(this));var o=-1!==p.toString().indexOf("[native code]")&&"Promise"===p.name?this:c.convert(n({},this),p.prototype),i=r.call(o,t,e);return c.convert(i,this.__proto__)},c.prototype.thenExternal=function(t,e){return p.prototype.then.call(this,t,e)},c.prototype.thenList=function(t){var e=this;return t.forEach(function(t){e=e.thenCore(t)}),e},c.prototype.catch=function(t){t&&(t=t.bind(this));var e=p.prototype.catch.call(this,t);return c.convert(e,this)},c.prototype.catchExternal=function(t){return p.prototype.catch.call(this,t)},c.prototype.error=function(t){return this.then(function(){throw new Error(t)})},c.prototype.using=c.prototype.set,c.prototype.saveAs=c.prototype.save,c.prototype.export=c.prototype.output,c.prototype.run=c.prototype.then,t.getPageSize=function(t,e,n){if("object"===(void 0===t?"undefined":r(t))){var o=t;t=o.orientation,e=o.unit||e,n=o.format||n}e=e||"mm",n=n||"a4",t=(""+(t||"P")).toLowerCase();var i=(""+n).toLowerCase(),a={a0:[2383.94,3370.39],a1:[1683.78,2383.94],a2:[1190.55,1683.78],a3:[841.89,1190.55],a4:[595.28,841.89],a5:[419.53,595.28],a6:[297.64,419.53],a7:[209.76,297.64],a8:[147.4,209.76],a9:[104.88,147.4],a10:[73.7,104.88],b0:[2834.65,4008.19],b1:[2004.09,2834.65],b2:[1417.32,2004.09],b3:[1000.63,1417.32],b4:[708.66,1000.63],b5:[498.9,708.66],b6:[354.33,498.9],b7:[249.45,354.33],b8:[175.75,249.45],b9:[124.72,175.75],b10:[87.87,124.72],c0:[2599.37,3676.54],c1:[1836.85,2599.37],c2:[1298.27,1836.85],c3:[918.43,1298.27],c4:[649.13,918.43],c5:[459.21,649.13],c6:[323.15,459.21],c7:[229.61,323.15],c8:[161.57,229.61],c9:[113.39,161.57],c10:[79.37,113.39],dl:[311.81,623.62],letter:[612,792],"government-letter":[576,756],legal:[612,1008],"junior-legal":[576,360],ledger:[1224,792],tabloid:[792,1224],"credit-card":[153,243]};switch(e){case"pt":s=1;break;case"mm":s=72/25.4;break;case"cm":s=72/2.54;break;case"in":s=72;break;case"px":s=.75;break;case"pc":case"em":s=12;break;case"ex":var s=6;break;default:throw"Invalid unit: "+e}if(a.hasOwnProperty(i))var u=a[i][1]/s,p=a[i][0]/s;else try{var u=n[1],p=n[0]}catch(t){throw new Error("Invalid format: "+n)}if("p"===t||"portrait"===t){if(t="p",p>u){c=p;p=u,u=c}}else{if("l"!==t&&"landscape"!==t)throw"Invalid orientation: "+t;if(t="l",u>p){var c=p;p=u,u=c}}return{width:p,height:u,unit:e,k:s}};var h={toContainer:c.prototype.toContainer};c.template.opt.pagebreak={mode:["css","legacy"],before:[],after:[],avoid:[]},c.prototype.toContainer=function(){return h.toContainer.call(this).then(function(){var t=this.prop.container,e=this.prop.pageSize.inner.px.height,r=[].concat(this.opt.pagebreak.mode),n={avoidAll:-1!==r.indexOf("avoid-all"),css:-1!==r.indexOf("css"),legacy:-1!==r.indexOf("legacy")},o={},a=this;["before","after","avoid"].forEach(function(e){var r=n.avoidAll&&"avoid"===e;o[e]=r?[]:[].concat(a.opt.pagebreak[e]||[]),o[e].length>0&&(o[e]=Array.prototype.slice.call(t.querySelectorAll(o[e].join(", "))))});var s=t.querySelectorAll(".html2pdf__page-break");s=Array.prototype.slice.call(s);var u=t.querySelectorAll("*");Array.prototype.forEach.call(u,function(t){var r={before:!1,after:n.legacy&&-1!==s.indexOf(t),avoid:n.avoidAll};if(n.css){var a=window.getComputedStyle(t),u=["always","page","left","right"];r={before:r.before||-1!==u.indexOf(a.breakBefore||a.pageBreakBefore),after:r.after||-1!==u.indexOf(a.breakAfter||a.pageBreakAfter),avoid:r.avoid||-1!==["avoid","avoid-page"].indexOf(a.breakInside||a.pageBreakInside)}}Object.keys(r).forEach(function(e){r[e]=r[e]||-1!==o[e].indexOf(t)});var p=t.getBoundingClientRect();if(r.avoid&&!r.before){var c=Math.floor(p.top/e),h=Math.floor(p.bottom/e),l=Math.abs(p.bottom-p.top)/e;h!==c&&l<=1&&(r.before=!0)}if(r.before){f=i("div",{style:{display:"block",height:e-p.top%e+"px"}});t.parentNode.insertBefore(f,t)}if(r.after){var f=i("div",{style:{display:"block",height:e-p.bottom%e+"px"}});t.parentNode.insertBefore(f,t.nextSibling)}})})};var l=[],f={toContainer:c.prototype.toContainer,toPdf:c.prototype.toPdf};c.prototype.toContainer=function(){return f.toContainer.call(this).then(function(){if(this.opt.enableLinks){var t=this.prop.container,e=t.querySelectorAll("a"),r=a(t.getBoundingClientRect(),this.prop.pageSize.k);l=[],Array.prototype.forEach.call(e,function(t){for(var e=t.getClientRects(),n=0;n<e.length;n++){var o=a(e[n],this.prop.pageSize.k);o.left-=r.left,o.top-=r.top;var i=Math.floor(o.top/this.prop.pageSize.inner.height)+1,s=this.opt.margin[0]+o.top%this.prop.pageSize.inner.height,u=this.opt.margin[1]+o.left;l.push({page:i,top:s,left:u,clientRect:o,link:t})}},this)}})},c.prototype.toPdf=function(){return f.toPdf.call(this).then(function(){if(this.opt.enableLinks){l.forEach(function(t){this.prop.pdf.setPage(t.page),this.prop.pdf.link(t.left,t.top,t.clientRect.width,t.clientRect.height,{url:t.link.href})},this);var t=this.prop.pdf.internal.getNumberOfPages();this.prop.pdf.setPage(t)}})};var d=function t(e,r){var n=new t.Worker(r);return e?n.from(e).save():n};return d.Worker=c,d});