使用jspdf将内容导出到pdf angular

时间:2019-06-20 05:42:37

标签: javascript angular typescript jspdf jspdf-autotable

我正在尝试使用jspdf创建pdf。但是我遇到了错误cannot create property 'header' on string 'Details'。我遇到了这个问题How to fix: Cannot create property 'header' on string,但找不到解决方案。请提供帮助。谢谢。 这是我的转换功能

convert() {

    var doc = new jsPDF();
    var col = ["Details", "Values"];
    var rows = [];

    /* The following array of object as response from the API req  */

    var itemNew = [
      { id: 'Case Number', name: '101111111' },
      { id: 'Patient Name', name: 'UAT DR' },
      { id: 'Hospital Name', name: 'Dr Abcd' }
    ];


    itemNew.forEach(element => {
      var temp = [element.id, element.name];
      rows.push(temp);

    });

    doc.autoTable(col, rows);
    doc.save('Test.pdf');
  }

StackBLitz:https://stackblitz.com/edit/angular-jspdf-xgoetc?file=app/app.component.ts

1 个答案:

答案 0 :(得分:0)

问题出在autoTable()函数中,您在其中指定headbody

var col = [["Details", "Values"]];

autoTable()一样

 doc.autoTable({
        head: col,
        body: rows
    });

更新的代码段:

convert() {

    var doc = new jsPDF();
    var col = [["Details", "Values"]];
    var rows = [];

    /* The following array of object as response from the API req  */

    var itemNew = [
      { id: 'Case Number', name: '101111111' },
      { id: 'Patient Name', name: 'UAT DR' },
      { id: 'Hospital Name', name: 'Dr Abcd' }
    ];


    itemNew.forEach(element => {
      var temp = [element.id, element.name];
      rows.push(temp);

    });

    //doc.autoTable(col, rows);
    doc.autoTable({
        head: col,
        body: rows
    });
    doc.save('Test.pdf');
  }

PDF预览(Test.pdf): PDF Preview

有关更多信息,请参考jsPDF-AutoTable Doccumentation

相关问题