Ada*_*cht 22 ruby-on-rails prawn paperclip prawnto ruby-on-rails-3
我正在使用Prawn和Prawnto向用户显示基于PDF的报告,但在某些情况下,我还想将PDF保存为我的某个模型的附件.我正在使用Paperclip来处理我的所有附件.有没有人对如何做到这一点有任何建议?
谢谢!
Dav*_*low 26
使用prawnto时,您需要评估.pdf.prawn模板中的变量.第二步是模仿回形针的真实文件.
生成PDF:
#find the prawwnto template you want
template = File.read("#{RAILS_ROOT}/app/views/reports/your_report.pdf.prawn")
pdf = Prawn::Document.new(:page_size => 'A4', :your_options => :etc)
pdf.instance_eval do
@report = find_report #put here, all local variables that the pdf template needs
eval(template) #this evaluates the template with your variables
end
attachment = pdf.render
Run Code Online (Sandbox Code Playgroud)使用回形针保存PDF:
file = StringIO.new(attachment) #mimic a real upload file
file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs
file.original_filename = "your_report.pdf"
file.content_type = "application/pdf"
#now just use the file object to save to the Paperclip association.
# assuming your Paperclip association is named "pdf_report"
@report_store.pdf_report = file
@report_store.save!
Run Code Online (Sandbox Code Playgroud)希望这可以帮助.
jem*_*ger 10
如果您只是将对该PDF的文件引用传递给Paperclip,它应该可以工作.
require 'prawn'
pdf = Prawn::Document.new
pdf.text("Prawn Rocks")
pdf.render_file('/path/to/prawn.pdf')
pdf_file = File.open('/path/to/prawn.pdf')
# assuming your Paperclip association is named "pdf_attachment"
my_model.pdf_attachment = pdf_file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8444 次 |
| 最近记录: |