wicked_pdf not loading header or footer in ActionMailer

假装没事ソ 提交于 2019-12-24 07:38:31

问题


I'm trying to generate a pdf with the following code:

    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

If I run this code inside of a controller's action, works great, but the same code in my mailer class just render a HTML, not a PDF. Then if I call WickedPdf.new.pdf_from_string(archivo) generates a PDF, but without the header or footer, which is right, because in the generated HTML doesn't include both header or footer. Is there something that I'm missing? Please help. In any case, I'm using:

  • wkhtmltopdf 0.11.0 rc1
  • rails (3.2.3)
  • wicked_pdf (0.7.9)

Thanks!


回答1:


WickedPdf doesn't alias_method_chain :render_to_string in ActionMailer like it does for ActionController.

First, update wicked_pdf to version 0.8.0 or git master. This will allow you to include it on an actionmailer class:

Then you get around this by manually including the PdfHelper module and just calling the method directly like so:

# 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


来源:https://stackoverflow.com/questions/13368888/wicked-pdf-not-loading-header-or-footer-in-actionmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!