Rails 3 ActionMailer 和 Wicked_PDF

发布于 2024-10-26 02:40:02 字数 1196 浏览 5 评论 0原文

我正在尝试使用 ActionMailer 和 wicked_pdf 生成带有渲染 PDF 附件的电子邮件。

在我的网站上,我已经分别使用 wicked_pdf 和 actionmailer。我可以使用 wicked_pdf 在网络应用程序中提供 pdf,并且可以使用 ActionMailer 发送邮件,但我在将渲染的 pdf 内容附加到 ActionMailer 时遇到问题(已编辑内容):

class UserMailer < ActionMailer::Base
  default :from => "[email protected]"

  def generate_pdf(invoice)
    render :pdf => "test.pdf",
     :template => 'invoices/show.pdf.erb',
     :layout => 'pdf.html'
  end

  def email_invoice(invoice)
    @invoice = invoice
    attachments["invoice.pdf"] = {:mime_type => 'application/pdf',
                                  :encoding => 'Base64',
                                  :content => generate_pdf(@invoice)}
    mail :subject => "Your Invoice", :to => invoice.customer.email
  end
end

Using Railscasts 206 (Action Mailer in Rails 3 )作为指导,我可以发送包含我想要的丰富内容的电子邮件,前提是我不尝试添加渲染的附件。

如果我尝试添加附件(如上所示),我会得到一个看起来大小合适的附件,只是附件的名称未按预期显示,也无法以 pdf 形式读取。除此之外,我的电子邮件内容丢失了...

是否有人有在 Rails 3.0 中动态渲染 PDF 时使用 ActionMailer 的经验?

提前致谢! - 担

I'm trying to generate emails with rendered PDF attachements using ActionMailer and wicked_pdf.

On my site, I'm using already both wicked_pdf and actionmailer separately. I can use wicked_pdf to serve up a pdf in the web app, and can use ActionMailer to send mail, but I'm having trouble attaching rendered pdf content to an ActionMailer (edited for content):

class UserMailer < ActionMailer::Base
  default :from => "[email protected]"

  def generate_pdf(invoice)
    render :pdf => "test.pdf",
     :template => 'invoices/show.pdf.erb',
     :layout => 'pdf.html'
  end

  def email_invoice(invoice)
    @invoice = invoice
    attachments["invoice.pdf"] = {:mime_type => 'application/pdf',
                                  :encoding => 'Base64',
                                  :content => generate_pdf(@invoice)}
    mail :subject => "Your Invoice", :to => invoice.customer.email
  end
end

Using Railscasts 206 (Action Mailer in Rails 3) as a guide, I can send email with my desired rich content, only if I don't try to add my rendered attachment.

If I try to add the attachment (as shown above), I get an attachement of what looks to be the right size, only the name of the attachment doesn't come across as expected, nor is it readable as a pdf. In addition to that, the content of my email is missing...

Does anyone have any experience using ActionMailer while rendering the PDF on the fly in Rails 3.0?

Thanks in advance!
--Dan

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

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

发布评论

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

评论(4

睡美人的小仙女 2024-11-02 02:40:02

WickedPDF 可以渲染为文件,以便附加到电子邮件或保存到文件系统。

上面的方法对您不起作用,因为 generate_pdf 是邮件程序上的一种方法,它返回邮件对象(不是您想要的 PDF)

此外,ActionMailer 中存在一个错误,导致消息如果您尝试在方法本身中调用 render

http://chopmode.wordpress.com/2011/03/25/render_to_string-causes-subsequent-mail-rendering-to-fail/

https://rails.lighthouseapp.com/projects/8994/tickets/6623 -render_to_string-in-mailer-causes-subsequent-render-to-fail

有两种方法可以实现此目的,

第一种是使用上面第一篇文章中描述的技巧:

def email_invoice(invoice)
  @invoice = invoice
  attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
  )
  self.instance_variable_set(:@lookup_context, nil)
  mail :subject => "Your Invoice", :to => invoice.customer.email
end

或者,您可以设置像这样的块中的附件:

def email_invoice(invoice)
  @invoice = invoice
  mail(:subject => 'Your Invoice', :to => invoice.customer.email) do |format|
    format.text
    format.pdf do
      attachments['invoice.pdf'] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
      )
    end
  end
end

WickedPDF can render to a file just fine to attach to an email or save to the filesystem.

Your method above won't work for you because generate_pdf is a method on the mailer, that returns a mail object (not the PDF you wanted)

Also, there is a bug in ActionMailer that causes the message to be malformed if you try to call render in the method itself

http://chopmode.wordpress.com/2011/03/25/render_to_string-causes-subsequent-mail-rendering-to-fail/

https://rails.lighthouseapp.com/projects/8994/tickets/6623-render_to_string-in-mailer-causes-subsequent-render-to-fail

There are 2 ways you can make this work,

The first is to use the hack described in the first article above:

def email_invoice(invoice)
  @invoice = invoice
  attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
  )
  self.instance_variable_set(:@lookup_context, nil)
  mail :subject => "Your Invoice", :to => invoice.customer.email
end

Or, you can set the attachment in a block like so:

def email_invoice(invoice)
  @invoice = invoice
  mail(:subject => 'Your Invoice', :to => invoice.customer.email) do |format|
    format.text
    format.pdf do
      attachments['invoice.pdf'] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
      )
    end
  end
end
饮湿 2024-11-02 02:40:02

我使用了上面的 Unixmonkey 解决方案,但是当我升级到 Rails 3.1.rc4 时,设置 @lookup_context 实例变量不再起作用。也许还有另一种方法可以实现对查找上下文的相同清除,但目前,在邮件块中设置附件效果很好,如下所示:

  def results_email(participant, program)
    mail(:to => participant.email,
         :subject => "my subject") do |format|
      format.text
      format.html
      format.pdf do
        attachments['trust_quotient_results.pdf'] = WickedPdf.new.pdf_from_string(
          render_to_string :pdf => "results",
               :template => '/test_sessions/results.pdf.erb',
               :layout => 'pdf.html')
      end
   end
  end

I used of Unixmonkey's solutions above, but then when I upgraded to rails 3.1.rc4 setting the @lookup_context instance variable no longer worked. Perhaps there's another way to achieve the same clearing of the lookup context, but for now, setting the attachment in the mail block works fine like so:

  def results_email(participant, program)
    mail(:to => participant.email,
         :subject => "my subject") do |format|
      format.text
      format.html
      format.pdf do
        attachments['trust_quotient_results.pdf'] = WickedPdf.new.pdf_from_string(
          render_to_string :pdf => "results",
               :template => '/test_sessions/results.pdf.erb',
               :layout => 'pdf.html')
      end
   end
  end
久伴你 2024-11-02 02:40:02

以下是我解决此问题的方法:

  1. 删除 wicked_pdf
  2. 安装的虾 (https:// github.com/sandal/prawn/wiki/Using-Prawn-in-Rails

虽然 Prawn 在布局文档方面有点麻烦,但它可以轻松地传递邮件附件......

Heres' how I fixed this issue:

  1. Removed wicked_pdf
  2. Installed prawn (https://github.com/sandal/prawn/wiki/Using-Prawn-in-Rails)

While Prawn is/was a bit more cumbersome in laying out a document, it can easily sling around mail attachments...

瀟灑尐姊 2024-11-02 02:40:02

最好使用 PDFKit,例如:

class ReportMailer < ApplicationMailer

  def report(users:, amounts:)
    @users = users
    @amounts = amounts

    attachments["proveedores.pdf"] = PDFKit.new(
      render_to_string(
        pdf: 'bcra',
        template: 'reports/users.html.haml',
        layout: false,
        locals: { users: @users }
      )
    ).to_pdf

    mail subject: "Report - #{Date.today}"
  end
end

Better to use PDFKit, for example:

class ReportMailer < ApplicationMailer

  def report(users:, amounts:)
    @users = users
    @amounts = amounts

    attachments["proveedores.pdf"] = PDFKit.new(
      render_to_string(
        pdf: 'bcra',
        template: 'reports/users.html.haml',
        layout: false,
        locals: { users: @users }
      )
    ).to_pdf

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