如何使用wicked_pdf将pdf文件保存在公用文件夹中

时间:2012-06-02 12:14:12

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 pdf-generation wicked-pdf

现在我正在使用Rails 3.0.0.i已经安装了gem wicked_pdf.Now想要将pdf文件保存在公共文件夹中。请帮助我。

3 个答案:

答案 0 :(得分:7)

如果您只需创建一个pdf而不显示它:

# 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

答案 1 :(得分:7)

这是如何在ruby on rails中转换pdf中的字符串的最佳答案 如果你不反对我提出一个答案:
有些服务会像JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9Ue. . .一样回复pdf 您可以使用以下命令从sting创建pdf文件:

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

然后您可以使用AcrobatReader或其他pdf阅读器打开pdf文件 希望它能帮到你。

答案 2 :(得分:5)

另外,你可以这样做:

render :pdf          => 'foo',
       :save_to_file => Rails.root.join('public', "foo.pdf"),
       :save_only    => true