Rails3 - wicked_pdf gem,从动作邮件模型调用时的页脚问题

时间:2011-12-21 00:03:56

标签: ruby-on-rails ruby-on-rails-3 pdf-generation wicked-pdf

我在动作邮件程序rails 3模型中使用了wicked_pdf pdf_from_string。 pdf渲染完美地做到了这一点:

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

当我尝试传递选项:footer时,它不适用于这些选项:

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})
  )

请注意:页脚选项在控制器内部运行良好,来自控制器默认'渲染' :pdf方法。

我最终做了这样的事情,但我不想使用陷阱。

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}                 
    )

有任何线索可以正常工作吗?

1 个答案:

答案 0 :(得分:0)

这看起来与此问题中的问题有关:

Rails 3 ActionMailer and Wicked_PDF

当您调用渲染时邮件程序不喜欢它。

尝试将附件设置包装在respond_to块中,如下所示:

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