Rails 和 Prawn PDF - 将当前项目 ID 添加到文件名?
我有两个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从prawnto文档来看,
prawnto
方法支持传递文件名作为选项哈希的一部分。所以你应该能够做这样的事情: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:如果不使用 prawnto,以下内容适用于 prawn-rails 和 prawn_plus。
If not using prawnto, the following works for prawn-rails and prawn_plus.