9

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!

Community
  • 1
  • 1
Stu Furlong
  • 3,490
  • 5
  • 34
  • 47

7 Answers7

16

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

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
3

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!

1

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();
Anup Bangale
  • 581
  • 7
  • 7
0

i had the same issue today while using https://github.com/SimulatedGREG/electron-vue. i resolved it by adding 'jspdf' and 'jspdf-autotable' to the whitelist array in path-to-project/.vscode

let whiteListedModules = [
  'vue',
  'vue-sweetalert2',
  'element-ui',
  'vue-avatar-component',
  'vue-router', 
  'vue-json-excel',
  'vuex',
  'vue-chart-js',
  'pluralize',   
  'Print',
  'jspdf',
  "jspdf-autotable"
]
Ewomazino Ukah
  • 2,286
  • 4
  • 24
  • 40
0

You can import jsPDF as its normally imported :

import jsPDF from 'jspdf';

and then for the autoTable :

require('jspdf-autotable');

add this ^ inside the function

0

This worked for me:

import jsPDF from 'jspdf';

require('jspdf-autotable');

const tableColumns = ["column1", "Column2", "Column3"];

const tableRows = [[1,2,3],[a,b,c],[X,Y,Z]];

const doc = new jsPDF();

doc.autoTable(tableColumns, tableRows, { startY: 20 });

doc.text("Closed tickets within the last one month.", 14, 15);

doc.save('dataModel.pdf');
ouflak
  • 2,458
  • 10
  • 44
  • 49
0
import jsPDF from 'jspdf';
import autoTable from 'jspdf-autotable';

const doc = new jsPDF();
autoTable(doc, { html: '#my-table' });
doc.save('table.pdf');`

this worked for me