wkhtmltopdf javascript delay for output of google maps
Asked Answered
K

4

12

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?

Kaufmann answered 5/2, 2014 at 13:18 Comment(1)
You may also want to try the --no-stop-slow-scripts option, which prevents long-running JS scripts from being aborted early. Might not fix your specific issue but it did fix ours.Tjaden
R
7

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.

Ragtime answered 6/2, 2014 at 6:7 Comment(0)
J
31

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.

Jibe answered 28/8, 2015 at 22:23 Comment(1)
this didn't work for me. The script wkhtmltopdf just stops.Inebriety
S
13

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

Slipon answered 6/2, 2014 at 14:9 Comment(1)
just a side note when you use the --javascript-delay you need to specify the delay in milliseconds. shell_exec("/usr/local/bin/wkhtmltopdf --javascript-delay {$delayInMilliseconds} {$url} {$fileName}");Wellmeaning
R
7

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.

Ragtime answered 6/2, 2014 at 6:7 Comment(0)
N
1

The --javascript-delay function works, but is suboptimal for my usage -- maybe your usage is variable as well? The PDF can sometimes contain a list of just a dozen items and their map view, or hundreds of items on the map. There is no one right msec delay for me...

I successfully used @rohit-singhal tip (Rails app using GMaps4Rails gem and haml views) inside the controller method:

def index
  ...
  respond_to do |format|
    format.pdf do
      @map_data = { markers: @map_hash, zoom: 10, cluster_zoom: 10, center: center_coordinates,
                    fit_to_bounds: true, show_center_marker: false, map_type: 'roadmap'}
      render pdf:             'Water Supplies',
             disposition:     'inline',
             layout:          'layouts/pdf.html.haml',
             show_as_html:     params.key?('debug'),
             no_stop_slow_scripts: ''
    end
  end
end

And I used @mwag's tilesloaded callback as well. (Verbatim in the google maps generation javascript.) The controller line of interest to switch out the no_stop_slow_scripts for:

             ...
             window_status:   'ready_to_print'
             ...

Both worked. Not sure if there are any advantages to one or the other.

Nosey answered 29/7, 2018 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.