将虾PDF附加到电子邮件 [英] Attach Prawn pdf to email

查看:125
本文介绍了将虾PDF附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找年龄,但我仍然似乎无法弄清这个问题,

I have been searching for ages and I still cant seem to figure this out,

我当前正在使用虾宝石,我希望能够将 pdf 附加到我的电子邮件 >发票管理员 创建操作。我目前可以通过 http:// localhost:3000 / invoices / 297.pdf << c $ c> invoices / show 页面链接到生成的pdf。 / code>,但是我无法弄清楚如何将此pdf附加到电子邮件中。

I am currently using the prawn gem and I want to be able to attach a pdf to an email in my invoice controllers create action. I am currently able to link to a generated pdf from my invoices/show page through http://localhost:3000/invoices/297.pdf but I cant figure out how to attach this pdf to an email.

当前我不在任何地方存储生成的PDF,我的邮件看起来像这样

Currently I am not storing the generated PDF anywhere and my mailer looks like this

邮件程序

class InvoiceMailer < ActionMailer::Base
default :from => "notifications@example.com"

 def invoice_email(user)
   @user = user
   mail(:to => user.email, :subject => "Invoice Recieved")
 end
end

我的InvoicesController创建操作

And my InvoicesController Create Action

{...}
respond_to do |format|
  if @invoice.save
    InvoiceMailer.invoice_email(@invoice.user).deliver
    format.html { redirect_to invoice_url(@invoice, back_path: 
{...}

如何将我的发票添加为此邮件的附件?我需要将发票存储在之前的某个位置吗?我可以发送它吗?

How can I add my invoice as an attachment to this mailer? Do I need to store the invoice somewhere before I can send it?

推荐答案

如何执行此操作取决于PDF生成需要多长时间和/或要放置多少负载。在您的服务器上,以及您是否关心该问题。就我而言,我是根据用户生成的内容生成PDF的,而我看到的一些PDF创建时间在30多秒的范围内。

How you do this depends on how long the PDF generation takes and/or how much load it places on your server and whether you care about that. In my case I was generating PDFs from user-generated-content and I was seeing some PDF creation times up in the 30+ seconds range. Solving for that becomes a run-the-job-somewhere-else and cache-it (whether DB or cloud storage) issue.

@toddmetheny在为除最简单的解决方案之外的所有解决方案建议云存储方面是正确的。有趣的是,如果您托管在带有临时存储的Heroku之类的东西上,或者将PDF创建与发送电子邮件的电子邮件分开om用户请求(例如再次来自Heroku,是Web dynos与worker dynos)。您可以将PDF生成为本地临时文件,但是在需要在工作人员上运行的Mailer中阅读该临时文件时,该临时文件可能不存在。

@toddmetheny is quite right in suggesting cloud storage for all but the simplest solutions. It gets more interesting if you are hosting on something like Heroku with ephemeral storage, or if you are separating PDF creation from email sending from user requests (e.g. from Heroku again, web dynos vs worker dynos). You can generate the PDF to a local temporary file, but that temporary file may not be there by the time you need to read it in your Mailer running on a 'worker'.

非常简单的选项

在Mailer中,您可以将PDF生成到本地文件,将其读回内存,然后将其附加:

In your Mailer you could generate the PDF to a local file, read it back into memory, then attach it:

def invoice_email(user)
  @user = user

  attachments['filename_for_user.pdf'] = generate_pdf_content

  mail(:to => user.email, :subject => "Invoice Recieved")
end

private

  # I had troubles trying to get Prawn to generate 'in memory'
  # so just write to, then read, then unlink a tempfile
  def generate_pdf_content
    pdf = some_method_that_generates_a_prawn_pdf_object
    Tempfile.create do |f|
      pdf.render_file f
      f.flush
      File.read(f)
    end
  end

我建议您从这里开始进行操作。

I suggest you start here to get things working.

更复杂的选择

总有一天,您可能希望将生成PDF的工作(可能需要很长时间)与发送电子邮件的工作分开,通常这要快得多。我这样做的方法是有一个将PDF生成到本地Tempfile的作业,然后将该文件上传到S3存储并记录S3对象ID(我在数据库中执行此操作,您可以将其作为job属性来执行)

Someday you may want to separate the job that generates the PDF (which can take a long time) from the jobs that send email, which are usually much faster. The way I do this is to have a job that generates the PDF to a local Tempfile, then uploads that file to S3 storage and records the S3 object id (I do it in my database, you could just do it as a job attribute you push around).

完成后,该作业将创建一个新的邮件作业。邮件程序作业(可能在其他服务器上执行)将PDF从S3存储下载到本地文件,然后像上面的简单选项一样将其添加到电子邮件中。

When complete, that job creates a new mailer job. The mailer job (which may execute on a different server) downloads the PDF from S3 storage to a local file, then adds it to the email much like the simple option above.

这篇关于将虾PDF附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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