Rails3 - wicked_pdf gem, footer issue when calling from action mailer model

喜你入骨 提交于 2019-12-12 08:16:27

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/8583573/rails3-wicked-pdf-gem-footer-issue-when-calling-from-action-mailer-model

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