(已解决)如何在jspdf中动态添加分页符,以防止表在两页间分页?

时间:2019-05-29 17:54:48

标签: php jquery mysql jspdf

我试图创建一个网页,将数据库信息导出为pdf,并使用php和Mysql从数据库中提取表。

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

我的问题是这个;如果初始表将在页面上占用大量空间,第二个表将分解到后续页面,那么我如何告诉jspdf返回上一个标记,并在插入-page break-之前它? (,即,如果表2+的任何行会中断并移至下一页,请将整个表移至下一页)。

我对jspdf库仍然很陌生,我不知道这个库有多聪明或装备多么精良。

我已经考虑过尝试对标记进行爬网和计数以获得计数,并试图设置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>

1 个答案:

答案 0 :(得分:0)

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