1

次のコードでPDFを生成しようとしています。

    archivo = render_to_string(
      :pdf => "formulario_de_registro.pdf",
      :template => 'pdf/profile_download.pdf.haml',
      :layout   => 'pdf/body.pdf.haml',
      :margin => { :bottom => 40, :top => 30 },
      :header => {
        :html => { :template => 'layouts/pdf/header.pdf.haml'},
        :font_size => 13},
      :footer => {
        :html => { :template => 'layouts/pdf/footer.pdf.haml'},
        :line => true}
    )
    # from here is just for debugging purposes
    save_path = Rails.root.join('tmp','prueba.pdf')
    File.open(save_path, 'wb') do |file|
      file << archivo
    end

このコードをコントローラーのアクション内で実行すると、うまく機能しますが、メーラークラスの同じコードは、PDFではなくHTMLをレンダリングするだけです。次に、呼び出すとWickedPdf.new.pdf_from_string(archivo)PDFが生成されますが、ヘッダーまたはフッターはありません。生成されたHTMLにはヘッダーとフッターの両方が含まれていないためです。足りないものはありますか?助けてください。いずれにせよ、私は以下を使用しています:

  • wkhtmltopdf 0.11.0 rc1
  • レール(3.2.3)
  • wicked_pdf(0.7.9)

ありがとう!

4

1 に答える 1

3

WickedPdfは、ActionControllerの場合のように、ActionMailerでalias_method_chain:render_to_stringを使用しません。

まず、wicked_pdfをバージョン0.8.0またはgitmasterに更新します。これにより、actionmailerクラスに含めることができます。

次に、PdfHelperモジュールを手動で含め、次のようにメソッドを直接呼び出すことで、これを回避します。

# Mailer
class Notifications < ActionMailer::Base
  include PdfHelper

  def send_email
    archivo = render_to_string_with_wicked_pdf(
      :pdf => "formulario_de_registro.pdf",
      :footer => { :html => { :template => 'layouts/pdf/footer.pdf.haml' } }
      # etc...
    )
    attachments['formulario_de_registro.pdf'] = archivo
    mail :to => 'person@example.com'
  end
end
于 2012-11-13T23:13:39.513 に答える