Take the 2-minute tour ×

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

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)


share|improve this question

1 Answer 1

up vote 2 down vote accepted

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'
share|improve this answer
It appears undefined method 'render_to_string_with_wicked_pdf' for #<NotificationsMailer:0xb579ad0> –  Alter Lagos Nov 14 '12 at 2:36
@AlterLagos I forgot to mention you'll have to include PdfHelper, and you can't even do that on 0.7.9 proper, and will have to use git master until a new gem version is published. I have verified that this works. –  Unixmonkey Nov 14 '12 at 15:39
Now is working with revision: 488e473b5c6c9af7870d793da5769af49ef1b500. Thanks a lot :) –  Alter Lagos Nov 14 '12 at 16:06
@AlterLagos I just published a new version of the gem (0.8.0), that you can pin to, instead of git master. –  Unixmonkey Nov 14 '12 at 16:07

Your Answer


