Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsPDF AutoTable - autoTable is not a function

I'm using JSPdf on an Angular app, and I'm attempting to use the JS autotable plugin but I'm running into the JS error

EXCEPTION: Uncaught (in promise): TypeError: doc.autoTable is not a function

TypeError: doc.autoTable is not a function

I have jspdf and jspdf-autotable installed via npm, I confirmed they are in the node modules.

I've imported both plugins this way:

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

and here is my code:

private renderPdf():void{
    let testcolumns = ["TestCol1", "TestCol2"];
    let testrows = [["test item 1", "test item 2"]];
    let doc = new jsPDF();
    doc.autoTable(testcolumns, testrows);
    doc.save('sample.pdf');
}

Is there anything I could be missing here or more code I could provide to help determine the issue?

Thanks!

like image 692
Stu Furlong Avatar asked Jun 23 '17 16:06

Stu Furlong


3 Answers

Just delete the 2 first line of imports and add the following lines:

var jsPDF = require('jspdf');
require('jspdf-autotable');

You can see an example here

like image 143
Jorge Casariego Avatar answered Nov 02 '22 10:11

Jorge Casariego


i was geting same issue and I fixed it like this:

import jsPDF from '../../node_modules/jspdf/dist/jspdf.umd.min.js'
import { applyPlugin } from 'jspdf-autotable'
applyPlugin(jsPDF)

i use "jspdf": "^2.3.1", "jspdf-autotable": "^3.5.20" i hope it helps you!

like image 3
Trịnh Đức Avatar answered Nov 02 '22 11:11

Trịnh Đức


I was getting same issue and This one is worked for me. i have written it in import as

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

And in function i declared it as

const doc = new jsPDF();
like image 1
Anup Bangale Avatar answered Nov 02 '22 12:11

Anup Bangale