Rails 3-从视图中呈现PDF并附加到电子邮件 [英] Rails 3 -Render PDF from view and attach to email

查看:149
本文介绍了Rails 3-从视图中呈现PDF并附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Wicked_pdf将视图呈现为PDF格式,并使用actionmailer来发送电子邮件,但是我无法让它们协同工作。我想使用actionmailer将某个视图的PDF版本附加到电子邮件,然后通过单击链接或按钮将其发送出去。我有一个link_to命令可以发送电子邮件。这是我的生成电子邮件的控制器:

I have been using Wicked_pdf to render a view as a PDF and actionmailer to send emails, but I can't get them to work together. I want to attach a PDF version of a certain view to an email using actionmailer and send it out by clicking a link or a button. I have a link_to command that sends out an email. Here is my controller that gets the email generated:

def sendemail
 @user = User.find(params[:id])
 Sendpdf.send_report(@user).deliver
 redirect_to user_path(@user)
 flash[:notice] = 'Email has been sent!'
end  

这是我的动作邮件程序中的内容:

Here is what I have in my actionmailer:

class Sendpdf < ActionMailer::Base
default :from => "myemail@email.com"

def send_report(user)
@user = user
  attachment "application/pdf" do |a|
  a.body = #Something should go here, maybe WickedPDF.new.something?
  a.filename = 'MyPDF'
end     
mail(:to => user.email, :subject => "awesome pdf, check it")
end

end

我见过很多问题和答案,大多数都与Prawn有关。似乎应该对此有一个简单的答案。有人可以帮忙吗?

I have seen many questions and answers, most dealing with Prawn. It seems like there should be a simple answer to this. Can anyone help?

更新,我很高兴能在以下答案中将其用作替代选项。但是,我真的很想学习如何将视图呈现为PDF并将其附加到我的电子邮件中。我愿意使用其他类似Prawn的东西,或者在需要时使用其他东西。

UPDATE I'm grateful for a suggestion to use as an alternative option in the answer below. However, I would really like to learn how to render a view as a PDF and attach it to my email. I am open to using something different like Prawn or anything else if I need to.

推荐答案

有2种方法。


  1. 或者,您都希望将pdf嵌入到您要发送的电子邮件中,以便当用户从电子邮件,没有要求为您各自的控制器呈现新的pdf操作。

    我不知道如何有效地执行此操作,因为我以前从未这样做过。

  1. Either, you want the pdf to be embedded in the email you are sending, so that when the user downloads the pdf from the email, there is no request to the render new pdf action for your respective controller.
    I don't know how to do this efficiently because I have never done this before.

或者,您只需在电子邮件中提供指向pdf的链接,当用户单击它时,现在将调用创建pdf的操作,然后用户可以下载它。 br>
这样,如果服务器上有很多下载pdf文件的负担,则可以将这些请求重定向到其他地方。简而言之,效率范围很大。

Or, you just provide a link to the pdf in your email and when the user clicks on it, now the action for creating the pdf is called, and then the user can download it.
This way, if there is a lot of burden on the server for the downloading of the pdf's, you can redirect these requests somewhere else. In short, there is a huge scope for efficiency.

第二种方法的示例代码(编写的代码已编写通过使用PDFkit,因此进行相应的更改):

A sample code for the 2nd method(code provided was written by using PDFkit, so change accordingly):

class PdfsController < ApplicationController
  def pdf
    respond_to do |format|
      format.pdf { render :text => wickedPDF.new( Pdf.find(params[:id]).content ).to_pdf }
    end
  end
...
end

按如下所示替换 Pdf.find(params [:id])。content 您的选择,以及根据wickedPDF的 to_pdf 方法。

Replace the Pdf.find(params[:id]).content as per your choice, and the to_pdf method, as per wickedPDF.

然后,您只需传递链接即可像这样在您的电子邮件中pdf下载

Then, you can simply pass the link for the pdf download in your email like this

<%= link_to "Download", pdf_pdf_path(pdf, :format => "pdf") %>

或其他适用于wickedPDF的东西。

or whatever suits as per wickedPDF.

这篇关于Rails 3-从视图中呈现PDF并附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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