Search code examples
angularprimengjspdfangular9jspdf-autotable

Export pdf in primeng datatable is not working in Angular 9


I tried to implement export as PDF in Primeng datatable in Angular 9. My code is given below:

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');
      
 })
})
}

But I am getting this error:

error TS2345: Argument of type '0' is not assignable to parameter of type 'string'.

    in       const doc = new jsPDF.default(0,0);

I installed these also:

"jspdf": "^1.5.3",
"jspdf-autotable": "^3.5.13"

How can I solve this error. Can someone help me please?


Solution

  • the default methods take the first argument as string '"p" | "portrait" | "l" | "landscape"' and the second is the unit and take these values "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");
    }
    

    demo 🚀