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?
如何在ActionMailer中将prawn pdf渲染为附件?我使用delayed_job并且不明白,我怎么能在动作邮件程序中渲染pdf文件(不在控制器中)。我应该使用什么格式?
3 个解决方案
#1
7
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.
您只需要告诉Prawn将PDF呈现为字符串,然后将其添加为电子邮件的附件。有关附件的详细信息,请参阅ActionMailer文档。
Here's an example:
这是一个例子:
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
#2
3
I followed the RailsCasts for PRAWN. Taken what has already been said and what I was trying to similarly accomplish, I set the attachment name and then created the PDF.
我跟着RailsCasts进行了PRAWN。采取已经说过的和我试图类似完成的内容,我设置了附件名称,然后创建了PDF。
InvoiceMailer:
def invoice_email(invoice)
@invoice = invoice
@user = @invoice.user
attachments["#{@invoice.id}.pdf"] = InvoicePdf.new(@invoice, view_context).render
mail(:to => @invoice.user.email,
:subject => "Invoice # #{@invoice.id}")
end
#3
0
My solution:
render_to_string('invoices/show.pdf', :type => :prawn)
PDF was corrupted because I didn't write block for mail function and multi-part email was incorrect.
PDF已损坏,因为我没有写邮件功能块和多部分电子邮件不正确。
#1
7
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.
您只需要告诉Prawn将PDF呈现为字符串,然后将其添加为电子邮件的附件。有关附件的详细信息,请参阅ActionMailer文档。
Here's an example:
这是一个例子:
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
#2
3
I followed the RailsCasts for PRAWN. Taken what has already been said and what I was trying to similarly accomplish, I set the attachment name and then created the PDF.
我跟着RailsCasts进行了PRAWN。采取已经说过的和我试图类似完成的内容,我设置了附件名称,然后创建了PDF。
InvoiceMailer:
def invoice_email(invoice)
@invoice = invoice
@user = @invoice.user
attachments["#{@invoice.id}.pdf"] = InvoicePdf.new(@invoice, view_context).render
mail(:to => @invoice.user.email,
:subject => "Invoice # #{@invoice.id}")
end
#3
0
My solution:
render_to_string('invoices/show.pdf', :type => :prawn)
PDF was corrupted because I didn't write block for mail function and multi-part email was incorrect.
PDF已损坏,因为我没有写邮件功能块和多部分电子邮件不正确。