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

发布于 2024-09-05 07:13:13 字数 1923 浏览 12 评论 0原文

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

这些 PDF 称为 jobsheet.pdf 和 discharge.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,代码如下:

Kases Controller

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

这可能吗?

谢谢,

丹尼

I have two PDFs that are made "on the fly" using Prawn PDF.

The PDFs are called jobsheet.pdf and discharge.pdf - their URLs are:

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

I am trying to work out how to automagically append the filename with the ID number:

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

To create the PDFs the code is as follows:

Kases Controller

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

Routes

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

To view the PDFs I use the following links:

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

Is this even possible?

Thanks,

Danny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-09-12 07:13:13

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 

From the prawnto documentation it looks like the prawnto method supports passing a file name as part of the options hash. So you should be able to do something like this:

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 
待天淡蓝洁白时 2024-09-12 07:13:13

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

# in format.pdf
headers["Content-Disposition"] = "attachment; filename=\"file-#{model.id}.pdf\""

If not using prawnto, the following works for prawn-rails and prawn_plus.

# in format.pdf
headers["Content-Disposition"] = "attachment; filename=\"file-#{model.id}.pdf\""
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文