Search code examples
javascriptphpwkhtmltopdf

How can I troubleshoot javascript or http errors in wkhtmltopdf?


For a while now, I have used wkhtmltopdf for on-the-fly generation of pdf documents, and generally it is far ahead of the pack (compared with other tools available in php).

However, it is a HUGE PAIN to do even the most basic debugging of this process. With your regular browser, one might alert(var); or console.log(var); and discover that "var" is undefined. Again, you can easily see a 404 or 500 http error somewhere in the browser console.

Take for instance this:

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

The pdf file does get created but is imperfect because certain aspects dependent on javscript or AJAX are missing.

With wkhtmltopdf, how best can I investigate these kinds of errors, if they exist in mypage.html?


Solution

  • You need to use the --debug-javascript command-line option. I tested it in Linux, but it's probably the same on 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
    

    For 404 and 500 error, wkhtmltopdf should prints some stuff on the command line too, if I remember well.