UNPKG

jspdf-html2canvas

Version:

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

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