jsPDF AutoTable - autoTable不是一个函数。

9
我正在一个Angular应用程序中使用JSPdf,并尝试使用JS autotable插件,但我遇到了JS错误。
异常信息如下:
EXCEPTION: Uncaught (in promise): TypeError: doc.autoTable is not a function TypeError: doc.autoTable is not a function
我已经通过npm安装了jspdf和jspdf-autotable,并确认它们在node模块中。
我是这样导入两个插件的:
import * as jsPDF from 'jspdf' 
import * as autoTable from 'jspdf-autotable'

这是我的代码:

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');
}

这里是否有什么我可能遗漏的或者更多的代码可以提供来帮助确定问题呢?

谢谢!

7个回答

16

只需删除前两行导入语句,并添加以下几行:

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

您可以在这里看到一个示例。


3

我曾经遇到了同样的问题,我这样解决了它:

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"。希望这能帮到您!

这也适用于 Vite。 - Branislav Popadič
它在我的Vite + Tailwind CSS中工作。 - sankalpdev

1

我也遇到了同样的问题,以下这个方法对我有用。我在导入中写了如下代码:

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

而在函数中,我将其声明为

const doc = new jsPDF();

import jsPDF from 'jspdf'; import 'jspdf-autotable'; - Stack Underflow

0
今天我在使用https://github.com/SimulatedGREG/electron-vue时遇到了同样的问题。我通过将'jspdf'和'jspdf-autotable'添加到项目路径/.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"
]

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

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

这对我有用


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');

0

您可以像通常导入一样导入jsPDF:

import jsPDF from 'jspdf';

然后是自动表格:

require('jspdf-autotable');

在函数内部添加这个 ^ 符号


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接