22

ActionMailer と wicked_pdf を使用して、レンダリングされた PDF 添付ファイルを含む電子メールを生成しようとしています。

私のサイトでは、すでに wicked_pdf と actionmailer の両方を別々に使用しています。wicked_pdf を使用して Web アプリで pdf を提供し、ActionMailer を使用してメールを送信できますが、レンダリングされた pdf コンテンツを ActionMailer (コンテンツ用に編集) に添付するのに問題があります。

class UserMailer < ActionMailer::Base
  default :from => "webadmin@mydomain.com"

  def generate_pdf(invoice)
    render :pdf => "test.pdf",
     :template => 'invoices/show.pdf.erb',
     :layout => 'pdf.html'
  end

  def email_invoice(invoice)
    @invoice = invoice
    attachments["invoice.pdf"] = {:mime_type => 'application/pdf',
                                  :encoding => 'Base64',
                                  :content => generate_pdf(@invoice)}
    mail :subject => "Your Invoice", :to => invoice.customer.email
  end
end

Railscasts 206 (Rails 3 の Action Mailer) をガイドとして使用すると、レンダリングされた添付ファイルを追加しようとしない場合にのみ、目的のリッチ コンテンツを含むメールを送信できます。

添付ファイルを追加しようとすると (上記のように)、適切なサイズの添付ファイルが表示されますが、添付ファイルの名前だけが期待どおりに表示されず、pdf として読み取ることもできません。それに加えて、メールの内容が欠落している...

Rails 3.0 でオンザフライで PDF をレンダリングする際に ActionMailer を使用した経験のある人はいますか?

前もって感謝します!――ダン

4

4 に答える 4

32

WickedPDF は、電子メールに添付したり、ファイルシステムに保存したりするのに問題なくファイルにレンダリングできます。

上記generate_pdfのメソッドは、メーラーのメソッドであり、メールオブジェクトを返します (必要な PDF ではありません)。

また、ActionMailer にはバグがあり、メソッド自体で render を呼び出そうとすると、メッセージが不正な形式になります。

http://chopmode.wordpress.com/2011/03/25/render_to_string-causes-subsequent-mail-rendering-to-fail/

https://rails.lighthouseapp.com/projects/8994/tickets/6623-render_to_string-in-mailer-causes-subsequent-render-to-fail

これを機能させるには2つの方法があります。

1 つ目は、上記の最初の記事で説明したハックを使用することです。

def email_invoice(invoice)
  @invoice = invoice
  attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
  )
  self.instance_variable_set(:@lookup_context, nil)
  mail :subject => "Your Invoice", :to => invoice.customer.email
end

または、次のようにブロックに添付ファイルを設定できます。

def email_invoice(invoice)
  @invoice = invoice
  mail(:subject => 'Your Invoice', :to => invoice.customer.email) do |format|
    format.text
    format.pdf do
      attachments['invoice.pdf'] = WickedPdf.new.pdf_from_string(
        render_to_string(:pdf => "invoice",:template => 'documents/show.pdf.erb')
      )
    end
  end
end
于 2011-04-02T16:00:50.890 に答える
4

上記の Unixmonkey のソリューションを使用しましたが、Rails 3.1.rc4 にアップグレードすると、 @lookup_context インスタンス変数の設定が機能しなくなりました。検索コンテキストを同じようにクリアする別の方法があるかもしれませんが、今のところ、メール ブロックに添付ファイルを設定すると、次のように問題なく動作します。

  def results_email(participant, program)
    mail(:to => participant.email,
         :subject => "my subject") do |format|
      format.text
      format.html
      format.pdf do
        attachments['trust_quotient_results.pdf'] = WickedPdf.new.pdf_from_string(
          render_to_string :pdf => "results",
               :template => '/test_sessions/results.pdf.erb',
               :layout => 'pdf.html')
      end
   end
  end
于 2011-06-14T19:07:06.810 に答える
0

この問題を修正した方法は次のとおりです。

  1. wicked_pdf を削除
  2. インストールされたエビ ( https://github.com/sandal/prawn/wiki/Using-Prawn-in-Rails )

Prawn は、ドキュメントのレイアウトが少し面倒ですが、メールの添付ファイルを簡単に扱うことができます...

于 2011-03-30T04:52:00.820 に答える