jspdf - jspdf脚本的问题
问题描述
我在我的网站上添加了这个 jspdf 脚本来下载 pdf。但是我得到这个错误。
Uncaught TypeError: Cannot read property '1' of undefined
at f.renderParagraph (jspdf.min.js:202)
at f.setBlockBoundary (jspdf.min.js:202)
at k (jspdf.min.js:202)
at k (jspdf.min.js:202)
at k (jspdf.min.js:202)
at jspdf.min.js:202
at l (jspdf.min.js:202)
at Image.i.onerror.i.onload (jspdf.min.js:202)
这发生在某些页面上,而其他页面工作正常。我在下面添加了我正在使用的代码。我不确定这是否与我的代码或 jspdf 有关。
//Code I am using:
<script type="text/javascript">
function HTMLtoPDF(){
var pdf = new jsPDF('p', 'pt', 'letter');
source = $('#HTMLtoPDF')[0];
specialElementHandlers = {
'#bypassme': function(element, renderer){
return true
}
}
margins = {
top: 50,
left: 60,
right:60,
width: 545
};
pdf.fromHTML(
source // HTML string or DOM elem ref.
, margins.left // x coord
, margins.top // y coord
, {
'width': margins.width // max width of content on PDF
, 'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('Download.pdf');
}
)
}
</script>
<button type="button" onclick="HTMLtoPDF()" style=" height: 40px; width: 154px; background-color: #008800; color: #ffffff; font-size: 150%;">Download PDF </button>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"> </script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
解决方案
script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var str_pdf = $("#HTMLtoPDF").html();
var regex = /<br\s*[\/]?>/gi;
$("#HTMLtoPDF").html(str_pdf.replace(regex, '<p data-empty="true"><br></p>'));
});
function HTMLtoPDF(){
var pdf = new jsPDF('p', 'pt', 'a4');
source = $('#HTMLtoPDF')[0];
specialElementHandlers = {
'#bypassme': function (element, renderer) {
return true
}
};
margins = {
top: 30,
bottom: 30,
left: 40,
width: 522
};
pdf.fromHTML(
source,
margins.left,
margins.top, {
'width': margins.width,
'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('Download.pdf');
}, margins);
}
</script>
<!-- these js files are used for making PDF -->
推荐阅读
- angular - Angular 的 Keycloak 不发送图像的不记名身份验证
- github - 在 coingecko API 上获取所有加密货币 github 存储库
- android - 从按钮 onClick 更改 TextView 属性不起作用
- asp.net - 使用 Chrome 时的 Asp.net MVC 项目不会等待方法以调试模式返回
- c# - C# 正则表达式不包含“*”符号
- android - NotFoundException - 在 android 10 的初始屏幕中
- amazon-web-services - 运行在不同节点上的 k8s 部署的多个副本如何以 READWRITEMANY pv 模式从 aws EBS 卷中读取?
- firebase - 我可以查看本月到目前为止的当前帐单吗?(Firebase Blaze 计划)
- java - RabbitMQ Client 关闭 MessageListener 无法恢复
- php - mysqli_real_connect(): (HY000/2006): MySQL 服务器已经消失 - max_allowed_packet 不好