html2canvas
    jspdf

    1. // 邮件插入阿里信用名片和T/T账户信息
    2. import html2Canvas from'html2canvas'
    3. import JsPDF from'jspdf'
    4. // 将base64转换为文件对象
    5. constdataURLtoFile=(dataurl,filename)=>{
    6. letarr=dataurl.split(',')
    7. letmime=arr[0].match(/:(.*?);/)[1]
    8. letbstr=atob(arr[1])
    9. letn=bstr.length
    10. letu8arr=newUint8Array(n)
    11. while (n--) {
    12. u8arr[n] =bstr.charCodeAt(n)
    13. }
    14. // 转换成file对象
    15. returnnewFile([u8arr],filename,{type:mime})
    16. // 转换成成blob对象
    17. // return new Blob([u8arr],{type:mime});
    18. }
    19. // 导出页面为PDF格式
    20. exportdefaultfunctionhtmlToPdf(element,fileName,callback=null){
    21. html2Canvas(element,{
    22. allowTaint:true,// 允许污染
    23. scale:2// 画质
    24. }).then(function(canvas){
    25. letcontentWidth=canvas.width
    26. letcontentHeight=canvas.height
    27. letpageHeight=contentWidth/592.28*841.89
    28. letleftHeight=contentHeight
    29. letposition=0
    30. letimgWidth=595.28
    31. letimgHeight=592.28/contentWidth*contentHeight
    32. letpageData=canvas.toDataURL('image/jpeg',1.0)
    33. letPDF=newJsPDF('','pt','a4')
    34. if (leftHeight<pageHeight) {
    35. PDF.addImage(pageData,'JPEG',0,0,imgWidth,imgHeight)
    36. }else{
    37. while (leftHeight>0) {
    38. PDF.addImage(pageData,'JPEG',0,position,imgWidth,imgHeight)
    39. leftHeight-=pageHeight
    40. position-=841.89
    41. if (leftHeight>0) {
    42. PDF.addPage()
    43. }
    44. }
    45. }
    46. // 将pdf输入为base格式的字符串
    47. letbuffer=PDF.output('datauristring')
    48. // 将base64格式的字符串转换为file文件
    49. letmyfile=dataURLtoFile(buffer,fileName+'.pdf')
    50. letfilelist= [myfile]
    51. callback&&callback(filelist)
    52. // PDF.save(fileName + '.pdf') // 保存本地pdf
    53. })
    54. }
    55. exportfunctionhtmlToPdfSave(element,fileName,callback=null){
    56. html2Canvas(element,{
    57. allowTaint:true,// 允许污染
    58. scale:2// 画质
    59. }).then(function(canvas){
    60. letcontentWidth=canvas.width
    61. letcontentHeight=canvas.height
    62. letpageHeight=contentWidth/592.28*841.89
    63. letleftHeight=contentHeight
    64. letposition=0
    65. letimgWidth=595.28
    66. letimgHeight=592.28/contentWidth*contentHeight
    67. letpageData=canvas.toDataURL('image/jpeg',1.0)
    68. letPDF=newJsPDF('','pt','a4')
    69. if (leftHeight<pageHeight) {
    70. PDF.addImage(pageData,'JPEG',0,0,imgWidth,imgHeight)
    71. }else{
    72. while (leftHeight>0) {
    73. PDF.addImage(pageData,'JPEG',0,position,imgWidth,imgHeight)
    74. leftHeight-=pageHeight
    75. position-=841.89
    76. if (leftHeight>0) {
    77. PDF.addPage()
    78. }
    79. }
    80. }
    81. // 将pdf输入为base格式的字符串
    82. letbuffer=PDF.output('datauristring')
    83. // 将base64格式的字符串转换为file文件
    84. letmyfile=dataURLtoFile(buffer,fileName+'.pdf')
    85. letfilelist= [myfile]
    86. PDF.save(fileName+'.pdf') // 保存本地pdf
    87. })
    88. }