【问题标题】:How to align the header of the PDF to the center in jspdf-autotable?如何将 PDF 的标题与 jspdf-autotable 中的中心对齐?
【发布时间】:2019-06-06 12:44:41
【问题描述】:

我必须将 pdf 的标题放在中间。我尝试了很多方法,但找不到可行的方法。

我正在使用 jspdf 和 jspdf-autotable 包使用 reactjs 形成 pdf。

var options = {
  didDrawPage: function (data) {
            // Header
            doc.text(heading, 20, 10,{
                    halign: 'center',
                    valign: 'middle'
                });
        }
};
doc.autoTable(columns, rows, options);
doc.save('table.pdf');

上面的代码对我不起作用。

【问题讨论】:

    标签: reactjs jspdf jspdf-autotable


    【解决方案1】:

    试试这个。

    var columns = [...];
    var rows = [...];
    var options = {
      headStyles:{
        valign: 'middle',
        halign : 'center'
      }
    };
    doc.autoTable(columns, rows, options);
    doc.save('table.pdf');
    

    【讨论】:

    • jspdf-autotable中如何让PDF的特定标题栏居中对齐?我希望我的第 7 位标题列右对齐,其他与以前相同
    【解决方案2】:

    要对齐特定的标题列:

    • 您必须在头部定义中添加列名
    • 添加函数钩子didParseCell
    const head = [
        {
            code: 'Code',// <- here
            qty: 'Qty',
        }
    ];
    const doc = new jsPDF();
    autoTable(doc, {
        head: head,
        body: someData,
        didParseCell: (hookData) => {
            if (hookData.section === 'head') {
                if (hookData.column.dataKey === 'qty') {
                    hookData.cell.styles.halign = 'right';
                }
            }
        }
    });
    doc.save('fileName.pdf');
    

    【讨论】:

    • 太棒了。这行得通!太好了!!
    • 太棒了。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2016-05-24
    • 2016-02-04
    • 2018-04-16
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多