Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtmltopdf with a jquery ajax request

I'm working on an app that needs to generate a pdf from some html. I'm using wkhtmltopdf and running into some problems.

The page has an ajax request. Under normal circumstances this ajax request returns some json. When I use this snippet:

  jQuery.get(URL, function(data){
    $("body").html('THIS CODE IS RUNNING');
  });

and visit the page in a browser everything behaves nicely. However, if I run wkhtmltopdf on the url then i get a blank pdf.

Coincidently, the url i'm passing to the ajax function can be modified with a format=array query that will return the json as a formatted string instead of json, and when I do this then everything works as expected and running wkhtmltopdf generates a pdf with the words "THIS CODE IS RUNNING".

I'm hitting a bit of a wall here. I think the problem might have to do with the way that jQuery parses json results, but I'm not sure. Thoughts?

Edit:

I forgot to add this important detail. I'm pretty confident that I've ruled out the theory that the ajax request just hadn't finished. I've tried the wkhtmltopdf call with large numbers for --javascript-delay with the same result. Not only that but the ajax request does work when I make the request (to the same server, returning the same data) and the request returns non-json data.

like image 464
Aaron Bruce Avatar asked Apr 02 '14 22:04

Aaron Bruce


1 Answers

wkhtmltopdf doesn't know about AJAX, so it might not be waiting for the request to return. Try using the --javascript-delay option and see if it works for you. You can also try to use the --window-status parameter if you have control of the page generation, see this post on the mailing list for details.

like image 100
ashkulz Avatar answered Oct 17 '22 00:10

ashkulz