Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf and the jQuery.ready() function

I've been trying to generate a PDF using the wkhtmltopdf tool (http://wkhtmltopdf.org/). The page from which I want to generate a PDF using jQuery and has some initializations using a jQuery.ready() function, something like this:

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

However, when I try to generate a PDF from the page the script is not executed. I've tried setting a delay to wait for the JavaScript to be executed with the option:

--javascript-delay 30000

But the result is the same.

When I've enabled wkhtmltopdf's JavaScript debugging option I get a warning, which I'm not sure if it's related to the problem:

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

Has anyone encountered this problem? Is there some kind of workaround?

EDIT: Seems the problem is caused by the error Warning: undefined:0 TypeError: 'null' is not an object. I've managed to print the error on the PDF using:

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

But the information is very limited and I have no idea what might cause it:

Error: TypeError: 'null' is not an object
Url: undefined
Line: 0 
like image 601
Ivo Avatar asked May 21 '14 05:05

Ivo


People also ask

What is ready function in jQuery?

The ready() method is an inbuilt method in jQuery which helps to load the whole page then execute the rest code. This method specify the function to execute when the DOM is fully loaded. Syntax: $(document).ready(function)

What does $( function ()) shorthand do?

This is a shortcut for $(document). ready() , which is executed when the browser has finished loading the page (meaning here, "when the DOM is available").

How do you call a function in document ready jQuery?

jQuery Document Ready Example $(document). ready(function() { //DOM manipulation code }); You call jQuery's $ function, passing to it the document object. The $ function returns an enhanced version of the document object.

How do you write a ready function in JavaScript?

ready : $(document). ready(function() { console. log("Ready!"); });


1 Answers

The problem turned out to be that Qt doesn't support localStorage, so one of the initialization scripts failed which cause the jQuery.ready() not being executed.

I've managed to debug it with a browser with Qt support: QtWeb. (Tried Arora as well, couldn't run it on my system).

like image 53
Ivo Avatar answered Sep 21 '22 12:09

Ivo