How to pass data through JSON to jsPDF autotable

3.9k views Asked by At
download() {
    var columns = [
        { title: "Producto", dataKey: "producto" },
        { title: "Fecha", dataKey: "fecha" }
    ];
    var data = [{ "producto": "ddf", "fecha": "cwx" }];
    var doc = new jsPDF('p', 'pt');
    console.log("1", data);
    console.log("2", columns);
    doc.autoTable(columns, data);
    doc.save('table.pdf');            
    //console.log("3", data);
    //this.PdfServiceService.getAll().subscribe(c => {
    //    console.log("4", this.country = c.pdfdata)
    //    this.data = this.country;
    //});  
};

<button (click)="download()">Generate PDF</button>

I want to pass data (var data) through JSON data. I'm not able to pass the data into the doc auto table. This is the link I have tried: https://www.npmjs.com/package/jspdf-autotable

1

There are 1 answers

1
Yuvraj Akhade On

Just add values in data and use array of arrays [ [],[]] and it will work.

var columns = ['id', 'Department', 'Joiners', 'Resignations', 'Terminations']

var data= [[1, 'IT',2, 1,0],[2, 'Comp', 1, 0, 0]]