我如何解决javascript或http错误在wkhtmltopdf


How can I troubleshoot javascript or http errors in wkhtmltopdf?

一段时间以来,我一直使用wkhtmltopdf实时生成pdf文档,总的来说它远远领先于其他工具(与php中可用的其他工具相比)。

然而,即使是对这个过程进行最基本的调试也是非常痛苦的。在您的普通浏览器中,您可能会看到alert(var);console.log(var);,并发现"var"是undefined。同样,您可以很容易地在浏览器控制台中看到404500 http错误。

例如:

wkhtmltopdf.exe http://localhost/mypage.html C:/temp/myfile.pdf

pdf文件确实创建了,但不完美,因为依赖于javascript或AJAX的某些方面缺失。

使用wkhtmltopdf,如果这些错误存在于mypage.html中,我如何才能最好地调查这些错误?

您需要使用--debug-javascript命令行选项。我在Linux上测试了它,但在Windows上可能是一样的:

$ echo '<script>console.log("YAY!");</script>' > test.html
$ wkhtmltopdf --debug-javascript test.html test.pdf
Loading pages (1/6)
Warning: file:///tmp/test.html:1 YAY!
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done

对于404和500错误,如果我没记错的话,wkhtmltopdf也应该在命令行上打印一些东西。

在我看来,您应该首先在浏览器中打开源页面。浏览器的调试器可以跟踪这些问题。一旦您知道页面是完美的,您可以通过wkhtmltopdf.

运行它。