wicked_pdf没有在ActionMailer中加载页眉或页脚

我正在尝试使用以下代码生成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 

如果我在控制器的动作中运行此代码,效果很好,但我的邮件程序类中的相同代码只是呈现HTML而不是PDF。 然后,如果我调用WickedPdf.new.pdf_from_string(archivo)生成一个PDF,但没有页眉或页脚,这是正确的,因为在生成的HTML中不包括页眉或页脚。 有什么东西我不见了吗? 请帮忙。 无论如何,我正在使用:

  • wkhtmltopdf 0.11.0 rc1
  • 铁轨(3.2.3)
  • wicked_pdf(0.7.9)

谢谢!

WickedPdf没有alias_method_chain:ActionMailer中的render_to_string就像它对ActionController一样。

首先,将wicked_pdf更新为0.8.0版或git master。 这将允许您将其包含在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