大虾PDF与Rails邮件程序?

发布于 2024-08-31 04:19:46 字数 291 浏览 18 评论 0原文

我已经成功创建了一封在创建 Kase 时发送的电子邮件,但现在我需要附加由 Prawn 和 Prawno 即时创建的 PDF。基本上,当您访问 application.com/kase/1 等 kase 时,您只需在 URL 后面附加 .pdf,即 application.com/kase/1。

我花了很长时间让 PDF 工作并看起来像我想要的那样,但我不知道如何将 PDF 添加到自动发送电子邮件中 ​​- 主要是因为我不知道如何在它自动生成时为其提供链接。

有没有人设法让它发挥作用?

谢谢,

丹尼

I have successfully created an email that sends on creation of a Kase, but now I need to attach a PDF that is created on the fly by Prawn and Prawno. Basically when you visit a kase such as application.com/kase/1 you just append the URL with .pdf i.e. application.com/kase/1.

I spent ages getting the PDF to work and look how I wanted, but I can't figure out how to add the PDF to an auto sending email - mainly because I cannot work out how to give it a link as it's auto generated.

Has anyone ever managed to get this to work?

Thanks,

Danny

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

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

发布评论

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

评论(4

旧情勿念 2024-09-07 04:19:46

我想如果你将生成的 pdf 存储在某个地方会更好 - 用于缓存目的等。
但使用当前配置,您可以使用 Net::HTTP 读取生成的页面并附加响应:

require 'net/http'

def your_mailer_method(record)
  #...
  attachment "application/pdf" do |a|
    a.body = Net::HTTP.get('yourdomain.com', "/kase/#{record.id}.pdf")
    a.filename="your_pdf_name.pdf"
  end  
end

I suppose it would be better if you store generated pdf somewhere - for caching purposes, etc.
But with current configuration, you can read generated page with Net::HTTP and attach response:

require 'net/http'

def your_mailer_method(record)
  #...
  attachment "application/pdf" do |a|
    a.body = Net::HTTP.get('yourdomain.com', "/kase/#{record.id}.pdf")
    a.filename="your_pdf_name.pdf"
  end  
end
夏至、离别 2024-09-07 04:19:46

您确实应该考虑不使用 Prawnto,并创建 Prawn::Document 的子类来执行您需要的操作。然后,在您的控制器和邮件程序代码中,它应该只是:

MyReport.new.render

请参阅有关此的 Prawn 文档:

http://wiki.github.com/sandal/prawn/using-prawn-in-rails

You really should consider just not using Prawnto, and creating a subclass of Prawn::Document to do what you need. Then, in both your controller and your mailer code, it should just be:

MyReport.new.render

See the Prawn documentation on this:

http://wiki.github.com/sandal/prawn/using-prawn-in-rails

天冷不及心凉 2024-09-07 04:19:46

对于较新的,您实际上不需要再次发送请求,当您可以时 ::

mail.attachments["invoice.pdf"] = {:mime_type => "application/pdf" , :content => pdf_generator}

不这样做 :: 只需

send_data pdf.render , :filename => file_name_here , :type => "application/pdf"

这样做 ::

pdf.render , :filename => file_name_here , :type => "application/pdf"

不要 send_data,只需在电子邮件附件中呈现该 pdf正如第一个片段中提到的。

事实上,我只是在github上写了一个Gist。

For the newer ones, you dont really need to send a request again, when you can ::

mail.attachments["invoice.pdf"] = {:mime_type => "application/pdf" , :content => pdf_generator}

Instead of doing this ::

send_data pdf.render , :filename => file_name_here , :type => "application/pdf"

just do this ::

pdf.render , :filename => file_name_here , :type => "application/pdf"

Do not send_data, just render that pdf in your email attachment as mentioned in the first snippet.

In fact, i just wrote a Gist on github.

太阳哥哥 2024-09-07 04:19:46

这段代码对我有用

def send_file(file, subject, text, to_email)
      @subject = subject
      @text = text
      attachments["#{invoice.invoice_number}.pdf"] = file
      from_email = [email protected]      
mail(:to => to_email, :from => from_email, :subject=> subject)
  end

This code works for me

def send_file(file, subject, text, to_email)
      @subject = subject
      @text = text
      attachments["#{invoice.invoice_number}.pdf"] = file
      from_email = [email protected]      
mail(:to => to_email, :from => from_email, :subject=> subject)
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文