【发布时间】:2019-01-03 09:55:28
【问题描述】:
我正在实现 jsPdf 示例并有示例 html 项目,其中我包含 jquery 和 jspdf cdn 链接以生成 pdf。这是我的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script> src = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js"</script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>
<div id="content">
<p>Testing testing testing</p>
</div>
<button type="button" id="export">Export To Pdf</button>
<script>
var doc = new jsPDF();
$('#export').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170
});
doc.save('sample-file.pdf');
});
</script>
</body>
</html>
但它给出了这个错误
Uncaught ReferenceError: jsPDF is not defined
at index.html:19
我的代码有什么问题?
【问题讨论】:
-
导入脚本时,它将导出您可能使用的变量/引用(即 jsPDF)。确保它被正确导入并记录通过头部和主体加载的脚本。我建议使用 webpack 或 requirejs 以避免全局命名空间混乱。
标签: javascript html jspdf html-to-pdf