问题描述
我试图在Angular 9的Primeng数据表中实现PDF导出。我的代码如下:
exportPdf() {
this.cols = [
{ field: 'code',header: 'Code' },{ field: 'name',header: 'Name' },{ field: 'category',header: 'Category' },{ field: 'quantity',header: 'Quantity' }
];
this.exportColumns = this.cols.map(col => ({title: col.header,dataKey: col.field})); console.log(this.exportColumns)
import("jspdf").then(jsPDF => {
import("jspdf-autotable").then(x => {
const doc = new jsPDF.default(0,0);
doc.autoTable(this.exportColumns,this.products);
doc.save('products.pdf');
})
})
}
但是我收到此错误:
error TS2345: Argument of type '0' is not assignable to parameter of type 'string'.
in const doc = new jsPDF.default(0,0);
我还安装了这些
"jspdf": "^1.5.3","jspdf-autotable": "^3.5.13"
解决方法
默认方法将第一个参数作为字符串'"p" | "portrait" | "l" | "landscape"'
,第二个参数作为单位,并采用这些值"pt" | "px" | "in" | "mm" | "cm" | "ex" | "em" | "pc"
import jsPDF from "jspdf";
import "jspdf-autotable";
...
exportPdf() {
const doc = new jsPDF('p','pt'); // or like this ? new jsPDF();
doc.autoTable(this.exportColumns,this.products);
doc.save("products.pdf");
}