当我使用旧版本的库时,我可以很好地创建pdf。但是由于这些新版本我做不到。早些时候我刚刚下载了jspdf.min.js文件,但是现在我可以看到各种版本,比如umd,es。但是我不知道该使用什么(?),所以我下载了umd版本。我不使用node.js或require,所以在创建pdf文档之前,我不能在javascript代码中使用require或import (导入时有一个错误:Uncaught SyntaxError: export declarations may only appear at top level of a module)指令。因此,我使用const { jsPDF } = window.jspdf;来使用jsPDF类(没有它,我就得到错误“未定义的ReferenceError: jsPDF未定义”,在行const doc = new jsPDF();之后)。但我也想使用插件AutoTable,在插件https://github.com/simonbengtsson/jsPDF-AutoTable的官方页面中,作者使用导入或要求,对我来说,它不起作用。当我尝试
const { jsPDF } = window.jspdf;
const doc= new jsPDF();
doc.autoTable({
head: [['Name', 'Email', 'Country']],
body: [
['David', 'david@example.com', 'Sweden'],
['Castille', 'castille@example.com', 'Spain'],
],
})我得到了错误Uncaught TypeError: pdf.autoTable is not a function。我怎么才能使用图书馆呢?我想jsPDF适用于、umd、和const { jsPDF } = window.jspdf;版本(我只创建了一个带有字符串的简单文档并使其正常工作),但AutoTable不起作用。
发布于 2020-10-12 06:35:42
现在对我来说唯一可行的解决方案是将jsPDF和AutoTable的代码保存在同一个文件中。这不是个好主意,我知道,我想把它们放在单独的文件里,但是我还没有建立其他的方法。任何人都能说出一个更好的方法来避免错误,那就太好了。
发布于 2020-10-06 14:17:46
导出autoTable方法,从'jspdf- autoTable '导入autotable
function createPdf() {
var doc = new jsPDF();
const head = [['ID', 'Country', 'Index', 'Capital']]
const data = [
[1, 'Finland', 7.632, 'Helsinki'],
[2, 'Norway', 7.594, 'Oslo'],
[3, 'Denmark', 7.555, 'Copenhagen'],
[4, 'Iceland', 7.495, 'Reykjavík'],
[5, 'Switzerland', 7.487, 'Bern'],
[9, 'Sweden', 7.314, 'Stockholm'],
[73, 'Belarus', 5.483, 'Minsk'],
]
autoTable(doc, {
head: head,
body: data,
didDrawCell: (data) => {
console.log(data.column.index)
},
})
doc.save('repro.pdf');
}发布于 2022-10-28 09:56:24
我成功地尝试了新版本的jspdf (版本2.5.1)和autotable (版本3.5.25)。
最起码的例子:
<!DOCTYPE html>
<html>
<head>
<title>jspdf-autotable demo</title>
</head>
<body>
<div>save pdf modal will pop up automatically</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.25/jspdf.plugin.autotable.min.js"></script>
<script>
const { jsPDF } = window.jspdf;
const doc= new jsPDF();
doc.autoTable({
head: [['Name', 'Email', 'Country']],
body: [
['David', 'david@example.com', 'Sweden'],
['Castille', 'castille@example.com', 'Spain'],
],
})
doc.save("demo.pdf");
</script>
</html>https://stackoverflow.com/questions/64209548
复制相似问题