jsPDF addHTML方法不工作,没有错误消息
jsPDF addHTML method not working with no error message
我使用jsPdf官方演示站点中的示例来测试新的addHTML函数,只做了一点更改就可以直接保存生成的PDF。
console.log("testing");
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.body,function() {
console.log("started");
pdf.save()
console.log("finished");
});
console.log("testing again");
当我运行上面的脚本时,它不会生成错误消息,但也不会生成PDF。在控制台中,只显示"测试"answers"再次测试",所以我猜脚本没有运行。
我错过了什么?我正在使用引导选项卡功能和一些使用highchart生成的图表。jsPDF处理起来太复杂了吗?
这是因为回调函数已更改为promise。
进行以下更改。
pdf.addHTML(document.body,function() {
console.log("started");
pdf.save()
console.log("finished");
});
至
pdf.addHTML(document.body).then(()=> {
console.log("started");
pdf.save()
console.log("finished");
});
首先,您需要html2canvas或rasterizeHTML来使用jsPDF的addHTML方法。进一步的问题是jsPDF覆盖了html2canvas库定义的许多方法。只需查看jsPDF源代码并搜索html2canvas即可。即使在我的项目中包含了html2canvas脚本之后,我也遇到了与您描述的完全相同的问题。
最后一个问题是包含脚本的顺序。对我来说,它适用于当前版本的jsPDF(1.1.239)和html2canvas(0.5.0-alpha1),首先包括jsPDF,然后包括html2canvas。在我的项目中,它看起来像这样:
<head>
<script src="./lib/jspdf/jspdf.debug.js"></script>
<script src="./lib/html2canvas/html2canvas.js"></script>
<!-- more includes ... -->
</head>
我认为在jsPDF中重写html2canvas方法是一种糟糕的做法,因为当html2canvas更改时很难维护,正如这个问题所示。
当通过RequireJS进行脚本包含时,这个问题的表现可能会有所不同。
jspdf无法转换svg元素。使用此库可以轻松完成此操作。
示例用法-
function _svgToCanvas() {
var nodesToRecover = [];
var nodesToRemove = [];
var svgElems = document.getElementsByTagName("svg");
for (var i = 0; i < svgElems.length; i++) {
var node = svgElems[i];
var parentNode = node.parentNode;
var svg = parentNode.innerHTML;
var canvas = document.createElement('canvas');
canvg(canvas, svg);
nodesToRecover.push({
parent: parentNode,
child: node
});
parentNode.removeChild(node);
nodesToRemove.push({
parent: parentNode,
child: canvas
});
parentNode.appendChild(canvas);
}
}
然后-
var pdf = new jsPDF('p', 'px', 'a4');
var options = {
pagesplit: true
};
pdf.addHTML($('body'), 0, 0, options, function () {
console.log("done");
pdf.save('test.pdf')
});
也有同样的问题。虽然我不确定addHTML
的确切问题是什么,但我还是设法使用html2canvas:使其正常工作
html2canvas(document.body).then(canvas => {
var pdf = new jsPDF('p', 'pt', 'a4');
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 210, 297);
pdf.save();
});
相关文章:
- jQuery在输入下验证post错误消息
- 在AngularJs中隐藏默认错误消息
- 回复'js'仅当请求有错误时(否则使用html)
- 如果用户输入的长度小于最小长度,则显示错误消息
- 使用ASP.NET CustomValidator避免重复的错误消息
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 更改精细上载中的错误消息
- 如何在页面刷新时隐藏错误消息
- 如何显示错误消息
- 表单已发送,但验证有错误
- 滑块未显示,控制台中没有错误消息
- 如果文件不存在,fs.watch是否有错误处理程序
- 表单提交没有'如果为空,则不会显示错误消息
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- Javascript在每个有问题的控件上输出一条错误消息
- 如果未配置闪存,我该怎么办,那么在自动捕获图像上应该有一些错误消息
- 有没有一种方法可以在加载页面后检查iframe是否有错误消息
- JQuery表单验证:当有转义JSON时不显示错误消息
- 是否有任何好的或可靠的方法可以仅使用Internet Explorer错误消息来确定JavaScript错误的位置