wkhtmltopdf and the jQuery.ready() function

wkhtmltopdf and the jQuery.ready() function

本文关键字:function ready jQuery and the wkhtmltopdf      更新时间:2023-09-26

我一直在尝试使用wkhtmltopdf工具(http://wkhtmltopdf.org/)生成PDF。我想使用jQuery生成PDF并使用jQuery.ready()函数进行一些初始化的页面,如下所示:

jQuery(function() {
    // do something
});

但是,当我尝试从页面生成PDF时,脚本没有执行。我试过设置一个延迟,等待JavaScript执行的选项:

--javascript-delay 30000

但结果是一样的。

当我启用wkhtmltopdf的JavaScript调试选项时,我得到一个警告,我不确定它是否与问题有关:

Warning: undefined:0 TypeError: 'null' is not an object

有人遇到过这个问题吗?有什么解决办法吗?

编辑:似乎问题是由错误Warning: undefined:0 TypeError: 'null' is not an object.引起的,我已经设法在PDF上打印错误:

window.onerror = function(error, url, line) {
    $('body').before('<b> Error: ' + error + '</b> <br />');
    $('body').before('<b> Url: ' + url + '</b> <br />');
    $('body').before('<b> Line: ' + line + '</b> <br />');
    console.log(error, ' ', url, ' ', line);
};

但是信息非常有限,我不知道是什么原因引起的:

Error: TypeError: 'null' is not an object
Url: undefined
Line: 0 

问题原来是Qt不支持localStorage,所以其中一个初始化脚本失败,导致jQuery.ready()未被执行。

我已经设法调试它与Qt支持的浏览器:QtWeb。(我也试过Arora,但无法在我的系统上运行)。