htmlToPdf.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // 页面导出为pdf格式 //title表示为下载的标题,html表示document.querySelector('#myPrintHtml')
  2. import html2Canvas from 'html2canvas'
  3. import JsPDF from 'jspdf'
  4. const htmlPdf = {
  5. getPdf(title, html) {
  6. html2Canvas(html, {
  7. allowTaint: true,
  8. useCORS: true,
  9. dpi: window.devicePixelRatio * 4, // 将分辨率提高到特定的DPI 提高四倍
  10. background: '#FFFFFF',
  11. }).then(canvas => {
  12. //未生成pdf的html页面高度
  13. var leftHeight = canvas.height
  14. var a4Width = 595.28
  15. var a4Height = 841.89 //A4大小,210mm x 297mm,四边各保留10mm的边距,显示区域190x277
  16. //一页pdf显示html页面生成的canvas高度;
  17. var a4HeightRef = Math.floor((canvas.width / a4Width) * a4Height)
  18. //pdf页面偏移
  19. var position = 0
  20. var pageData = canvas.toDataURL('image/jpeg', 1.0)
  21. var pdf = new JsPDF('p', 'pt', 'a4') //A4纸,纵向
  22. var index = 1,
  23. canvas1 = document.createElement('canvas'),
  24. height
  25. pdf.setDisplayMode('fullwidth', 'continuous', 'FullScreen')
  26. var pdfName = title
  27. function createImpl(canvas) {
  28. console.log(leftHeight, a4HeightRef)
  29. if (leftHeight > 0) {
  30. index++
  31. var checkCount = 0
  32. if (leftHeight > a4HeightRef) {
  33. var i = position + a4HeightRef
  34. for (i = position + a4HeightRef; i >= position; i--) {
  35. var isWrite = true
  36. for (var j = 0; j < canvas.width; j++) {
  37. var c = canvas.getContext('2d').getImageData(j, i, 1, 1).data
  38. if (c[0] != 0xff || c[1] != 0xff || c[2] != 0xff) {
  39. isWrite = false
  40. break
  41. }
  42. }
  43. if (isWrite) {
  44. checkCount++
  45. if (checkCount >= 10) {
  46. break
  47. }
  48. } else {
  49. checkCount = 0
  50. }
  51. }
  52. height = Math.round(i - position) || Math.min(leftHeight, a4HeightRef)
  53. if (height <= 0) {
  54. height = a4HeightRef
  55. }
  56. } else {
  57. height = leftHeight
  58. }
  59. canvas1.width = canvas.width
  60. canvas1.height = height
  61. console.log(index, 'height:', height, 'pos', position)
  62. var ctx = canvas1.getContext('2d')
  63. ctx.drawImage(
  64. canvas,
  65. 0,
  66. position,
  67. canvas.width,
  68. height,
  69. 0,
  70. 0,
  71. canvas.width,
  72. height,
  73. )
  74. var pageHeight = Math.round((a4Width / canvas.width) * height)
  75. // pdf.setPageSize(null, pageHeight)
  76. if (position != 0) {
  77. pdf.addPage()
  78. }
  79. pdf.addImage(
  80. canvas1.toDataURL('image/jpeg', 1.0),
  81. 'JPEG',
  82. 10,
  83. 10,
  84. a4Width,
  85. (a4Width / canvas1.width) * height,
  86. )
  87. leftHeight -= height
  88. position += height
  89. if (leftHeight > 0) {
  90. setTimeout(createImpl, 500, canvas)
  91. } else {
  92. pdf.save(title + '.pdf')
  93. }
  94. }
  95. }
  96. //当内容未超过pdf一页显示的范围,无需分页
  97. if (leftHeight < a4HeightRef) {
  98. pdf.addImage(
  99. pageData,
  100. 'JPEG',
  101. 0,
  102. 0,
  103. a4Width,
  104. (a4Width / canvas.width) * leftHeight,
  105. )
  106. pdf.save(title + '.pdf')
  107. } else {
  108. try {
  109. pdf.deletePage(0)
  110. setTimeout(createImpl, 500, canvas)
  111. } catch (err) {
  112. // console.log(err);
  113. }
  114. }
  115. })
  116. }
  117. }
  118. export default htmlPdf