Rails and Prawn PDF - add current item ID to filename?
Posted
by dannymcc
on Stack Overflow
See other posts from Stack Overflow
or by dannymcc
Published on 2010-06-07T10:26:27Z
Indexed on
2010/06/07
10:32 UTC
Read the original article
Hit count: 334
Hi Everyone,
I have two PDFs that are made "on the fly" using Prawn PDF.
The PDFs are called jobsheet.pdf and discharge.pdf - their URL's 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 PDF's I use the following links:
jobsheet_kase_path(@kase, :format => 'pdf')
discharge_kase_path(@kase, :format => 'pdf')
Is this even possible?
Thanks,
Danny
© Stack Overflow or respective owner