forked from eKoopmans/html2pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml2pdf.min.js
6 lines (6 loc) · 13.2 KB
/
html2pdf.min.js
1
2
3
4
5
6
/**
* html2pdf.js v0.9.1
* Copyright (c) 2018 Erik Koopmans
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("es6-promise/auto"),require("jspdf"),require("html2canvas")):"function"==typeof define&&define.amd?define(["es6-promise/auto","jspdf","html2canvas"],e):t.html2pdf=e(null,t.jsPDF,t.html2canvas)}(this,function(t,e,r){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e,r=r&&r.hasOwnProperty("default")?r.default:r;var n="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},o=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},i=function(t){var e=void 0===t?"undefined":n(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"},a=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},s=function(t,e){if("number"===i(t))return 72*t/96/e;var r={};for(var n in t)r[n]=72*t[n]/96/e;return r},p=function(t,e){return Math.floor(t*e/72*96)},h=function t(e){var r=o(t.convert(Promise.resolve()),JSON.parse(JSON.stringify(t.template))),n=t.convert(Promise.resolve(),r);return n=n.setProgress(1,t,1,[t]),n=n.set(e)};(h.prototype=Object.create(Promise.prototype)).constructor=h,h.convert=function(t,e){return t.__proto__=e||h.prototype,t},h.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:{}}},h.prototype.from=function(t,e){return this.then(function(){switch(e=e||function(t){switch(i(t)){case"string":return"string";case"element":return"canvas"===t.nodeName.toLowerCase?"canvas":"element";default:return"unknown"}}(t)){case"string":return this.set({src:a("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.")}})},h.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.")}},h.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=a("div",{className:"html2pdf__overlay",style:t}),this.prop.container=a("div",{className:"html2pdf__container",style:e}),this.prop.container.appendChild(r),this.prop.overlay.appendChild(this.prop.container),document.body.appendChild(this.prop.overlay)})},h.prototype.toCanvas=function(){var t=[function(){return document.body.contains(this.prop.container)||this.toContainer()}];return this.thenList(t).then(function(){var t=o({},this.opt.html2canvas);return delete t.onrendered,r(this.prop.container,t)}).then(function(t){(this.opt.html2canvas.onrendered||function(){})(t),this.prop.canvas=t,document.body.removeChild(this.prop.overlay)})},h.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})},h.prototype.toPdf=function(){return this.thenList([function(){return this.prop.canvas||this.toCanvas()}]).then(function(){var t=this.prop.canvas,r=this.opt,n=(t.getContext("2d"),t.height),o=Math.floor(t.width*this.prop.pageSize.inner.ratio),i=Math.ceil(n/o),a=this.prop.pageSize.inner.height,s=document.createElement("canvas"),p=s.getContext("2d");s.width=t.width,s.height=o,this.prop.pdf=this.prop.pdf||new e(r.jsPDF);for(var h=0;h<i;h++){h===i-1&&(s.height=n%o,a=s.height*this.prop.pageSize.inner.width/s.width);var c=s.width,u=s.height;p.fillStyle="white",p.fillRect(0,0,c,u),p.drawImage(t,0,h*o,c,u,0,0,c,u),h&&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)}})},h.prototype.output=function(t,e,r){return"img"===(r=r||"pdf").toLowerCase()||"image"===r.toLowerCase()?this.outputImg(t,e):this.outputPdf(t,e)},h.prototype.outputPdf=function(t,e){return this.thenList([function(){return this.prop.pdf||this.toPdf()}]).then(function(){return this.prop.pdf.output(t,e)})},h.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.'}})},h.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)})},h.prototype.set=function(t){if("object"!==i(t))return this;var e=Object.keys(t||{}).map(function(e){if(e in h.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)})},h.prototype.get=function(t,e){return this.then(function(){var r=t in h.template.prop?this.prop[t]:this.opt[t];return e?e(r):r})},h.prototype.setMargin=function(t){return this.then(function(){switch(i(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)},h.prototype.setPageSize=function(t){return this.then(function(){(t=t||e.getPageSize(this.opt.jsPDF)).hasOwnProperty("inner")||(t.inner={width:t.width-this.opt.margin[1]-this.opt.margin[3],height:t.height-this.opt.margin[0]-this.opt.margin[2]},t.inner.px={width:p(t.inner.width,t.k),height:p(t.inner.height,t.k)},t.inner.ratio=t.inner.height/t.inner.width),this.prop.pageSize=t})},h.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},h.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)},h.prototype.then=function(t,e){var r=this;return this.thenCore(t,e,function(t,e){return r.updateProgress(null,null,1,[t]),Promise.prototype.then.call(this,function(e){return r.updateProgress(null,t),e}).then(t,e).then(function(t){return r.updateProgress(1),t})})},h.prototype.thenCore=function(t,e,r){r=r||Promise.prototype.then;t&&(t=t.bind(this)),e&&(e=e.bind(this));var n=-1!==Promise.toString().indexOf("[native code]")&&"Promise"===Promise.name?this:h.convert(o({},this),Promise.prototype),i=r.call(n,t,e);return h.convert(i,this.__proto__)},h.prototype.thenExternal=function(t,e){return Promise.prototype.then.call(this,t,e)},h.prototype.thenList=function(t){var e=this;return t.forEach(function(t){e=e.thenCore(t)}),e},h.prototype.catch=function(t){t&&(t=t.bind(this));var e=Promise.prototype.catch.call(this,t);return h.convert(e,this)},h.prototype.catchExternal=function(t){return Promise.prototype.catch.call(this,t)},h.prototype.error=function(t){return this.then(function(){throw new Error(t)})},h.prototype.using=h.prototype.set,h.prototype.saveAs=h.prototype.save,h.prototype.export=h.prototype.output,h.prototype.run=h.prototype.then,e.getPageSize=function(t,e,r){if("object"===(void 0===t?"undefined":n(t))){var o=t;t=o.orientation,e=o.unit||e,r=o.format||r}e=e||"mm",r=r||"a4",t=(""+(t||"P")).toLowerCase();var i=(""+r).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 p=a[i][1]/s,h=a[i][0]/s;else try{var p=r[1],h=r[0]}catch(t){throw new Error("Invalid format: "+r)}if("p"===t||"portrait"===t){if(t="p",h>p){c=h;h=p,p=c}}else{if("l"!==t&&"landscape"!==t)throw"Invalid orientation: "+t;if(t="l",p>h){var c=h;h=p,p=c}}return{width:h,height:p,unit:e,k:s}};var c={toContainer:h.prototype.toContainer};h.template.opt.pagebreak={mode:["css","legacy"],before:[],after:[],avoid:[]},h.prototype.toContainer=function(){return c.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={},i=this;["before","after","avoid"].forEach(function(e){var r=n.avoidAll&&"avoid"===e;o[e]=r?[]:[].concat(i.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 p=t.querySelectorAll("*");Array.prototype.forEach.call(p,function(t){var r={before:!1,after:n.legacy&&-1!==s.indexOf(t),avoid:n.avoidAll};if(n.css){var i=window.getComputedStyle(t),p=["always","page","left","right"];r={before:r.before||-1!==p.indexOf(i.breakBefore||i.pageBreakBefore),after:r.after||-1!==p.indexOf(i.breakAfter||i.pageBreakAfter),avoid:r.avoid||-1!==["avoid","avoid-page"].indexOf(i.breakInside||i.pageBreakInside)}}Object.keys(r).forEach(function(e){r[e]=r[e]||-1!==o[e].indexOf(t)});var h=t.getBoundingClientRect();if(r.avoid&&!r.before){var c=Math.floor(h.top/e),u=Math.floor(h.bottom/e),l=Math.abs(h.bottom-h.top)/e;u!==c&&l<=1&&(r.before=!0)}if(r.before){f=a("div",{style:{display:"block",height:e-h.top%e+"px"}});t.parentNode.insertBefore(f,t)}if(r.after){var f=a("div",{style:{display:"block",height:e-h.bottom%e+"px"}});t.parentNode.insertBefore(f,t.nextSibling)}})})};var u=[],l={toContainer:h.prototype.toContainer,toPdf:h.prototype.toPdf};h.prototype.toContainer=function(){return l.toContainer.call(this).then(function(){if(this.opt.enableLinks){var t=this.prop.container,e=t.querySelectorAll("a"),r=s(t.getBoundingClientRect(),this.prop.pageSize.k);u=[],Array.prototype.forEach.call(e,function(t){for(var e=t.getClientRects(),n=0;n<e.length;n++){var o=s(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,a=this.opt.margin[0]+o.top%this.prop.pageSize.inner.height,p=this.opt.margin[1]+o.left;u.push({page:i,top:a,left:p,clientRect:o,link:t})}},this)}})},h.prototype.toPdf=function(){return l.toPdf.call(this).then(function(){if(this.opt.enableLinks){u.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 f=function t(e,r){var n=new t.Worker(r);return e?n.from(e).save():n};return f.Worker=h,f});