1

我有两个使用 Prawn PDF “即时”制作的 PDF。

PDF 文件称为 jobsheet.pdf 和 dispatch.pdf - 它们的 URL 是:

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

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

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

要创建 PDF,代码如下:

卡西斯控制器

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')

这甚至可能吗?

谢谢,

丹尼

4

2 回答 2

1

prawnto 文档看来,该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 
于 2010-06-07T12:22:30.713 回答
0

如果不使用 prawnto,以下适用于 prawn-rails 和 prawn_plus。

# in format.pdf
headers["Content-Disposition"] = "attachment; filename=\"file-#{model.id}.pdf\""
于 2021-11-14T08:18:05.657 回答