最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - wkhtmltopdf with a jquery ajax request - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Apr 3, 2014 at 16:09 Aaron Bruce asked Apr 2, 2014 at 22:17 Aaron BruceAaron Bruce 1,1701 gold badge12 silver badges24 bronze badges 2
  • Did you ever solve this? – HPage Commented Oct 27, 2016 at 14:49
  • It's been awhile, but I ended up switching to phantomjs. This particular problem turned out to be a js error that I wasn't able to find using the wkhtmltopdf tools, but I believe there is a way to check for js errors when using wkhtml2pdf and that probably would have solved this problem. – Aaron Bruce Commented Oct 31, 2016 at 21:57
Add a ment  | 

1 Answer 1

Reset to default 4

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.

发布评论

评论列表(0)

  1. 暂无评论