2

I'm using wicked_pdf pdf_from_string inside an action mailer rails 3 model. The pdf render perfectly doing this:

attachments["pdf.pdf"] = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "pdf.pdf",:template => 'documents/show.pdf.erb') )

When I try to pass the option :footer, it does not work with these options:

attachments["pdf.pdf"] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "pdf.pdf", :template => 'pdf/pdf.html.erb', :layout => 'pdfs/pdf', 
    :footer => {:html => {:template => 'pdf/pdf_footer.html.erb', :layout => 'pdfs/pdf'}, :spacing => -65})
  )

Note that :footer option works sweet inside a controller, coming from a controller default 'render' :pdf method.

I ended up doing something like this, but I'd prefer not using gotchas.

File.open("/tmp/wicked_pdf_#{@model.number}.html", 'w+b', 0644) { |f|
 f.write render_to_string({:template => 'pdf/pdf_footer.html.erb', :layout => 'pdfs/pdf'})
}
attachments["pdf.pdf"] = WickedPdf.new.pdf_from_string(
      render_to_string(:pdf => "pdf.pdf", :template => 'pdf/pdf.html.erb', :layout => 'pdfs/pdf'),
      :footer => {:html => {:url => "file:///tmp/wicked_pdf_#{@model.number}.html"}, :spacing => -65}                 
    )

Any clue to have this working properly?

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
CLod
  • 917
  • 2
  • 11
  • 28

1 Answers1

0

This looks to be related to the problem in this question:

Rails 3 ActionMailer and Wicked_PDF

Where the mailer doesn't like it when you call render.

Try wrapping your attachment setting in a respond_to block like so:

mail(:subject => 'Your pdf', :to => user.email) do |format|
  format.text
  format.pdf do
    attachments['pdf.pdf'] = WickedPdf.new.pdf_from_string(
      render_to_string(
        :pdf      => "pdf.pdf",
        :template => 'pdf/pdf.html.erb',
        :layout   => 'pdfs/pdf', 
        :footer   => {
          :html => {
            :template => 'pdf/pdf_footer.html.erb',
            :layout   => 'pdfs/pdf'
          },
          :spacing => -65
        }                 
      )
  end
end
Community
  • 1
  • 1
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • I need to render an html email with a pdf attached. I tried to do: format.html do attachments["pdf_#{@model.number}.pdf"] = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "pdf_#{@model.number}.pdf", :template => 'pdfs/pdf.html.erb', :layout => 'pdfs/pdf', :footer => {:html => {:template => 'pdf/pdf_footer.html.erb', :layout => 'pdfs/pdf'}, :spacing => -65}) ) render :the_html the pdf is attached, but still don't have footer – CLod Dec 21 '11 at 01:35
  • also if use the pdf format the pdf is rendered totally non-styled. footer still missing. is there another solution? – CLod Dec 21 '11 at 01:57
  • looking at the code of pdf_helper of the gem def make_pdf(options = {}) html_string = render_to_string(:template => options[:template], :layout => options[:layout]) options = prerender_header_and_footer(options) w = WickedPdf.new(options[:wkhtmltopdf]) w.pdf_from_string(html_string, options) end I open an issue – CLod Dec 21 '11 at 02:46
  • forked the gem with a fix for this; waiting for the pull request to be considered. in the meantime I'm using my fork available here: https://github.com/clod81/wicked_pdf – CLod Dec 21 '11 at 22:04
  • 1
    calling my fix like this: attachments["XXX.pdf"] = WickedPdf.new.pdf_from_string( render_to_string(:pdf => "XXX.pdf", :template => 'pdf.html.erb', :layout => 'pdfs/pdf'), :footer => {:content => render_to_string({:template => 'pdf_footer.html.erb', :layout => 'pdfs/pdf'}), :spacing => -70} ) – CLod Dec 21 '11 at 22:05