jsPDF addHTML方法不工作,没有错误信息 [英] jsPDF addHTML method not working with no error message

查看:117
本文介绍了jsPDF addHTML方法不工作,没有错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jsPdf官方演示网站中的示例来测试新的addHTML函数,并带有

  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(再次测试);

当我运行上面的脚本时,它不会生成错误消息,也不会生成PDF。在控制台中,只显示testing和testing again,所以我猜这个脚本没有运行。



我错过了什么?我正在使用引导选项卡功能,并使用高图生成一些图表。 jsPDF处理起来太复杂了吗?

首先,你需要 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>
<! - 更多包括... - >
< / head>

我认为在jsPDF中覆盖html2canvas方法是不好的做法,因为当html2canvas很难维护正如这个问题所显示的那样。



当脚本包含通过RequireJS完成时,这个问题可能会有所不同。


I was using the example from jsPdf official demo site to test the new addHTML function, with a little change to directly save the PDF generated.

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");

When I run the script above, it generates no error message but also no PDF is generated. In the console, only "testing" and "testing again" is shown, so I guess the script is not run.

What have I missed? And I am using bootstrap tab function and some chart generated using highchart. Is it too complicated for jsPDF to handled?

解决方案

First, you need either html2canvas or rasterizeHTML to use the addHTML method of jsPDF. The further problem is that jsPDF overwrites many of the methods defined by html2canvas library. Just look at the jsPDF source code and search for html2canvas. I encountered the exact same problem that you describe even after including the html2canvas script in my project.

The final issue is the order of including the scripts. For me it works with the current version of jsPDF (1.1.239) and html2canvas (0.5.0-alpha1) when including jsPDF first and html2canvas afterwards. In my project it looks like this:

<head>
    <script src="./lib/jspdf/jspdf.debug.js"></script>
    <script src="./lib/html2canvas/html2canvas.js"></script>
    <!-- more includes ... -->
</head>

I think the overwriting of html2canvas methods in jsPDF is bad practice, because it is hard to maintain when html2canvas changes, as this problem shows.

Maybe this problem behaves differently when script including is done via RequireJS.

这篇关于jsPDF addHTML方法不工作,没有错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆