Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save pdf file inside the public folder using wicked_pdf

Right Now i am using Rails 3.0.0.i already installed the gem wicked_pdf.Now to want to save the pdf file inside the public folder.please help me.

like image 352
chandrashekar Avatar asked Jun 02 '12 12:06

chandrashekar


3 Answers

If you need to just create a pdf and not display it:

# create a pdf from a string
pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')

# create a pdf from string using templates, layouts and content option for header or footer
WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "pdf_file.pdf", :template => 'templates/pdf.html.erb', :layout => 'pdfs/layout_pdf'), 
    :footer => {:content => render_to_string({:template => 'templates/pdf_footer.html.erb', :layout => 'pdfs/layout_pdf'})}

# or from your controller, using views & templates and all wicked_pdf options as normal
pdf = render_to_string :pdf => "some_file_name"

# then save to a file
save_path = Rails.root.join('pdfs','filename.pdf')
File.open(save_path, 'wb') do |file|
  file << pdf
end
like image 145
arcooverbeek Avatar answered Nov 02 '22 15:11

arcooverbeek


This is top answer on How to convert string in pdf in ruby on rails
And if you aren't against i put one of the answer:
Some services returns pdf as a string like: JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9Ue. . .
You can create a pdf file from the sting, with:

f = File.new("/tmp/file.pdf", "w")
f.write(Base64.decode64(invoice[:file]).force_encoding('UTF-8'))
f.close

And then you can open pdf file with AcrobatReader or another pdf reader.
Wish it helps.

like image 20
itsnikolay Avatar answered Nov 02 '22 13:11

itsnikolay


Also, you can do this:

render :pdf          => 'foo',
       :save_to_file => Rails.root.join('public', "foo.pdf"),
       :save_only    => true
like image 23
Unixmonkey Avatar answered Nov 02 '22 15:11

Unixmonkey