使用 jspdf 将内容导出为 pdf angular

Export content to pdf angular using jspdf

我正在尝试使用 jspdf 创建 pdf。但是我收到错误 cannot create property 'header' on string 'Details'。我已经完成了这个问题 How to fix: Cannot create property 'header' on string 但无法提前找到 solution.Please Help.Thanks。 这是我的转换函数

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

问题出在您指定 headbodyautoTable() 函数中。

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):

有关详细信息,请参阅 jsPDF-AutoTable Documentation