jsPDF AutoTable - autoTable is not a function

Stu Furlong picture Stu Furlong · Jun 23, 2017 · Viewed 14.1k times · Source

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!

Answer

Jorge Casariego picture Jorge Casariego · Jun 23, 2017

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