ActionMailer中的Rails 3 Render Prawn pdf [英] Rails 3 Render Prawn pdf in ActionMailer

查看:64
本文介绍了ActionMailer中的Rails 3 Render Prawn pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ActionMailer中将虾pdf呈现为附件?我使用了delay_job,但不明白如何在行动邮件中(而非控制器中)呈现pdf文件。我应该使用哪种格式?

How to render prawn pdf as attachment in ActionMailer? I use delayed_job and don't understand, how could I render pdf-file in action mailer (not in controller). What format should I use?

推荐答案

您只需要告诉Prawn将PDF呈现为字符串,然后将其添加为电子邮件附件。有关附件的详细信息,请参见 ActionMailer文档

You just need to tell Prawn to render the PDF to a string, and then add that as an attachment to the email. See the ActionMailer docs for details on attachments.

这是一个例子:

class ReportPdf
  def initialize(report)
    @report = report
  end

  def render
    doc = Prawn::Document.new

    # Draw some stuff...
    doc.draw_text @report.title, :at => [100, 100], :size => 32

    # Return the PDF, rendered to a string
    doc.render
  end
end

class MyPdfMailer < ActionMailer::Base
  def report(report_id, recipient_email)
    report = Report.find(report_id)

    report_pdf_view = ReportPdf.new(report)

    report_pdf_content = report_pdf_view.render()

    attachments['report.pdf'] = {
      mime_type: 'application/pdf',
      content: report_pdf_content
    }
    mail(:to => recipient_email, :subject => "Your report is attached")
  end
end

这篇关于ActionMailer中的Rails 3 Render Prawn pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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