2017-04-12 70 views
0

我正在寻找使用对虾PDF和导轨4.2提取所有记录到一个单一的PDF。对虾PDF和多记录记录

目前,我有他们生成的个人PDF格式的ID和运作良好。

显示在表

def show 
    @agreement = Agreement.find(params[:id]) 

    respond_to do |format| 
     format.html 
     format.pdf do 
     pdf = AgreementPdf.new(@agreement) 
     send_data pdf.render, filename: "Agreement - #{@agreement.entity}", type: "application/pdf", disposition: "inline" 
     end 
    end 
    end 

指数

<% @agreements.each do |agreement| %> 
     <tr> 
     <td><%= agreement.entity %></td> 
     <td><%= link_to 'Download', agreement_path(agreement.id, format: 'pdf'), :target => "_blank" %></td> 
      </tr> 
    <% end %> 

回答

1

首先,你必须添加新方法的路径的路线:

get '/agreements/all_records' => 'agreements#all_records', :as => :agreement_all_records

然后在视图中调用的方法有:

<td><%= link_to 'Download All Records', agreement_all_records_path(), :target => "_blank" %></td>

它看起来像这样的控制器:

def all_records 
 

 
    @agreements = Agreement.all 
 

 
    pdf = AgreementsPdf.new(@agreements) 
 
    send_data pdf.render,filename:'agreements.pdf',type:'application/pdf', disposition: 'inline' 
 
end

和报表可能看起来像这样(假设协议模型有id,名字) DS):

require 'prawn/table' 
 

 
class AgreementsPdf < PdfReport 
 
    TABLE_WIDTHS = [100, 100] 
 
    PAGE_MARGIN = [40, 40, 40, 40] 
 

 

 
    def initialize(agreements=[]) 
 
    super(:page_size => "LEGAL", margin: PAGE_MARGIN, :page_layout => :landscape) 
 
    @agreements = agreements 
 
    display_table 
 
    end 
 

 
    private 
 

 
    def display_table 
 
    if table_data.empty? 
 
     text "None data" 
 
    else 
 
     table(table_data,column_widths: TABLE_WIDTHS, :cell_style => { size: 10 })  
 
    end 
 
    end 
 

 
    def table_data 
 
    @table_data ||= @agreements.map { |e| [e.id, e.name] } 
 
    end 
 

 
end

希望这会给你一个想法

+0

你将如何调用视图中的变量? – DollarChills

+0

我编辑答案并添加视图和路线。我没有测试,只是代码来展示如何去做 –

+0

钉住它。谢谢你的帮助。 – DollarChills