生成并链接到多个prawn pdfs单个动作

时间:2015-04-10 01:28:53

标签: ruby-on-rails pdf-generation prawn

我目前正在show行动中使用Prawn gem生成PDF:

format.pdf do
  pdf = ProcurementReportPdf.new @real_property_sale_project, current_user, view_context
  send_data pdf.render,
  filename: "ProcurementReport.pdf",
  type: "application/pdf",
  disposition: "inline"
end

我在show视图中访问pdf,如下所示:

<%= link_to '', "/real_property_sale_projects/#{@rpsp.id}.pdf", class: "fa fa-file-pdf-o right track-with-mixpanel", :data => { :event => "clicked bid export icon", tooltip: "export_bids" }, :title => "Procurement Report (PDF)", :target => "_blank" %>
# this line is for the scroll bar, so you can read the code 

我想为此操作生成第二份报告,但我不确定最佳方法吗?生成报告后,如何在我的视图中链接到它?

1 个答案:

答案 0 :(得分:0)

通过在控制器中定义以下方法解决了这个问题:

 def vendor_report
    @real_property_sale_project = RealPropertySaleProject.find params[:project_id]

    respond_to do |format|
      format.pdf do
        vendor_report = VendorReportPdf.new @real_property_sale_project, current_user, view_context
        send_data vendor_report.render,
          filename: "Vendor_Report.pdf",
          type: "application/pdf",
          disposition: "inline"
      end
    end
  end

添加了路线:

get 'vendor_report', to: 'real_property_sale_projects#vendor_report', as: 'vendor_report'

然后在show视图中添加了链接:

<%= link_to 'vendor report', vendor_report_path(project_id: @rpsp.id, format: 'pdf') %>