2017-04-12 46 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