【问题标题】:Uncaught TypeError in jsPdf autotablejsPdf自动表中未捕获的TypeError
【发布时间】:2016-08-01 12:11:59
【问题描述】:

我正在尝试使用 jsPdf Auto-table 将动态数据打印到 pdf 文件中。当我这样做时,我遇到了某种错误,比如

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)

这是我的代码 -

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");

注意:如果错误在我的代码中意味着我可以找到解决方案,但它说 错误在 (jspdf.plugin.autotable.js:10)( angular.js:12314) 所以我在这里感到困惑。有人可以澄清一下吗?

【问题讨论】:

    标签: javascript jquery angularjs jspdf jspdf-autotable


    【解决方案1】:

    正如错误所解释的,输入数据必须是数组或对象。在您的情况下,这仅意味着调用函数而不是将它们发送到 autotable。替换这个

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

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

    【讨论】:

    • 是的,你是对的。那是我的“粗心”错误:(
    • ,嘿嘿..我有一个小疑问。我正在尝试应用自定义宽度列。所以我在你的 github 网站上按照你的指示进行了操作。但是在申请之后,似乎我没有为我工作。你能帮我吗?这是我的代码 --> {title: "Category", dataKey: "cate",width: 20} ,
    • 或者有没有其他方法可以做到这一点?
    • 您在哪里看到提到了宽度选项? github 页面应指向 columnWidth 样式,其使用如下:doc.autoTable(columns, rows, {columnStyles: {cate: {columnWidth: 20}}})
    • 我也试过这种方式 --> columnStyles: {cate: {columnWidth: 200}}但它说的是----> (jspdf.plugin.autotable.js:10 )使用已弃用选项:column.width,使用 column.styles.columnWidth 代替
    猜你喜欢
    • 2017-09-06
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2022-10-19
    • 1970-01-01
    相关资源
    最近更新 更多