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!

4

6 回答 6

16

只需删除 2 第一行导入并添加以下行:

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

你可以在这里看到一个例子

于 2017-06-23T19:08:06.773 回答
2

我遇到了同样的问题,我像这样修复了它:

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

我用 "jspdf": "^2.3.1", "jspdf-autotable": "^3.5.20" 希望对你有帮助!

于 2021-08-31T14:31:57.333 回答
1

我遇到了同样的问题,这对我有用。我已经把它写在导入中

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

在函数中我将其声明为

const doc = new jsPDF();
于 2019-03-12T07:57:57.370 回答
0

这对我有用:

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');
于 2022-01-14T07:20:25.967 回答
0

我今天在使用https://github.com/SimulatedGREG/electron-vue时遇到了同样的问题。我通过将“jspdf”和“jspdf-autotable”添加到 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"
]
于 2018-04-17T11:11:16.157 回答
0

您可以将 jsPDF 作为正常导入的方式导入:

import jsPDF from 'jspdf';

然后对于 autoTable :

require('jspdf-autotable');

在函数中添加这个 ^

于 2020-09-04T04:03:04.123 回答