UNPKG

jspdf-html2canvas-pro

Version:

A combine usage with jsPDF and html2canvas-pro, which translating html content to PDF file.

1 lines 4.95 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("jspdf"),require("html2canvas-pro")):"function"==typeof define&&define.amd?define(["jspdf","html2canvas-pro"],e):"object"==typeof exports?exports["jspdf-html2canvas-pro"]=e(require("jspdf"),require("html2canvas-pro")):t.html2PDF=e(t.jspdf,t["html2canvas-pro"])}(self,((t,e)=>(()=>{"use strict";var n={791:t=>{t.exports=e},295:e=>{e.exports=t}},r={};function o(t){var e=r[t];if(void 0!==e)return e.exports;var a=r[t]={exports:{}};return n[t](a,a.exports,o),a.exports}o.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return o.d(e,{a:e}),e},o.d=(t,e)=>{for(var n in e)o.o(e,n)&&!o.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var a={};o.d(a,{default:()=>w});var i=o(295),f=o(791),p=o.n(f);const c={jsPDF:{unit:"pt",format:"a4"},html2canvas:{imageTimeout:15e3,logging:!0,useCORS:!1},margin:{right:0,top:0,bottom:0,left:0},imageType:"image/jpeg",imageQuality:1,autoResize:!0,output:"jspdf-generate.pdf",watermark:void 0,init:function(){},success:function(t){t.save(this.output)}},s=window.devicePixelRatio,u={"image/jpeg":"JPEG","image/png":"PNG","image/webp":"WEBP"},d=function(t){return u[t]},l=function(t,e,n){let{pdf:r,pdfContentWidth:o,pdfContentHeight:a,pdfWidth:i,pdfHeight:f,position:p,currentPage:c,pageOfCurrentNode:u}=e;const{pageData:l,printWidth:g,printHeight:m}=function({canvas:t,pdf:e,pdfContentWidth:n,opts:r}){const o=t.toDataURL(r.imageType,r.imageQuality),a=e.getImageProperties(o);return{pageData:o,printWidth:r.autoResize?n:a.width/s,printHeight:r.autoResize?n/a.width*a.height:a.height/s}}({canvas:t,pdf:r,pdfContentWidth:o,opts:n});let h=m;p<0&&(r.addPage(),c+=1,u=1,p=0);const{margin:v}=n;for(;h>0;){if(r.addImage(l,d(n.imageType),v.left,p+v.top*u+v.bottom*(u-1),g,m),r.setFillColor(255,255,255),r.rect(0,0,i,v.top,"F"),r.rect(0,f-v.bottom,i,v.bottom,"F"),h<a){p-=h;break}h-=a,p-=f,r.addPage(),c+=1,u+=1}!function(t,e,n,r,o){t.pdf=e,t.position=n,t.currentPage=r,t.pageOfCurrentNode=o}(e,r,p,c,u)};const g=function(t,e){return n=this,r=void 0,a=function*(){e.watermark&&(yield function(t,e){return new Promise((n=>{var r;const o=new Image,a="string"==typeof e.watermark?e.watermark:"function"!=typeof e.watermark&&(null===(r=e.watermark)||void 0===r?void 0:r.src),i=()=>{t.pdf=function(t,e){var n,r;const o=t.internal.getNumberOfPages();if("function"==typeof e.watermark)for(let n=1;n<=o;n++){t.setPage(n);const r={pdf:t,pageNumber:n,totalPageNumber:o};e.watermark.call(e,r)}else if(e.watermarkImg){const a=t.getImageProperties(e.watermarkImg),i="string"!=typeof e.watermark&&(null===(n=e.watermark)||void 0===n?void 0:n.scale)||1;for(let n=1;n<=o;n++)if(t.setPage(n),"string"!=typeof e.watermark&&(null===(r=e.watermark)||void 0===r?void 0:r.handler)){const r={pdf:t,pageNumber:n,totalPageNumber:o,imgNode:e.watermarkImg};e.watermark.handler.call(e,r)}else t.addImage(e.watermarkImg,d("image/png"),(t.internal.pageSize.width-a.width*i)/2,(t.internal.pageSize.height-a.height*i)/2,a.width*i,a.height*i)}else console.warn('[jspdf-html2canvas-pro] "watermark" option should be either "string" or "function" type.');return t}(t.pdf,e),n(null)};a?(o.onload=function(){e.watermarkImg=o,i()},o.crossOrigin="Anonymous",o.src=a):"function"==typeof e.watermark&&i()}))}(t,e))},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{p(a.next(t))}catch(t){e(t)}}function f(t){try{p(a.throw(t))}catch(t){e(t)}}function p(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,f)}p((a=a.apply(n,r||[])).next())}));var n,r,o,a};function m(t){return null!==t&&"object"==typeof t}function h(t){return"function"==typeof t}function v(t,e){for(let n=0,r=Object.keys(t);n<r.length;n++){const o=r[n];if(o in e){const n=m(t[o]),r=m(e[o]);n&&r?h(t[o])&&h(e[o])?h(t[o])&&!h(e[o])?console.error(`[jspdf-html2canvas-pro] config key "${o}" type invalid`):t[o]=e[o]:t[o]=v(t[o],e[o]):r?(e[o].old=t[o],t[o]=e[o]):n?t[o].new=e[o]:t[o]=e[o],delete e[o]}}return Object.assign(t,e)}const w=function(t){return e=this,n=arguments,o=function*(t,e={}){const n=v(c,e),r=function(t){const{margin:e}=t,n=new i.jsPDF(t.jsPDF),r=n.internal.pageSize.getWidth(),o=n.internal.pageSize.getHeight();return{pdf:n,pdfWidth:r,pdfHeight:o,pdfContentWidth:r-(e.left+e.right),pdfContentHeight:o-(e.top+e.bottom),position:0,currentPage:1,pageOfCurrentNode:1}}(n);if(n.init.call(n,r.pdf),"length"in t)for(let e=0;e<t.length;e++){const o=yield p()(t[e],n.html2canvas);l(o,r,n)}else{const e=yield p()(t,n.html2canvas);l(e,r,n)}return yield g(r,n),n.success.call(n,r.pdf),r.pdf},new((r=void 0)||(r=Promise))((function(t,a){function i(t){try{p(o.next(t))}catch(t){a(t)}}function f(t){try{p(o.throw(t))}catch(t){a(t)}}function p(e){var n;e.done?t(e.value):(n=e.value,n instanceof r?n:new r((function(t){t(n)}))).then(i,f)}p((o=o.apply(e,n||[])).next())}));var e,n,r,o};return a.default})()));