Question

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!

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top