【问题标题】:How can I dynamically add page breaks in jspdf, to prevent tables from breaking across two pages?如何在 jspdf 中动态添加分页符,以防止表格跨越两页?
【发布时间】:2019-05-29 17:54:47
【问题描述】:

我正在尝试创建一个将数据库信息导出为 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>

【问题讨论】:

    标签: javascript jquery jspdf


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-11
      • 2022-12-12
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      相关资源
      最近更新 更多