Search code examples
javascriptjqueryangularjsjspdfjspdf-autotable

Uncaught TypeError in jsPdf autotable


Am trying to print the dynamic data's into pdf file using jsPdf Auto-table . When am doing this i got some kind of error's like

The headers should be an object or array, is: function     (jspdf.plugin.autotable.js:10 )
The data should be an object or array, is: function        (jspdf.plugin.autotable.js:10)
TypeError: t.forEach is not a function     (angular.js:12314)

Here is my code -

var getColumns = function () {
    return [
        { title: "ID", dataKey: "id" },
        { title: "Name", dataKey: "first_name" },
        { title: "Email", dataKey: "email" },
        { title: "City", dataKey: "city" },
        { title: "Country", dataKey: "country" },
        { title: "Expenses", dataKey: "expenses" }
    ];
};

function getData(mainSteps) {
    mainSteps = mainSteps || 4;
    //var sentence = faker.lorem.words(12);
    var data = [];
    for (var j = 1; j <= rowCount; j++) {
        data.push({
            id: j,
            first_name: this.getSteps(),
            email: this.getSteps(),
            country: this.getSteps(),
            city: this.getSteps()(),
            expenses: this.getSteps()
            // text: shuffleSentence(sentence),
            //text2: shuffleSentence(sentence)
        });
    }
    return data;
}

var pdfsize = 'a4';
var doc = new jsPDF('p', 'pt', pdfsize);

doc.autoTable(getColumns, getData, {
    theme: 'grid', // 'striped', 'grid' or 'plain'
    headerStyles: {
        fillColor: [12, 234, 227],
        textColor: [12, 1, 1]
    },
    // margin: { top: 50, left: 20, right: 20, bottom: 0 },
    styles: {
        overflow: 'linebreak',
        columnWidth: 88
    },
    beforePageContent: function (data) {

        doc.text("Process Name :" + mainData.name + "  ||   " + "Description :" + mainData.description, 40, 30);

    },
    //startY: doc.autoTableEndPosY() + 20,
    columnStyles: {
        0: { columnWidth: 200 }
    }
});

//startY: doc.autoTableEndPosY() + 20
doc.save(mainData.name + ".pdf");

Note : If the error is in my code mean's i can find the solution for that, But it says error is in (jspdf.plugin.autotable.js:10 ) and (angular.js:12314) so am getting confused here. Can someone clarify me please.


Solution

  • As the errors explain the input data must be arrays or objects. In your case that simply means calling the functions instead of sending them to autotable. Replace this

    doc.autoTable(getColumns, getData, {...});

    with

    doc.autoTable(getColumns(), getData(), {...});