使用javascript将HTML表格转为pdf

问题描述 投票:3回答:2

我用javascript创建了一个下载html表格到pdf的应用程序。jsPDF 插件。应用程序工作正常,但问题是表格不正确。表格的宽度以及页眉没有正确对齐。我使用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);
}
javascript html angularjs pdf jspdf
2个回答
4
投票

我想你目前必须手动操作(抚弄):

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');
}

0
投票

试试用 "jspdf "代替 "jsPDF"

© www.soinside.com 2019 - 2024. All rights reserved.