• 使用html2canvas将html转pdf,由于table表的水平和竖直有滚动条导致显示不全(或者有空白)


    结果:

    业务:将页面右侧的table打印成想要的格式的pdf,首先遇到的问题是table表上下左右都有滚轮而html2canvas相当于屏幕截图,那滚动区域如何显示出来是个问题?

    gif有点模糊,但是大致功能可以看出

    可复制代码在最下面

    参考文章:主要思路就是table既然原始高度宽度不对,那你在转pdf之前就把他的宽度高度还原成真实的高度宽度然后打印成pdf,最后再转成原始高度宽度。(值得注意的是画布的高度宽度也要设置,和table一样就行)

    html代码:

    script代码

    table表上有个“导出按钮”,点击就会触发handleExport函数

    全部代码:

    html:

    1. <div
    2. ref="myContainer"
    3. id="fatherDiv"
    4. class="demo-form-inline"
    5. style="height: calc(100% - 120px)"
    6. >
    7. <el-table
    8. ref="workforceTable"
    9. :data="tableData"
    10. border
    11. @drop.native="drop($event)"
    12. @dragover.native="allowDrop($event)"
    13. stripe
    14. :span-method="objectSpanMethod"
    15. :cell-class-name="tableCellClassName"
    16. max-height="100%"
    17. height="100%"
    18. class="demo-form-inline"
    19. id="factTable"
    20. >
    21. <el-table-column
    22. prop="time"
    23. label="时间"
    24. width="70"
    25. align="center"
    26. fixed
    27. >el-table-column>
    28. <el-table-column
    29. :prop="item.sectorCode"
    30. :label="item.sectorName"
    31. v-for="item in sectorList"
    32. :key="item.sectorCode"
    33. align="center"
    34. >
    35. <el-table-column
    36. :prop="seat.seatId"
    37. :label="seat.seatName"
    38. v-for="seat in item.seatList"
    39. :key="seat.seatId"
    40. align="center"
    41. min-width="102px"
    42. >
    43. <template slot-scope="scope">
    44. <span
    45. :defProp="seat.seatId"
    46. :defTime="scope.row.time"
    47. :defIndex="scope.$index"
    48. >span>
    49. <span
    50. class="el-tag el-tag--light"
    51. defid="scope.row[seat.seatId]"
    52. v-if="scope.row[seat.seatId]"
    53. >
    54. {{ scope.row[seat.seatId] }}
    55. <i
    56. class="el-tag__close el-icon-close"
    57. @click="onRemovePerson(scope.$index, seat.seatId)"
    58. v-if="isManual"
    59. >i>
    60. span>
    61. template>
    62. el-table-column>
    63. el-table-column>
    64. <el-table-column
    65. :prop="seat.seatId"
    66. :label="seat.seatName"
    67. v-for="seat in seatList"
    68. :key="seat.seatId"
    69. align="center"
    70. min-width="102px"
    71. >
    72. <template slot-scope="scope">
    73. <span
    74. :defProp="seat.seatId"
    75. :defTime="scope.row.time"
    76. :defIndex="scope.$index"
    77. >span>
    78. <span
    79. class="el-tag el-tag--light"
    80. defid="scope.row[seat.seatId]"
    81. v-if="scope.row[seat.seatId]"
    82. >
    83. {{ scope.row[seat.seatId] }}
    84. <i
    85. class="el-tag__close el-icon-close"
    86. @click="onRemovePerson(scope.$index, seat.seatId)"
    87. v-if="isManual"
    88. >i>
    89. span>
    90. template>
    91. el-table-column>
    92. el-table>
    93. div>

    script代码:

    1. handleExport() {
    2. this.$nextTick(() => {
    3. let pdfName =
    4. (this.deptRegion == "TWR"
    5. ? "塔台"
    6. : this.deptRegion == "APP"
    7. ? "进近"
    8. : "区域") +
    9. "管制室" +
    10. this.$common.parseTime(this.selectedMonth, "{y}/{m}/{d}") +
    11. "日排班表";
    12. document
    13. .getElementsByClassName("demo-form-inline")[0]
    14. .classList.add("export-pdf-style");
    15. this.generatePDF(this.$refs.myContainer, pdfName);
    16. });
    17. },
    18. generatePDF(el, name) {
    19. let bodyWrapper = document.querySelector(
    20. "#factTable .el-table__body-wrapper"
    21. );
    22. let headerNode = document.querySelector(
    23. "#factTable .el-table__header-wrapper"
    24. );
    25. bodyWrapper.style.height = `${bodyWrapper.scrollHeight}px`;
    26. document.getElementById("fatherDiv").style.width = `${bodyWrapper.scrollWidth}px`;
    27. document.getElementById("fatherDiv").style.height = `${bodyWrapper.scrollHeight + headerNode.scrollHeight不}px`;
    28. setTimeout(function () {
    29. html2canvas(el, {
    30. scale: 4,
    31. width: bodyWrapper.scrollWidth + 60, // 为了使横向滚动条的内容全部展示,这里必须指定!!
    32. height: bodyWrapper.scrollHeight + headerNode.scrollHeight ,
    33. }).then((canvas) => {
    34. let contentWidth = canvas.width;
    35. let contentHeight = canvas.height;
    36. let pageHeight = (contentWidth / 592.28) * 841.89; //一页pdf显示html页面生成的canvas高度;
    37. let leftHeight = contentHeight; //未生成pdf的html页面高度
    38. let position = 0; //页面偏移
    39. let imgWidth = 595.28; //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
    40. let imgHeight = (592.28 / contentWidth) * contentHeight;
    41. let pageData = canvas.toDataURL("image/jpeg", 1.0);
    42. let pdf = new jsPDF("", "pt", "a4");
    43. if (leftHeight < pageHeight) { //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
    44. pdf.addImage(pageData, "JPEG", 5, 30, imgWidth, imgHeight); //当内容未超过pdf一页显示的范围,无需分页
    45. } else {
    46. while (leftHeight > 0) {
    47. pdf.addImage(pageData, "JPEG", 5, position, imgWidth, imgHeight); //arg3-->距离左边距;arg4-->距离上边距;arg5-->宽度;arg6-->高度
    48. leftHeight -= pageHeight;
    49. position -= 841.89;
    50. if (leftHeight > 0) { //避免添加空白页
    51. pdf.addPage(); //添加新页
    52. }
    53. }
    54. }
    55. pdf.save(`${name}.pdf`);
    56. document.getElementById("fatherDiv").style.width = `${100}%`;
    57. document.getElementById("fatherDiv").style.height = `calc(100% - 120px)`;
    58. document.getElementsByClassName("demo-form-inline")[0].classList.remove("export-pdf-style");
    59. });
    60. }, 200);
    61. },

    css代码:

    1. .export-pdf-style >>> .el-table ,
    2. .export-pdf-style >>> .el-tag{
    3. background: white !important;
    4. color: black !important;
    5. }
    6. .export-pdf-style >>> .el-table th,
    7. .export-pdf-style >>> .el-table .el-table__cell {
    8. color: black;
    9. border: 1px solid black;
    10. background: white !important;
    11. }

  • 相关阅读:
    centos7虚拟机部署苍穹私有云环境记录
    世界第一ERP厂商SAP,推出类ChatGPT产品—Joule
    基于STM32的设计智慧超市管理系统(带收银系统+物联网环境监测)
    MySQL事务管理
    IDEA 好用的插件
    LCM Sum (hard version)(树状数组,筛因子)
    CSS(二)css选择器+css背景
    leetcode 20. Valid Parentheses 有效的括号(中等)
    对话腾讯天琴赵伟峰:当音乐与科技结合,会碰撞出怎样的火花?
    leetcode第362场周赛
  • 原文地址:https://blog.csdn.net/qq_42192641/article/details/133853249