Rails 3 - 从视图中渲染 PDF 并附加到电子邮件

发布于 2024-11-02 20:24:39 字数 1041 浏览 7 评论 0原文

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

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

这是我的 actionmailer 中的内容:

class Sendpdf < ActionMailer::Base
default :from => "[email protected]"

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 有关。看来这个问题应该有一个简单的答案。有人可以帮忙吗?

更新 我很感激您在下面的答案中提出用作替代选项的建议。但是,我真的很想了解如何将视图呈现为 PDF 并将其附加到我的电子邮件中。如果需要的话,我愿意使用不同的东西,比如虾或其他东西。

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 => "[email protected]"

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

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?

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.

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

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

发布评论

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

评论(2

鼻尖触碰 2024-11-09 20:24:39

按照您想要的方式执行此操作的 2 种好方法:

1:在控制器中创建 pdf,然后将其作为参数发送到电子邮件。

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

# mailer
def send_report(user, pdf)
  @user = user
  attachments['MyPDF.pdf'] = pdf
  mail(:to => user.email, :subject => "awesome pdf, check it")
end

2:直接在邮件程序中创建pdf(稍微复杂一些,但可以从模型中调用)

def send_report(user)
  @user = user
  mail(:to => user.email, :subject => "awesome pdf, check it") do |format|
    format.text # renders send_report.text.erb for body of email
    format.pdf do
      attachments['MyPDF.pdf'] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => 'MyPDF',:template => 'reports/show.pdf.erb')
      )
    end
  end
end

2 good ways to do this the way you want:

1: Create the pdf in the controller, then send that to the email as a param.

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

# mailer
def send_report(user, pdf)
  @user = user
  attachments['MyPDF.pdf'] = pdf
  mail(:to => user.email, :subject => "awesome pdf, check it")
end

2: Create the pdf in the mailer directly (a little more involved, but can be called from a model)

def send_report(user)
  @user = user
  mail(:to => user.email, :subject => "awesome pdf, check it") do |format|
    format.text # renders send_report.text.erb for body of email
    format.pdf do
      attachments['MyPDF.pdf'] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => 'MyPDF',:template => 'reports/show.pdf.erb')
      )
    end
  end
end
画中仙 2024-11-09 20:24:39

有两种方法。

  1. 或者,您希望将 pdf 嵌入到您发送的电子邮件中,这样当用户从电子邮件下载 pdf 时,就不会请求为各自的控制器渲染新的 pdf 操作。
    我不知道如何有效地做到这一点,因为我以前从未这样做过。

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

第二种方法的示例代码(提供的代码是使用 PDFkit 编写的,因此请相应更改):

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 ,以及 < code>to_pdf 方法,按照 wickedPDF。

然后,您可以简单地在电子邮件中传递 pdf 下载的链接,如下所示

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

或根据 wickedPDF 的任何适合的内容。

There are 2 ways for it.

  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.

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

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

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

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") %>

or whatever suits as per wickedPDF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文