1

I'm trying to set up a form that takes in a set of criteria, runs a query using those criteria, and either displays the results in the browser window below the form (via js) or as a pdf (using Prawn) in a new window.

index.html.erb (the form):

<%= form_for :criteria, url: results_path, method: :patch, remote: true do |f| %>
  ...input fields...
  <%= button_tag("View", remote: true, value: 'js', name: 'format' %>
  <%= button_tag("PDF", remote: true, value: 'pdf', name: 'format', formtarget: '_blank') %>
  <div id='report-results'>
  </div>
<% end %>

controller:

def index
end

def results
  @results = ...the query...
  respond_to do |format|
    format.js
    format.pdf do
      report = ResultsPdf.new(@results)
      send_data report.render, filename: "Results_#{Date.today}.pdf", type: 'application/pdf', disposition: "inline"
    end
  end
end

results.js.erb:

$('div#report-results').empty();
$('div#report-results').html("<%= escape_javascript(render 'report_results') %>");

With this code, the View option works (renders the results in the browser) but the PDF option appears to do nothing. The server shows it hitting the controller as PDF, running the query, and sending the data but the pdf itself is not generated. If I remove remote: true from the form declaration then the PDF options works but the View option just renders the text of results.js.erb in the browser.

CChandler81
  • 347
  • 3
  • 14
  • Do you see the pdf data in the browser? – Pavan Aug 23 '17 at 07:56
  • @Pavan Nope. In the server I see it process the controller action as PDF, run the query with the supplied criteria, and send then "Sent data Results_08/23/2017.pdf" but the browser does nothing. – CChandler81 Aug 23 '17 at 08:14
  • @CChandler81 Did you try this? https://stackoverflow.com/a/7293155/1154044 – cnnr Aug 23 '17 at 09:37
  • @cnnr The second example in that answer is exactly what I have in my format.pdf block above. – CChandler81 Aug 23 '17 at 10:12
  • @CChandler81 ,you added `remote: true` in form so when you submit the form ,always it's format will be `js`.I suggest remove the form and add two links only one with `js` format and other with `pdf` format unless you can change the logic in controller as per the `params` – Pitabas Prathal Aug 23 '17 at 10:53
  • @pitabasprathal As I said in my question, if I remove remote: true then the PDF button works but the View (js) one just renders the text of the associated .js file. Regular links won't work because I need to be able to submit the form. – CChandler81 Aug 23 '17 at 10:58
  • @CChandler81 Do you have the `application.pdf.erb` file.why don't you use `wicked_pdf gem` – Pitabas Prathal Aug 23 '17 at 11:31
  • @pitabasprathal Because I already use Prawn for other things and I don't want to simply generate PDFs from HTML. I find Prawn to be a much more flexible PDF generator. I don't think Wicked PDF wouldn't solve my problem anyway. – CChandler81 Aug 23 '17 at 11:52

1 Answers1

0

Here's how I got this to work (for now, anyway):

Removed the PDF button from the form, forcing the user to submit the form via ajax and view the results in the browser.

Added a link in the report_results partial and passed back the IDs of the records resulting from the run in step 1 as a param:

<%= link_to "PDF", results_path(ids: @results.ids, format: 'pdf'), class: "btn", target: "_blank" %>

Added an if/else block to the controller to determine if the request to the action came from the form submission or the pdf link:

if params[:criteria]  
  @results = ...query based on params[:criteria]...  
elsif params[:ids]  
  @results = ...query based on params[:ids]...  
else  
  ...error handling...  
end

Hackey? Probably. Does it work? Yeah.

CChandler81
  • 347
  • 3
  • 14