7

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?

DavidDunham
  • 1,245
  • 1
  • 22
  • 44

2 Answers2

4

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';
    }
}
Simon Bengtsson
  • 7,573
  • 3
  • 58
  • 87
DavidDunham
  • 1,245
  • 1
  • 22
  • 44
3

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

Stack Underflow
  • 2,363
  • 2
  • 26
  • 51
  • Still. Heading is not aligned with this ColumnStyle. I got solution from there, https://stackoverflow.com/questions/56477890/how-to-align-the-header-of-the-pdf-to-the-center-in-jspdf-autotable – Jeya Suriya Muthumari Mar 23 '22 at 05:57