简体   繁体   English

保存表单并呈现PDF

[英]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. 也可以通过这些更改生成pdf。 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 . 我的表单中有2个按钮,这些按钮将通过ajax remote: true发送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. 另一个按钮value=print应该保存更改,呈现pdf,将其发送到浏览器并关闭表单。

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 部分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? 有没有更好的方法来存储和存储并渲染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. 1)生成pdf,将其保存在您的文件系统中,并为用户提供下载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). 2)生成pdf,将其保存在tmp位置,然后使用send_file将其直接发送给用户(在浏览器中打开“保存文件”对话框)。

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? render_pdf是否会将文件发送回用户,或者是否将文件加载到他们的浏览器中,或者执行其他操作?

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. 编辑 :一种更好的处理方法是让show操作以正常方式在页面上为html请求显示一些产品信息,并为pdf请求返回产品信息的pdf。 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: 现在,您可以有一个常规的显示页面,该页面显示有关产品的信息,并具有指向pdf的链接:

<%= 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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