将 Prawn PDF 保存为回形针附件? [英] Save a Prawn PDF as a Paperclip attachment?

查看:68
本文介绍了将 Prawn PDF 保存为回形针附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Prawn 和 Prawnto 向用户显示基于 PDF 的报告,但在某些情况下,我还想将 PDF 作为附件保存到我的模型之一.我的所有附件都使用 Paperclip.有人对如何做到这一点有任何建议吗?

I'm using Prawn and Prawnto to display a PDF-based reports to the user, but in some circumstances, I'd also like to save the PDF as an attachment to one of my models. I'm using Paperclip for all of my attachments. Does anyone have any suggestions on how to do this?

谢谢!

推荐答案

使用 prawnto 时,您需要评估 .pdf.prawn 模板中的变量.第二步是为回形针模拟一个真实的文件.

When using prawnto you will need to eval the variables in the .pdf.prawn template. Second step is to mimic a real file for paperclip.

  1. 生成 PDF:

  1. Generating the 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:

  • Save PDF with paperclip:

    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!
    

  • 希望这会有所帮助.

    这篇关于将 Prawn PDF 保存为回形针附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆