Ruby 1.8.7, Rails 3.0.4, PDFKit 0.5.0
I'm trying to create a PDF with PDFKit without using the middleware so I can disable javascript (there's an accordion action in there that hides a lot of info that should be on the PDF). However, whenever I try, it fails because it says the partials in my view (show.html.erb) are missing:
Missing partial programs/details with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml]}
If I remove the references to the partials, it works fine. I've also tried putting the partials in the same directory with show.html.erb to no avail. Here is the code in my controller's show action:
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:template => "show.html.erb")
kit = PDFKit.new(html, :disable_javascript => true )
send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
}
end
Is there any way to do this and keep the partials?
EDIT: for now I've done this:
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
end
This has the disadvantage of turning off javascript for every PDF I generate, but it'll do for now. Any answers on the original question of getting the partials to work still with render_to_string still appreciated.
I was running into this issue this morning and came across your question while looking for a solution.
Controller extract:
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:layout => false , :action => "constitution.pdf.haml")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
send_data(kit.to_pdf, :filename => "#{@organisation_name} Constitution.pdf",
:type => 'application/pdf', :disposition => 'inline')
return
}
end
constitution.pdf.haml
extract:
=render :partial => 'shared/constitution'
Error:
Missing partial shared/constitution with {:locale=>[:en, :e ...
After a while banging my head against a wall, I had a guess and changed constitution.pdf.haml
to:
=render :partial => 'shared/constitution.html.haml'
I only know a tiny amount about Rails. Could it really be that (unlike my normal Haml views), PDFKit requires the file extension? That's fixed it for me!