简体   繁体   中英

Save Form and render PDF

some information why i have this situation:

in my application, the user can change some data in a form and store it. it also possible to generate a pdf with these changes. the user had to save it, open the dialog again and can print now these changes.

thats not a good way. i changed it to one action. now the user can save the changes, form will be closed. if the user want to print the changes, he just can push the print button, the changed data will be saved, and after this the document will be printed.

I had 2 buttons in my form which will be send via ajax remote: true . One button is the default submit button to save the changes and close the form.

The other button value=print should save the changes, render the pdf, send it to the browser, and close the form.

this works pretty well. but looks not good to me.

the form

<%= form_for @product, remote: true do |f| %>
  <!-- some boring fields -->
  <button class="btn btn-default glyphicon glyphicon-print" type="submit" value="pdf" name="print"></button>
  <button type="button" class="btn btn-default" data-dismiss="modal"><%=t('forms.close')%></button>
  <button class="btn btn-primary" type="submit"><%=t('forms.save')%></button>
<% end %>

there you can see the both submit buttons.

the controller actions

def show
  # just generate the pdf and render it to browser
  # render_pdf will perform the send_file method with default :attachment
  render_pdf PdfEngine.render params
end

def update
  @product = Product.find params[:id]
  # do the update logic
  render :create 
end

the partial create.js.erb

<% if params[:print] %>
  location.href='<%=product_path(@product)%>'
<% else %>
  // do some blinky stuff
  // close the form and so one
<% end %>

is there a better way to store the and to store and render pdf?

There's a couple of different ways you could handle this.

1) generate the pdf, save it in your filesytem, and give the user a link to download the pdf.

2) generate the pdf, save it in a tmp location, and use send_file to send it directly to the user (making a "Save file" dialog open in their browser).

It's not clear to me which of these you are doing at the moment. Does render_pdf send the file back to the user, or does it load the file in their browser, or do something else?

I think your question is a bit too vague and open-ended: it would be better for you to choose what you want to happen and then get help making that happen.

EDIT : A nicer way to handle this is to have the show action display some product info on the page, in the normal way, for html requests, and to return a pdf of the product info for pdf requests. ie

def show
  @product = Product.find params[:id]
  respond_to do |format|
    format.html #default is to render the show template, let it do that
    format.pdf do 
      render_pdf PdfEngine.render params #not sure exactly what this requires from params
    end
  end
end

You will need to do this in your config, if you haven't already

#in config/initializers/mime_types.rb
Mime::Type.register 'application/pdf', :pdf

Now, you can have a regular show page which shows info about the product, and have a link to the pdf on it:

<%= link_to "Download PDF", product_path(@product, :format => "pdf") %>

Also i would have your create action, if successful, redirect to the show page since this is the normal behaviour:

def update
  @product = Product.find params[:id]
  @updated = @product.update_attributes(params[:product])
  if @updated 
    redirect_to product_path(@product)
  else
    render :action => "edit" #or whatever
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM