Rails和Prawn PDF - 将当前项目ID添加到文件名?

时间:2010-06-07 10:26:27

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

我有两个使用Prawn PDF“动态”制作的PDF。

PDF名称为jobsheet.pdf和discharge.pdf - 其网址为:

railsroot/kases/IDNO/jobsheet.pdf
railsroot/kases/IDNO/discharge.pdf

我正在尝试解决如何使用ID号自动附加文件名:

railsroot/kases/IDNO/jobsheet_IDNO.pdf
railsroot/kases/IDNO/discharge_IDNO.pdf

要创建PDF,代码如下:

Kases控制器

def jobsheet
    @kase = Kase.find(params[:id])

    respond_to do |format|
      format.html {} # jobsheet.html.erb
      format.xml  { render :xml => @kase }
      format.pdf { render :layout => false }

      prawnto :prawn => { 
                 :background => "#{RAILS_ROOT}/public/images/jobsheet.png", 
                 :left_margin => 0, 
                 :right_margin => 0, 
                 :top_margin => 0, 
                 :bottom_margin => 0, 
                 :page_size => 'A4' }
    end

  end

  # GET /kases/1
  # GET /kases/1.xml
  def discharge
    @kase = Kase.find(params[:id])

    respond_to do |format|
      format.html { } # discharge.html.erb
      format.xml  { render :xml => @kase }
      format.pdf { render :layout => false }

      prawnto :prawn => { 
                 :background => "#{RAILS_ROOT}/public/images/discharge.png", 
                 :left_margin => 0, 
                 :right_margin => 0, 
                 :top_margin => 0, 
                 :bottom_margin => 0, 
                 :page_size => 'A4' }
    end

  end

路线

 map.resources :kases, :member => { :discharge => :get }
  map.resources :kases, :member => { :jobsheet => :get }

要查看PDF,请使用以下链接:

jobsheet_kase_path(@kase, :format => 'pdf')
discharge_kase_path(@kase, :format => 'pdf')

这甚至可能吗?

谢谢,

丹尼

1 个答案:

答案 0 :(得分:1)

prawnto documentation看起来prawnto方法支持将文件名作为选项哈希的一部分传递。所以你应该能够做到这样的事情:

def jobsheet 
  @kase = Kase.find(params[:id]) 

  respond_to do |format| 
    format.html # jobsheet.html.erb 
    format.xml { render :xml => @kase } 
    format.pdf { render :layout => false } 

    prawnto :filename => "jobsheet_#{@kase.id}", :prawn => {  
      :background => "#{RAILS_ROOT}/public/images/jobsheet.png",          
      :left_margin => 0,  
      :right_margin => 0,  
      :top_margin => 0,  
      :bottom_margin => 0,  
      :page_size => 'A4' } 
  end 
end