大虾PDF和多条记录
Prawn PDF and muliplte records
我希望使用 Prawn PDF 和 rails 4.2 将所有记录提取到一个 pdf 文件中。
目前我让它们通过 id 为个人 pdf 生成并且效果很好。
显示
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
索引在table
<% @agreements.each do |agreement| %>
<tr>
<td><%= agreement.entity %></td>
<td><%= link_to 'Download', agreement_path(agreement.id, format: 'pdf'), :target => "_blank" %></td>
</tr>
<% end %>
首先,您必须添加一个带有新方法路径的路由:
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、name 字段):
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
希望这会给你一个想法
我希望使用 Prawn PDF 和 rails 4.2 将所有记录提取到一个 pdf 文件中。
目前我让它们通过 id 为个人 pdf 生成并且效果很好。
显示
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
索引在table
<% @agreements.each do |agreement| %>
<tr>
<td><%= agreement.entity %></td>
<td><%= link_to 'Download', agreement_path(agreement.id, format: 'pdf'), :target => "_blank" %></td>
</tr>
<% end %>
首先,您必须添加一个带有新方法路径的路由:
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、name 字段):
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
希望这会给你一个想法