Rails 3 ActionMailer 和 Wicked_PDF [英] Rails 3 ActionMailer and Wicked_PDF

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

问题描述

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

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

在我的网站上,我已经分别使用了 wicked_pdf 和 actionmailer.我可以使用 wicked_pdf 在网络应用程序中提供 pdf,也可以使用 ActionMailer 发送邮件,但我无法将呈现的 pdf 内容附加到 ActionMailer(针对内容进行了编辑):

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 => "webadmin@mydomain.com"

  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

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

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.

如果我尝试添加附件(如上所示),我会得到一个看起来大小合适的附件,只有附件的名称没有按预期显示,也不能作为 pdf 格式阅读.除此之外,我的邮件内容丢失了...

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

有没有人有在 Rails 3.0 中即时渲染 PDF 时使用 ActionMailer 的经验?

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

提前致谢!--丹

推荐答案

WickedPDF 可以渲染到文件,只需附加到电子邮件或保存到文件系统即可.

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

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

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)

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

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,

首先是使用上面第一篇文章中描述的hack:

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

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

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