simonbengtsson / jsPDF-AutoTable

jsPDF plugin for generating PDF tables with javascript
https://simonbengtsson.github.io/jsPDF-AutoTable/
MIT License
2.33k stars 624 forks source link

autoTable does not exist on type jsPDF #324

Closed cesargrano closed 6 years ago

cesargrano commented 6 years ago

Hello. I'm trying to use on Ionic 2 and WebStrom. You are showing me the following error when compiling: autoTable does not exist on type jsPDF

me code is like this:

import * as jsPDF from 'jspdf'; import 'jspdf-autotable';

dowloadPDF() {
    let doc = new jsPDF('p', 'pt');
    let columns = [
        {title: "ID", dataKey: "id"},
        {title: "Name", dataKey: "name"},
        {title: "Country", dataKey: "country"},
    ];
    let rows = [
        {"id": 1, "name": "Shaw", "country": "Tanzania"},
        {"id": 2, "name": "Nelson", "country": "Kazakhstan"},
        {"id": 3, "name": "Garcia", "country": "Madagascar"},
    ];
           // Error ========================
    doc.autoTable(columns, rows);
    doc.save('teste.pdf');
}
mtAlves commented 6 years ago

Try change the import :

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

dowloadPDF() {
    let doc = new jsPDF('p', 'pt');
    let columns = [
        {title: "ID", dataKey: "id"},
        {title: "Name", dataKey: "name"},
        {title: "Country", dataKey: "country"},
    ];
    let rows = [
        {"id": 1, "name": "Shaw", "country": "Tanzania"},
        {"id": 2, "name": "Nelson", "country": "Kazakhstan"},
        {"id": 3, "name": "Garcia", "country": "Madagascar"},
    ];
           // Error ========================
    doc.autoTable(columns, rows);
    doc.save('teste.pdf');
}

It's working for me in vue.js

cesargrano commented 6 years ago

Hello.

I'm sorry it did not work as you showed it.

Look at the error

doc.autoTable is not a function

Typescript Error Property 'autoTable' does not exist on type 'jsPDF'.

mtAlves commented 6 years ago

https://jsfiddle.net/fbf0wLe5/

Exclude line 2 (import js-autotable) Do you have the same error

cesargrano commented 6 years ago

I already did this, it even works, but it gives an error in the compilation of Typescript

BhuvaneswariVajravel commented 6 years ago

i have the same issue in angular 2 issue in plugin jspdf-autotable/src/main.ts it shows lot of errors

mubasharrasheed commented 6 years ago

Did you add "../node_modules/jspdf/dist/jspdf.min.js", "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js" to angular-cli.json ?? The same issue was for me and resolved instantly after adding these lines in scripts array of angular-cli.json file. Hope this helps for new comers too.

jhonatan2760 commented 6 years ago

I solved this issue, first import the Jspdf import * as jsPDF from 'jspdf'; i'm used a codesmells tatic, copied the content of jspdf autotable and pasted inside the jspdf.js, then works fine for me.

simonbengtsson commented 6 years ago

Try solution posted here if using angular #375

alex2wong commented 5 years ago

@simonbengtsson any plan to add type definition to @types/jspdf ?? then any project involving typescript could pass the type checking.

/// file:  node_modules\@types\jspdf\index.d.ts

// jsPDF plugin: AutoTable
autoTable(options:any):jsPDF;

OR, there's a workround, add // @ts-ignore before jsPDF.autoTable

// @ts-ignore
doc.autoTable()
pabloubal commented 4 years ago

This was the solution for me (Angular 2): https://stackoverflow.com/a/54332585/12410011

gaurav5harma commented 3 years ago

In my case //@ts-ignore worked

exportPdf() { this.exportColumns = this.selectedCols.map(col => ({title: col.header, dataKey: col.field})); import("jspdf").then(jsPDF => { import("jspdf-autotable").then(x => { //@ts-ignore const doc = new jsPDF.default('p','mm'); //@ts-ignore doc.autoTable(this.exportColumns, this.exportData); doc.save('tests.pdf'); }) }) }