ruby-on-rails – Ruby on Rails&Prawn PDF – 创建客户列表
发布时间:2020-12-17 03:29:58 所属栏目:百科 来源:网络整理
导读:我正在尝试用Prawn生成PDF报告,我可以通过传递单个ID轻松地对show动作进行报告,但我想生成一个包含每个记录的报告.像标准的rails scaffold索引页面.使用rails它看起来像这样: % @customer.each do |customer| %%= customer.id %%= customer.name %%end% 简
我正在尝试用Prawn生成PDF报告,我可以通过传递单个ID轻松地对show动作进行报告,但我想生成一个包含每个记录的报告.像标准的rails scaffold索引页面.使用rails它看起来像这样:
<% @customer.each do |customer| %> <%= customer.id %> <%= customer.name %> <%end%> 简单! 但是我不确定如何用Prawn做到这一点.. 就像是: def index @customer = Customer.all respond_to do |format| format.html Prawn::Document.generate("customer_list.pdf") do |pdf| pdf.text "#{@customer.id} " pdf.text "#{@customer.name} " end end end 这显然是不对的. 有任何想法吗?谢谢. 解决方法
使用Prawn很容易,Gemfile =>宝贝’对虾’,捆绑
假设您有客户模型: customers_controller.rb def show @customer = Customer.find(params[:id]) respond_to do |format| format.html format.pdf do pdf = CustomerPdf.new(@customer) send_data pdf.render,filename: "customer_#{id}.pdf",type: "application/pdf",disposition: "inline" end end end 然后在apps目录下创建pdfs文件夹,并创建文件customer_pdf.rb class CustomerPdf< Prawn::Document def initialize(customer) super() @customer = customer text "Id##{@customer.id}" text "Name##{@customer.name}" end end show.html.erb <div class="pdf_link"> <%= link_to "E-version",customer_path(@customer,:format => "pdf") %> </div> 编辑: 并且不要忘记将pdf包含在config / initializers / mime_types.rb中 Mime::Type.register "application/pdf",:pdf (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |