问题
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?
回答1:
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.
回答2:
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.
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/21578457/wkhtmltopdf-javascript-delay-for-output-of-google-maps