export.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const loadJspdf = () => {
  2. return new Promise((resolve, reject) => {
  3. if (window.FormMaking_OPTIONS.jsPDF) {
  4. resolve(window.FormMaking_OPTIONS.jsPDF)
  5. } else if (window.jspdf) {
  6. resolve(window.jspdf.jsPDF)
  7. } else {
  8. reject('jsPDF not found')
  9. }
  10. })
  11. }
  12. const loadHtml2canvas = () => {
  13. return new Promise((resolve, reject) => {
  14. if (window.FormMaking_OPTIONS.html2canvas) {
  15. resolve(window.FormMaking_OPTIONS.html2canvas)
  16. } else if (window.html2canvas) {
  17. resolve(window.html2canvas)
  18. } else {
  19. reject('html2canvas not found')
  20. }
  21. })
  22. }
  23. export const exportPDF = (content) => {
  24. return new Promise((resolve, reject) => {
  25. loadJspdf().then((jsPDF) => {
  26. loadHtml2canvas().then(html2canvas => {
  27. const pdf = new jsPDF('p', 'pt', 'a4')
  28. html2canvas(content, {
  29. dpi: 300,
  30. scale: 4,
  31. allowTaint: true,
  32. useCORS: true
  33. }).then((canvas) => {
  34. let context = canvas.getContext('2d')
  35. context.mozImageSmoothingEnabled = false
  36. context.webkitImageSmoothingEnabled = false
  37. context.msImageSmoothingEnabled = false
  38. context.imageSmoothingEnabled = false
  39. const marginLeft = 20;
  40. const marginRight = 20;
  41. const marginTop = 20;
  42. const marginBottom = 30;
  43. const pageWidth = pdf.internal.pageSize.getWidth();
  44. const pageHeight = pdf.internal.pageSize.getHeight();
  45. const contentWidth = pageWidth - marginLeft - marginRight;
  46. const contentHeight = pageHeight - marginTop - marginBottom;
  47. const imgWidth = contentWidth
  48. const imgHeight = (canvas.height * imgWidth) / canvas.width
  49. let leftHeight = imgHeight
  50. let position = marginTop
  51. let currentPage = 1
  52. if (leftHeight < contentHeight) {
  53. let imgData = canvas.toDataURL('image/jpeg')
  54. pdf.addImage(imgData, 'JPEG', marginLeft, marginRight, imgWidth, imgHeight)
  55. } else {
  56. while (leftHeight > 0) {
  57. let croppWidth = canvas.width
  58. let croppHeight = (croppWidth * contentHeight) / contentWidth
  59. const croppedCanvas = document.createElement('canvas');
  60. const ctx = croppedCanvas.getContext('2d');
  61. ctx.mozImageSmoothingEnabled = false
  62. ctx.webkitImageSmoothingEnabled = false
  63. ctx.msImageSmoothingEnabled = false
  64. ctx.imageSmoothingEnabled = false
  65. croppedCanvas.width = croppWidth;
  66. croppedCanvas.height = croppHeight;
  67. let remainHeight = canvas.height - (currentPage - 1) * croppHeight
  68. if (remainHeight < croppHeight) {
  69. croppedCanvas.height = remainHeight
  70. ctx.drawImage(canvas, 0, (currentPage - 1) * croppHeight, canvas.width, remainHeight, 0, 0, croppWidth, remainHeight)
  71. let imgData = croppedCanvas.toDataURL('image/jpeg')
  72. pdf.addImage(imgData, 'JPEG', marginLeft, marginTop, contentWidth, (contentHeight / croppHeight) * remainHeight)
  73. } else {
  74. ctx.drawImage(canvas, 0, (currentPage - 1) * croppHeight, canvas.width, croppHeight, 0, 0, croppWidth, croppHeight)
  75. let imgData = croppedCanvas.toDataURL('image/jpeg')
  76. pdf.addImage(imgData, 'JPEG', marginLeft, marginTop, contentWidth, contentHeight)
  77. }
  78. leftHeight -= contentHeight
  79. position -= contentHeight
  80. if (leftHeight > 0) {
  81. pdf.addPage()
  82. currentPage++
  83. }
  84. }
  85. }
  86. const footerText = 'Powered by FormMaking - https://form.making.link'
  87. const totalPages = pdf.internal.getNumberOfPages()
  88. for (let i = 1; i <= totalPages; i++) {
  89. pdf.setPage(i); // 设置当前页
  90. // 添加页脚
  91. pdf.setTextColor(144, 147, 153)
  92. pdf.setFontSize(10);
  93. pdf.text(footerText, pageWidth - 247, pageHeight - 20);
  94. }
  95. resolve(pdf.output('blob'))
  96. }).catch(() => {
  97. reject()
  98. })
  99. }).catch(error => reject(error))
  100. }).catch(error => reject(error))
  101. })
  102. }