0

我试图将数据打印到 PDF 中,所以我使用了 jsPdf ,当时数据没有正确对齐到我的 PDF 表中。所以我搜索了很多网站,他们推荐我使用 jsPdf Auto-table 。这里出现了问题,在注入 jsPdf Auto-table 之前,每件事都工作正常(没有对齐)但是在我插入之后

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.16/jspdf.plugin.autotable.js"></script>

这进入我的index.html出现错误,

未捕获的 ReferenceError:未定义 jsPDF (jspdf.plugin.autotable.js:10)

4

2 回答 2

3

您需要在 jspdf-autotable 插件之前包含 jspdf 库。有关更多信息,请参阅文档。您可能还想要最新版本。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.debug.js"></script>

<!-- EDIT: For now, add this line between the libraries -->
<!-- The reason being that jspdf includes a version of requirejs which -->
<!-- jspdf-autotable currently is not expecting. You can also use version < 2.0.21 -->
<script>if (window.define) delete window.define.amd;</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.0.28/jspdf.plugin.autotable.js"></script>

<script>
var columns = ["ID", "Name", "Country"];
var rows = [
   [1, "Shaw", "Tanzania"],
   [2, "Nelson", "Kazakhstan"],
   [3, "Garcia", "Madagascar"]
];
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, rows);
doc.save('table.pdf');
</script>

于 2016-07-22T12:07:39.793 回答
2

这不是直接相关的,但对于那些试图在 Angular cli 创建的 Angular 4 项目中使用它并获得 doc.autoTable 不是函数的人。在脚本数组中的 .angular-cli.json 文件中包含 jspdf 和 jspdf.plugin.autotable

"scripts": [
    "../node_modules/jspdf/dist/jspdf.min.js",
     "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.src.js"
  ],

然后在要使用自动表的组件中首先导入 jspdf 和 jspdf-autotable

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

然后如下使用。您必须在进行 doc.autotable 调用之前声明 jpt 变量

let doc = new jsPDF('p', 'pt');    jpt; 
doc.autoTable(this.columns, this.data);
doc.text(20, 20, 'Hello world!');
doc.text(20, 40, 'This is client-side Javascript, pumping out a PDF.');

// Save the PDF
doc.save('Test.pdf');
于 2017-06-13T18:42:22.907 回答