Rails 3 通过电子邮件发送一个邪恶的 pdf 问题

Rails 3 email a wicked pdf issue

我在 Rails 3.2 应用程序中有 gem 'wicked_pdf' 和 gem 'combine_pdf'。 如果我在浏览器中显示 pdf,效果很好。

我想通过电子邮件发送 pdf。我当前的代码发送电子邮件,但 pdf 只是垃圾:

这是控制器代码:

  def pdfemail
    @costprojects = Costproject.find(params[:costproject_ids])
    respond_to do |format|
      format.html
      format.pdf do
        pdf = CombinePDF.new
        @costprojects.each do |costproject|
          @costproject = costproject
          pdf2 = render_to_string pdf: "Costproject.pdf", template: "costprojects/viewproject", encoding: "UTF-8"
          pdf << CombinePDF.parse(pdf2)
          costproject.attachments.each do |attachment|
            pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( attachment.attach.url ) ) )
          end
        end
        pdf = pdf.to_pdf
        SendReport.send_report(pdf).deliver
        redirect_to :back
        flash[:notice] = 'Email containing pdf has been sent to you!'
      end
    end
  end

这是我的邮件:

  def send_report(pdf)
    tomail = "somebody@gmail.com"
    frommail = "somebody@gmail.com"
    attachments['Report.pdf'] = pdf
    mail(
        :to => tomail,
        :from => frommail,
        :subject => "Report pdf")
  end

同样 - 当我在浏览器中显示时,pdf 的格式工作正常。

感谢您的帮助!

See this question.

基本上您需要将其更改为:

attachments['Report.pdf'] = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "report",:template => "costprojects/viewproject") )