Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf javascript delay for output of google maps

Tags:

wkhtmltopdf

I am working with WKTHMTOPDF and really enjoying it. However, the page that is being converted has google maps and the resulting PDF comes out with the map half loaded. I know there was an option to add --javascript--delay in previous versions, but it would appear it is deprecated. I am using version 0.99. Is there a different option?

like image 530
Afroman Makgalemela Avatar asked Feb 05 '14 13:02

Afroman Makgalemela


3 Answers

You can use the wkhtmltopdf version 0.12.0

I am also using in websites some highly javascript contents. Previously, It was not rendering properly with version 0.99 But when I used version 0.12 with using the option --javascript-delay, everything looks fine.

You can add other options too to load your javascript perfectly i.e. --enable-javascript , --no-stop-slow-scripts etc

Be sure that you have to use proper time delay in using --javascript-delay, it depends on your site that how much time it is taking to render. If you will use more time delay then it will take more time to execute and if you will take less time delay then javascript will not be loaded properly.
The link to latest version of wkhtmltopdf

like image 161
Rohit Singhal Avatar answered Nov 18 '22 17:11

Rohit Singhal


There is another a better way to do this that does not require using --javascript--delay (and has the advantage of not requiring you to set a delay time before you know what the required delay will actually be).

Add a callback to the 'tileloaded' event:

google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
   window.status = 'ready_to_print';
});

Then call wkhtmltopdf with the --window.status option set to 'ready_to_print' e.g.

wkhtmltopdf --window-status ready_to_print map.html map.pdf

obviously you can change the string 'ready_to_print' to be whatever you want so long as window.status does not already equal that value when wkhtmltopdf is called and before the above code fires.

A similar approach can be used with google charts, though the appropriate event goes by a different name.

like image 45
mwag Avatar answered Nov 18 '22 16:11

mwag


The --javascript-delay option is not deprecated at all. Also, it would be advisable to upgrade to the latest version -- 0.9.9 is a very old version.

like image 7
ashkulz Avatar answered Nov 18 '22 17:11

ashkulz