Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsPDF autotable right align x position bug

My example (just click "export PDF"): https://jsfiddle.net/j9vaqpnz/7/

My example exports my table which looks like this:

enter image description here .

The table then is exported to pdf using libraries jspdf and autotable.

During the export function I use the "drawCell" function and for all columns which contain a number i right-align them as follows:

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

.

Problem: In the PDF all the columns which I have right-aligned are positioned inproperly, it looks like this:

enter image description here

Is this a bug? Or maybe I am using "drawCell" inproperly?

like image 248
DavidDunham Avatar asked Jan 30 '17 10:01

DavidDunham


2 Answers

When using "didParseCell" (v3.x) the right align positions the elements properly.

Updated example: https://jsfiddle.net/j9vaqpnz/10/

New Code:

...
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';
    }
}
like image 145
DavidDunham Avatar answered Oct 08 '22 08:10

DavidDunham


You can align cells using the columnStyles property

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

like image 29
Stack Underflow Avatar answered Oct 08 '22 09:10

Stack Underflow