El Kopyto
El Kopyto

Reputation: 1177

Debugging javascript in wkhtmltopdf

Where I can see the output of javascript debug when wkhtmltopdf runs in debug mode (--debug-javascript)

Upvotes: 22

Views: 23794

Answers (4)

AHeraldOfTheNewAge
AHeraldOfTheNewAge

Reputation: 59

In order to see the js errors made by wkhtmltopdf use "--debug-javascript" parameter in your command.

PLEASE! do not use the quiet mode for wkhtmltopdf -> ("-q" or "--quiet") because it will silence your js errors, and you wont be able to see anything.

ALSO! if you want to make something like a console.log() if you want to debug your js more in detail -> you can use: throw new Error(yourVar) and you will see the contents in the response of wkhtmltopdf (make sure you convert the variable you want to throw as error into string(if it's an object or something..))

wkhtmltopdf documentation: https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

Upvotes: 0

halfbit
halfbit

Reputation: 3939

Although Daffy Punks answer is correct, I had an additional idea some weeks ago, that helped me a lot. This I want to share: Show it inside PDF

When rendering the layout for PDF I put an additional (hidden) DIV #pdf_errors

And very early in source - if #pdf_errors is here - I let point console output to fill this div, and on error - I show it. Its not really debugging, but at least I see now what was going wrong.

Source in coffeescript, my plain javascript times are long gone ...

if ($pdf=$("#pdf_is_here")).length
    for type in ["log","warn","error"]
        do (type) =>
            console[type]= (a...) =>
                $pdf.append("<div class='#{type}'>#{s}</div>") for s in a

    window.onerror= (messageOrEvent, source, lineno, colno, error) =>
        $pdf.append("<div class='critical'>#{messageOrEvent}</div>")
        $pdf.show()

    $pdf.hide() # just in case css is not here

Upvotes: 9

CShark
CShark

Reputation: 2221

Another (I would say easiest) means of debugging javascript in WKHTMLTOPDF is to download QT Browser, the underlying browser used by WKHTMLTOPDF, and to inspect the javascript execution for your page from within the browser.

You can download it from here

Instructions on debugging javascript in QT here

You can basically debug your JavaScript in QT Browser just as you would in Chrome or Firefox.

Upvotes: 43

Jasper
Jasper

Reputation: 2471

Rendering test.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    BODY
    <script>
      console.log('Hi!');
    </script>
  </body>
</html>

like this

wkhtmltopdf test.html test.pdf --debug-javascript

should return something like this

Loading pages (1/5)
Warning: :0 Hi!                                                   
Resolving links (2/5)                                              
Counting pages (3/5)                                                      
Printing pages (5/5)                                                      
Done 

Upvotes: 21

Related Questions