How to set prawn pdf file name in Ruby on Rails?

I have a controller action that responds to html and pdf format, for example:

def detail @record = Model.find(params[:id]) respond_to do |format| format.html # detail.html.erb format.pdf { render :layout => false } #detail.pdf.prawn end end 

but when I receive the file, it comes with the name: 1.pdf 2.pdf depending on params[:id] how to set the file name to myfile.pdf

- UPDATE -

An example of my detail.pdf.prawn file

 pdf.font "Helvetica" pdf.image open("http://localhost:3000/images/myImage.png"),:position => :left,:width=>100 pdf.text "some text" pdf.table(someData,:cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table| table.row(0).style :background_color => 'D3D3D3' table.column(0..1).style(:align => :left) table.column(2..4).style(:align => :center) table.column(0).width = 100 table.column(1).width = 250 table.column(3..4).width = 68 table.row(2).column(0..2).borders = [] table.row(2).column(3).style(:font_style => :bold, :align => :right) end
pdf.font "Helvetica" pdf.image open("http://localhost:3000/images/myImage.png"),:position => :left,:width=>100 pdf.text "some text" pdf.table(someData,:cell_style => { :border_width => 0.1,:border_color=> 'C1C1C1' }) do |table| table.row(0).style :background_color => 'D3D3D3' table.column(0..1).style(:align => :left) table.column(2..4).style(:align => :center) table.column(0).width = 100 table.column(1).width = 250 table.column(3..4).width = 68 table.row(2).column(0..2).borders = [] table.row(2).column(3).style(:font_style => :bold, :align => :right) end 

and format.pdf { render :layout => false } in the controller displays a pdf file with instructions on detail.pdf.prawn

+4
source share
3 answers

To clarify fl00r's answer, if you use prawnto, pdf settings can go in your controller, including the file name.

 def detail @record = Model.find(params[:id]) prawnto :prawn => { :page_size => 'A4', :left_margin => 50, :right_margin => 50, :top_margin => 80, :bottom_margin => 50}, :filename => @record.name, :inline => true #or false respond_to do |format| format.html # detail.html.erb format.pdf { render :layout => false } #detail.pdf.prawn end end 

If you create many different pdf files with prawnto, you will probably move the configuration to your own method. but if you only do what is normal in the controller.

NOTE. The pdf url will still be displayed, for example. 1.pdf But when they save the PDF, the file name will be displayed in the save dialog.

+4
source
 prawnto :filename => "myfile", :prawn => { ... } 
0
source

Actually, this post really helps my old pdf way. There is another way to use prawn Rails. You can check this link. Not mine, but a good tutorial to create it. Just saying, probably for those who are still confused how to do this.

I used this method before, then followed the method from this link. It’s actually interesting to do research on this.

0
source

All Articles