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

cha*_*kar 4 pdf-generation ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 wicked-pdf

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

小智 8

如果您只需创建一个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
Run Code Online (Sandbox Code Playgroud)


its*_*lay 7

这是关于如何在ruby on rails中转换pdf中的字符串的最佳答案
如果你不反对我提出了一个答案:
某些服务返回pdf作为一种刺痛,JVBERi0xLjQKJeLjz9MKNCAwIG9iago8PC9Ue. . .
你可以从sting创建一个pdf文件,其中:

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

然后你可以用AcrobatReader另一个pdf阅读器打开pdf文件.
希望它会有所帮助.


Uni*_*key 5

此外,您可以这样做:

render :pdf          => 'foo',
       :save_to_file => Rails.root.join('public', "foo.pdf"),
       :save_only    => true
Run Code Online (Sandbox Code Playgroud)

  • @ abhishek77in您可以在大多数现代的Heroku堆栈中写入文件系统,但不能保证如果dyno重新启动,它将继续存在。在这种情况下,最好将文件保存到Amazon S3存储桶或类似的文件中。如果需要临时保存到文件系统,最好使用`/ tmp`。 (2认同)