Micku
Micku

Reputation: 71

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?

Upvotes: 1

Views: 2502

Answers (2)

Anderson Benício
Anderson Benício

Reputation: 118

Try to use this:

First, make sure to install the necessary plugins using npm:

npm install jspdf jspdf-autotable

then, put the imports in the component and try this code:

import jsPDF from 'jspdf';
import autoTable from 'jspdf-autotable';

exportColumns: any =[]
cols: any = [ 
    { field: 'code', header: 'Code' },
    { field: 'name', header: 'Name' },
    { field: 'category', header: 'Category' },
    { field: 'quantity', header: 'Quantity' }
  ];


exportPdf() {
  this.exportColumns = this.cols.map(col => ({title: col.header, dataKey: col.field}));
  const doc = new jsPDF('portrait', 'px', 'a4');
  autoTable(doc, {columns: this.exportColumns , body: this.products, });
  doc.save('products.pdf');
}

Upvotes: 0

Muhammed Albarmavi
Muhammed Albarmavi

Reputation: 24406

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 🚀

Upvotes: 1

Related Questions