html2canvas
jspdf
// 邮件插入阿里信用名片和T/T账户信息import html2Canvas from'html2canvas'import JsPDF from'jspdf'// 将base64转换为文件对象constdataURLtoFile=(dataurl,filename)=>{letarr=dataurl.split(',')letmime=arr[0].match(/:(.*?);/)[1]letbstr=atob(arr[1])letn=bstr.lengthletu8arr=newUint8Array(n)while (n--) {u8arr[n] =bstr.charCodeAt(n)}// 转换成file对象returnnewFile([u8arr],filename,{type:mime})// 转换成成blob对象// return new Blob([u8arr],{type:mime});}// 导出页面为PDF格式exportdefaultfunctionhtmlToPdf(element,fileName,callback=null){html2Canvas(element,{allowTaint:true,// 允许污染scale:2// 画质}).then(function(canvas){letcontentWidth=canvas.widthletcontentHeight=canvas.heightletpageHeight=contentWidth/592.28*841.89letleftHeight=contentHeightletposition=0letimgWidth=595.28letimgHeight=592.28/contentWidth*contentHeightletpageData=canvas.toDataURL('image/jpeg',1.0)letPDF=newJsPDF('','pt','a4')if (leftHeight<pageHeight) {PDF.addImage(pageData,'JPEG',0,0,imgWidth,imgHeight)}else{while (leftHeight>0) {PDF.addImage(pageData,'JPEG',0,position,imgWidth,imgHeight)leftHeight-=pageHeightposition-=841.89if (leftHeight>0) {PDF.addPage()}}}// 将pdf输入为base格式的字符串letbuffer=PDF.output('datauristring')// 将base64格式的字符串转换为file文件letmyfile=dataURLtoFile(buffer,fileName+'.pdf')letfilelist= [myfile]callback&&callback(filelist)// PDF.save(fileName + '.pdf') // 保存本地pdf})}exportfunctionhtmlToPdfSave(element,fileName,callback=null){html2Canvas(element,{allowTaint:true,// 允许污染scale:2// 画质}).then(function(canvas){letcontentWidth=canvas.widthletcontentHeight=canvas.heightletpageHeight=contentWidth/592.28*841.89letleftHeight=contentHeightletposition=0letimgWidth=595.28letimgHeight=592.28/contentWidth*contentHeightletpageData=canvas.toDataURL('image/jpeg',1.0)letPDF=newJsPDF('','pt','a4')if (leftHeight<pageHeight) {PDF.addImage(pageData,'JPEG',0,0,imgWidth,imgHeight)}else{while (leftHeight>0) {PDF.addImage(pageData,'JPEG',0,position,imgWidth,imgHeight)leftHeight-=pageHeightposition-=841.89if (leftHeight>0) {PDF.addPage()}}}// 将pdf输入为base格式的字符串letbuffer=PDF.output('datauristring')// 将base64格式的字符串转换为file文件letmyfile=dataURLtoFile(buffer,fileName+'.pdf')letfilelist= [myfile]PDF.save(fileName+'.pdf') // 保存本地pdf})}
