【问题标题】:Export content to pdf angular using jspdf使用jspdf将内容导出为pdf angular
【发布时间】:2019-06-20 05:42:37
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 你能分享到stackbiltz吗
  • @AdritaSharma 添加了 stackblitz 链接

标签: javascript angular typescript jspdf jspdf-autotable


【解决方案1】:

问题出在您指定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 了解更多信息。

【讨论】:

  • @rock11 很高兴为您提供帮助。您能否将答案标记为有用。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2018-01-07
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多