将 Prawn PDF 保存为回形针附件?

发布于 2024-10-18 10:58:38 字数 115 浏览 6 评论 0原文

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

谢谢!

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?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痕至 2024-10-25 10:58:38

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

  1. 生成 PDF:

    #找到你想要的prawwnto模板
    模板 = File.read("#{RAILS_ROOT}/app/views/reports/your_report.pdf.prawn")
    
    pdf = Prawn::Document.new(:page_size => 'A4', :your_options => :etc)
    
    pdf.instance_eval 做
      @report = find_report #这里放置pdf模板需要的所有局部变量
      eval(template) #this 使用您的变量评估模板
    结尾
    
    附件=pdf.render
    
  2. 用回形针保存 PDF:

    file = StringIO.new(attachment) #模拟真实的上传文件
    file.class.class_eval { attr_accessor :original_filename, :content_type } #添加回形针需要的属性
    file.original_filename = "your_report.pdf"
    file.content_type =“应用程序/pdf”
    
    
    #现在只需使用文件对象保存到回形针关联。
    
    
    # 假设您的回形针关联名为“pdf_report”
    @report_store.pdf_report = 文件
    @report_store.保存!
    

希望这会有所帮助。

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. 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
    
  2. 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!
    

Hope this helps.

缪败 2024-10-25 10:58:38

如果您只是将对该 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

It should work if you just pass a File reference to that PDF to 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
故乡的云 2024-10-25 10:58:38

我通过相反的方式使其无需实例评估即可工作:在模型中生成 PDF 并在控制器中渲染它

在模型中:

  def generate_pdf
    Prawn::Document.new(:page_size => 'A4', :top_margin => 0, :left_margin => 0) do |pdf|
        <your pdf code here>
        <copy paste from your template>
    end.render
  end

然后可以将其作为邮件附件发送:

attachment = generate_pdf
mail = Notifier.send_pdf(attachment)
mail.deliver

或者在浏览器窗口中渲染它控制器 :

send_data your_model.generate_pdf, :type => "application/pdf", :disposition => 'inline'

I got it working without instance eval by turning it the other way around : generate the PDF in your model and render it in you controller

In a model :

  def generate_pdf
    Prawn::Document.new(:page_size => 'A4', :top_margin => 0, :left_margin => 0) do |pdf|
        <your pdf code here>
        <copy paste from your template>
    end.render
  end

You can then send it as a mail attachment :

attachment = generate_pdf
mail = Notifier.send_pdf(attachment)
mail.deliver

Or render it in your browser windows in your controller :

send_data your_model.generate_pdf, :type => "application/pdf", :disposition => 'inline'
自此以后,行同陌路 2024-10-25 10:58:38

这对我有用

pdf = Prawn::Document.new(:page_size => "LETTER", :page_layout => :landscape)
pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
current_user.certificate = File.open("#{Rails.root}/app/pdfs/x.pdf")
current_user.save!

其中 certificate 是我的回形针附件在模型中保存的内容:

class User < ActiveRecord::Base
  has_attached_file :certificate

This worked for me

pdf = Prawn::Document.new(:page_size => "LETTER", :page_layout => :landscape)
pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
current_user.certificate = File.open("#{Rails.root}/app/pdfs/x.pdf")
current_user.save!

Where certificate is what my paperclip attachment is saved as in the model:

class User < ActiveRecord::Base
  has_attached_file :certificate
如梦 2024-10-25 10:58:38

@Adam Albrecht,您将把图像保存为附件,但是为了将 pdf 保存为附件,您需要再添加一个验证 -

****validates_attachment :document, content_type: { content_type: 'application/ pdf' }****

@Adam Albrecht,You will be saving image as a attachment but for saving pdf as an attachment you need to add one more validation -

****validates_attachment :document, content_type: { content_type: 'application/pdf' }****

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文