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

How can I dynamically add page breaks in jspdf, to prevent tables from breaking across two pages?

我正在尝试创建一个将数据库信息导出到 PDF 的网页,其中 tables 从具有 PHP 和 MySQL 的数据库中提取。

我的 table 具有 可变长度 ,并且有时可以包括多个 table,可能多达 3+ tables.

如果第一个table会在一个页面上占据大量space,而第二个table会突破到后续页面,我该怎么办告诉 jspdf 返回到之前的 <table> 标签,并在它之前插入一个分页符? (即,如果 table 2+ 的任何一行会中断并移至下一页,则将整个 table 移至下一页)。

我考虑过尝试抓取并计算 <tr> 标签以获得计数,并尝试设置一个 if-else 语句将 table 发送到下一页,但我不知道两个以上的 table 是如何工作的。

<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 autotable 插件。 https://github.com/simonbengtsson/jsPDF-AutoTable 并查看多个表的示例