使用javascript将HTML表转换为pdf

HTML table to pdf using javascript

本文关键字:转换 pdf HTML javascript 使用      更新时间:2023-09-26

我创建了一个应用程序,用于使用我使用的jsPDF插件将html表下载到pdf。应用程序运行良好,但问题是表不正确。表格的宽度和标题没有正确对齐。我正在使用angularjs创建表格。

有人能为这个问题给我一些建议吗?

JSFiddle

function demoFromHTML() {
    var pdf = new jsPDF('p', 'pt', 'letter');
    source = $('#customers')[0];
    specialElementHandlers = {
        '#bypassme': function (element, renderer) {
            return true
        }
    };
    margins = {
        top: 4,
        bottom: 4,
        left: 4,
        width: 522
    };
    pdf.fromHTML(
    source, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },
    function (dispose) {
        pdf.save('Test.pdf');
    }, margins);
}

我想你现在必须手动(fiddle):

function demoFromHTML() {
    var pdf = new jsPDF('p', 'pt', 'letter');
    pdf.cellInitialize();
    pdf.setFontSize(10);
    $.each( $('#customers tr'), function (i, row){
        $.each( $(row).find("td, th"), function(j, cell){
            var txt = $(cell).text().trim() || " ";
            var width = (j==4) ? 40 : 70; //make 4th column smaller
            pdf.cell(10, 50, width, 30, txt, i);
        });
    });
    pdf.save('sample-file.pdf');
}

尝试"jspdf"而不是"jspdf"