mileszs / wicked_pdf

PDF generator (from HTML) plugin for Ruby on Rails
http://www.mileszs.com/wicked-pdf-plugin
MIT License
3.54k stars 646 forks source link

AJAX server calls not happening when generating PDF #591

Open solasdev opened 7 years ago

solasdev commented 7 years ago

Anyone else have trouble loading PDF where template content is populated with several AJAX calls to the controller? I'm up to date and using --no-stop-slow-scripts and javescript_delay and my javascript is imported correctly. If I check the logs however there are no calls being made and the AJAX request never finishes. Iv'e also tried jsonp requests.

Any help here much appreciated.

One the ajax calls: $('#custom_waste').html('Loading General Waste tables...') $.ajax({ type: "POST", url: '/reports/custom_load', data: 'year=<%= @report.year %>&month=<%= @report.month %>&element_title=Reduce General Waste&partial=waste_to_landfill&interval=Monthly&from_date=<%= Date.parse('01/01/2016') -1.month %>&to_date=<%= Date.parse(@report.month+'/'+@report.year).end_of_year %>', success: function(data){ $('#custom_waste').html(data) } });

unixmonkey commented 7 years ago

The request for the Ajax call will never resolve because it has to go through Rails (routes) to be served, and the pdf request will never finish because wkhtmltopdf is waiting for the pdf to finish... This causes a deadlock because in dev mode, by default, Rails (with Webrick, anyway) will only serve 1 request at a time, and it can't switch to serving your Ajax request until it's done serving your PDF request, which it can't do because it's waiting for your Ajax request.

It should work just fine in production because most production environments are setup to run multiple web workers (with passenger or unicorn or something), but to properly test this in development mode, you'll need to configure your app to run multiple web workers, so your Ajax request can happen in another thread and return back to your main request.

I would suggest you add the puma gem to your Gemfile, and run your dev server with rails server Puma --workers 3 (by the way, puma is included by default in new Rails 5 apps because people were having this same issue with ActionCable requests).

Let me know how it goes.

solasdev commented 7 years ago

Thanks for the suggestion. Still no joy however running Puma locally with 3 workers, ajax server call stil not touching the server, used full server address also in the ajax request. Feel like i'm close but no idea why they won't work.

My controller code: def init_report_export @report = KeelingsInitReport.find(params[:report_id])

respond_to do |format|
  format.html
  format.pdf do
    render :pdf => 'out.pdf', :layout => 'layouts/pdf.html.erb', :extra => '--no-stop-slow-scripts', :javascript_delay => 5000
  end
end

end

unixmonkey commented 7 years ago

Sorry I missed this the first time, but it makes sense since you say the request isn't touching your server. Your request to /reports/custom_load is a relative path, which wkhtmltopdf won't know what host to load from.

You should be able to add the host info with something like this:

"<%= request.protocol + '://' + request.host_with_port %>/reports/custom_load"
solasdev commented 7 years ago

I was thinking that's the issue but with the the full url path in it's still the same. Very strange. Going to simplify things create a new dummy app and test make sure it's not something else that's interfering although can't think what that might be.

solasdev commented 7 years ago

Ok sorry to come back on this again but iv'e started from scratch a rails app using latest wicked_pdf etc. performing a basic ajax request when rendering a pdf and still no activity to the server(using puma with workers) with such request.

Here's the project - https://github.com/solasdev/test_wicked_pdf

when I go to init_report_export action in the app the pdf that generated should have the html of the div changed to 'moo'. Maybe i'm still missing something obvious?

hgcode commented 7 years ago

hi, any luck with this? I'm facing the same issue.

unixmonkey commented 7 years ago

@hgcode My comments above should help you avoid this issue. Have you tried them?

g3ck0 commented 6 years ago

Hi @unixmonke y i facing the same issue, i already tried your suggestions but it doesn't work, my Puma workers are 3, the errors that ajax throws are saying that the ajaxreadystate is 0, i'm using the full url to access my server (local server for now), everything seems that it is correct, but it is not working