UNPKG

jspdf-html2canvas

Version:

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

1 lines 4.57 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"]=e(require("jspdf"),require("html2canvas-pro")):t.html2PDF=e(t.jspdf,t.html2canvas)}(this,(t,e)=>(()=>{"use strict";var a={326:t=>{t.exports=e},396:e=>{e.exports=t}},n={};function r(t){var e=n[t];if(void 0!==e)return e.exports;var o=n[t]={exports:{}};return a[t](o,o.exports,r),o.exports}r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var a in e)r.o(e,a)&&!r.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:e[a]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var o={};r.d(o,{default:()=>y});var i=r(396),s=r(326),p=r.n(s);const f={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)}},c="undefined"!=typeof window?window.devicePixelRatio:1,d={"image/jpeg":"JPEG","image/png":"PNG","image/webp":"WEBP"},g=function(t){return d[t]},l=function(t,e,a){let{pdf:n,pdfContentWidth:r,pdfContentHeight:o,pdfWidth:i,pdfHeight:s,position:p,currentPage:f,pageOfCurrentNode:d}=e;const{pageData:l,printWidth:m,printHeight:u}=function({canvas:t,pdf:e,pdfContentWidth:a,opts:n}){if(!t||!t.toDataURL)throw new Error("[jspdf-html2canvas] Invalid canvas element");const r=Math.max(0,Math.min(1,n.imageQuality)),o=t.toDataURL(n.imageType,r),i=e.getImageProperties(o);return{pageData:o,printWidth:n.autoResize?a:i.width/c,printHeight:n.autoResize?a/i.width*i.height:i.height/c}}({canvas:t,pdf:n,pdfContentWidth:r,opts:a});let h=u;p<0&&(n.addPage(),f+=1,d=1,p=0);const{margin:w}=a;for(;h>0;){if(n.addImage(l,g(a.imageType),w.left,p+w.top*d+w.bottom*(d-1),m,u),n.setFillColor(255,255,255),n.rect(0,0,i,w.top,"F"),n.rect(0,s-w.bottom,i,w.bottom,"F"),h<o){p-=h;break}h-=o,p-=s,n.addPage(),f+=1,d+=1}!function(t,e,a,n,r){t.pdf=e,t.position=a,t.currentPage=n,t.pageOfCurrentNode=r}(e,n,p,f,d)},m=async function(t,e){e.watermark&&await function(t,e){return new Promise((a,n)=>{const r=new Image,o="string"==typeof e.watermark?e.watermark:"function"!=typeof e.watermark&&e.watermark?.src,i=()=>{t.pdf=function(t,e){const a=t.internal.getNumberOfPages();if("function"==typeof e.watermark)for(let n=1;n<=a;n++){t.setPage(n);const r={pdf:t,pageNumber:n,totalPageNumber:a};e.watermark.call(e,r)}else if(e.watermarkImg){const n=t.getImageProperties(e.watermarkImg),r="string"!=typeof e.watermark&&e.watermark?.scale||1;for(let o=1;o<=a;o++)if(t.setPage(o),"string"!=typeof e.watermark&&e.watermark?.handler){const n={pdf:t,pageNumber:o,totalPageNumber:a,imgNode:e.watermarkImg};e.watermark.handler.call(e,n)}else t.addImage(e.watermarkImg,g("image/png"),(t.internal.pageSize.width-n.width*r)/2,(t.internal.pageSize.height-n.height*r)/2,n.width*r,n.height*r)}else console.warn('[jspdf-html2canvas] "watermark" option should be either "string" or "function" type.');return t}(t.pdf,e),a(null)};o?(r.onload=function(){e.watermarkImg=r,i()},r.onerror=function(t){console.error("[jspdf-html2canvas] Failed to load watermark image:",o),a(null)},r.crossOrigin="Anonymous",r.src=o):"function"==typeof e.watermark?i():a(null)})}(t,e)};function u(t){return null!==t&&"object"==typeof t}function h(t){return"function"==typeof t}function w(t,e){for(let a=0,n=Object.keys(t);a<n.length;a++){const r=n[a];if(r in e){const a=u(t[r]),n=u(e[r]);a&&n?h(t[r])&&h(e[r])?t[r]=e[r]:h(t[r])&&!h(e[r])?console.error(`[jspdf-html2canvas] config key "${r}" type invalid`):t[r]=w(t[r],e[r]):n?(e[r].old=t[r],t[r]=e[r]):a?t[r].new=e[r]:t[r]=e[r],delete e[r]}}return Object.assign(t,e)}const y=async function(t,e={}){const a=w(f,e),n=function(t){const{margin:e}=t,a=new i.jsPDF(t.jsPDF),n=a.internal.pageSize.getWidth(),r=a.internal.pageSize.getHeight();return{pdf:a,pdfWidth:n,pdfHeight:r,pdfContentWidth:Math.max(0,n-(e.left+e.right)),pdfContentHeight:Math.max(0,r-(e.top+e.bottom)),position:0,currentPage:1,pageOfCurrentNode:1}}(a);if(a.init.call(a,n.pdf),Array.isArray(t)||t&&"object"==typeof t&&"length"in t&&"number"==typeof t.length&&t.length>0){const e=Array.isArray(t)?t:Array.from(t);for(let t=0;t<e.length;t++){const r=await p()(e[t],a.html2canvas);l(r,n,a)}}else{const e=await p()(t,a.html2canvas);l(e,n,a)}return await m(n,a),a.success.call(a,n.pdf),n.pdf};return o.default})());