jsPDF自动表格右对齐X位置错误问题

7

我举的例子(点击“导出 PDF”即可查看)如下:https://jsfiddle.net/j9vaqpnz/7/

我的表格样式如下:

enter image description here .

然后使用库jspdfautotable将表格导出为 PDF。

在导出过程中,我使用“drawCell”函数,并将所有包含数字的列向右对齐,方法如下:

drawCell: function (cell, data) {
                var col = data.column.index;
                if(col==3 || col==5 || col==6 || col==7 || col==8 || col==9 || col==10){
                    cell.styles.halign = 'right';
                }
            }

问题:在PDF中,我将所有右对齐的列都定位不当,显示如下:enter image description here。这是一个错误吗?或者我可能没有正确使用“drawCell”?

尝试使用createdCell而不是drawCell。 - Simon Bengtsson
谢谢Simon。我已经发布了下面的更新 - 工作 - 示例,供其他人参考。 - DavidDunham
2个回答

4

使用“didParseCell”(v3.x)时,右对齐可以正确地定位元素。

更新示例:https://jsfiddle.net/j9vaqpnz/10/

新代码:

...
didParseCell: function (cell, data) {
    alignCol(cell, data);
}
...

function alignCol(data){
    var col = data.column.index;
    if(col==3 || col==5 || col==6 || col==7 || col==8 || col==9 || col==10){
        data.cell.styles.halign = 'right';
    }
}

jsfiddle正在使用createdHeaderCellcreatedCell,它们能够正常工作。 - Lahiru Chandima

3
您可以使用columnStyles属性来对齐单元格。
const pdf = new jsPDF();

pdf.autoTable({

    ...

    columnStyles: {
        3: {
            halign: 'right'
        },
        5: {
            halign: 'right'
        },
        6: {
            halign: 'right'
        },
        7: {
            halign: 'right'
        },
        8: {
            halign: 'right'
        },
        9: {
            halign: 'right'
        },
        10: {
            halign: 'right'
        }
    }
});

jsPDF-AutoTable documentation


仍然存在问题,标题与此ColumnStyle不对齐。我从这里得到了解决方案:https://stackoverflow.com/questions/56477890/how-to-align-the-header-of-the-pdf-to-the-center-in-jspdf-autotable - Jeya Suriya Muthumari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接