我使用 Prawn 和 Prawnto 向用户显示基于 PDF 的报告,但在某些情况下,我还想将 PDF 保存为我的模型之一的附件。我所有的附件都使用回形针。有人对如何做到这一点有任何建议吗?
Thanks!
使用 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
-
用回形针保存 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!
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)