如何在 jspdf 中动态添加分页符,以防止表格跨两页断开



我正在尝试创建一个将数据库信息导出为PDF的网页,其中包含从PHP和MySQL数据库中提取的表格。

我的表具有可变长度,有时可以包含多个表,可能最多 3+ 个表

如果初始表将占用页面上的大量空间,第二个表将中断到后续页面,我将如何告诉 jspdf 返回到上一个<table>标记,并在它之前插入一个 -page break- ?(即,如果表 2+ 的任何行会中断并移动到下一页,请将整个表格移动到下一页(。

我已经考虑过尝试抓取和计数 <tr> 标签以获取计数,并尝试设置 if-else 语句以将表发送到下一页,但我不知道这将如何适用于两个以上的表。

<script>
  //this my current idea of the code, none of it is implemented or tested
  $(document).on('load', function(e) { //when the pages loads, execute this function
    var PDF_WIDTH = 816;
    var PDF_HEIGHT = (PDF_Width * 1.2941); //for a 8.5*11 Letter sized pdf
    var rowCount = 0;
    var tableCount = 0;
    $(tr).each(function(e) { //counts all the rows that exists on the page
      var rowCount++;
    });
    $(table).each(function(e) { //counts all the tables that exist on the page
      var tableCount++;
    });
    if (rowcount > 36) { //if the total number of rows would exceed the amount of rows displayed on the page
      var pdf = new jsPDF('p', 'pt' [PDF_WIDTH, PDF_HEIGHT]);
      //??? trying to select the table that would break 
      //??? add a page break before the second table
      //alternatively append a page break after the first table
    }
  });
</script>

你应该尝试JSPDF自动表插件。https://github.com/simonbengtsson/jsPDF-AutoTable并查看多个表的示例

我处于同样的情况。将"行分页符"选项设置为"避免"和

  • 表拆分
  • 添加了新页面
  • 多余的行将流向新页面

最新更新