问题
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