我正在尝试使用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
答案 0 :(得分:0)
问题出在autoTable()
函数中,您在其中指定head
和body
。
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');
}
有关更多信息,请参考jsPDF-AutoTable Doccumentation。