【问题标题】:Jspdf v3 autotable colspan functionality not workingJspdf v3 autotable colspan 功能不起作用
【发布时间】:2019-08-22 10:42:16
【问题描述】:

我是 Angular 和 jspdf 的新手,所以请帮帮我。 我正在尝试使用 json 数据创建 pdf,并且我在 angular 6 中使用 jspdf 和 jspdf 自动表。我面临的问题是我无法将 colspan 应用于我的表头。我尝试了许多不同的代码,但没有运气。最后我使用了 jspdfautotable/example.js 的 github 上可用的代码,但标题仍然不符合预期。

以下是我提到的https://github.com/simonbengtsson/jsPDFAutoTable/blob/master/examples/examples.js

export(){
    // Col spans and row spans
    var doc = new jsPDF('p', 'pt');
    doc.setFontSize(12);
    doc.setTextColor(0);
    doc.setFontStyle('bold');
    doc.text('Rowspan and colspan', 40, 50);
    let body = this.bodyRows(15);
    for (var i = 0; i < body.length; i++) {
        var row = body[i];
        if (i % 5 === 0) {
        row['id'] = {rowSpan: 5, content: i / 5 + 1, styles: {valign: 'middle', halign: 'center'}};
        }
    }
    let head = [
    [
        {content: 'Data', colSpan: 5, styles: {halign: 'center', fillColor: [22, 160, 133]}},
    ],
        ['ID', 'Name', 'Email', 'City', 'Sum']
    ]
    doc.autoTable({
    startY: 60,
    head: head,
    body: body,
    theme: 'grid'
    });
    doc.save('TESTCOLSPAN.pdf');
    }

    bodyRows(rowCount) {
        rowCount = rowCount || 10;
        let body = [];
        for (var j = 1; j <= rowCount; j++) {
            body.push({
            id: j,
            name: 'PARTH',
            email: 'PARTH',
            city: 'PARTH',
            expenses: 'PARTH',
            });
        }
    return body;
}

输出pdf图片:

【问题讨论】:

    标签: angular jspdf jspdf-autotable


    【解决方案1】:

    而不是

    let head = [
        [
            {content: 'Data', colSpan: 5, styles: {halign: 'center', fillColor: [22, 160, 133]}},
        ],
            ['ID', 'Name', 'Email', 'City', 'Sum']
        ]
    

    请试试这个,

    let head = [
    
          {id:{content: 'Data', colSpan: 5, styles: {halign: 'center', fillColor: [22, 160, 133]}}},
    
          {id:'ID', name:'Name', email:'Email', city:'City', expenses:'Sum'}
    ]
    

    对我有用

    Output

    【讨论】:

    • 尝试了您的解决方案。没有运气。使用 "jspdf": "^1.5.3", "jspdf-autotable": "^2.3.2" 版本。错误类型错误:inputHeaders.forEach 不是 Object.jsPDF.API.autoTable (jspdf.plugin.autotable.js:2277) 的 Object.createModels (jspdf.plugin.autotable.js:1188) 的函数
    • 这是正确的调用方式 doc.autoTable(head,body{ startY: 60, theme: 'grid' });
    • 是的。该解决方案已尝试使用以下版本 "jspdf": "^1.3.5", "jspdf-autotable": "^3.1.1"
    • @Ruban 感谢您的回答,但根据我的 pdf 格式要求,我为上述问题找到了不同的解决方案。
    • @NikeshDevaki 当你做 rowspan 和 colspan 时你仍然面临同样的问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多