Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render_to_string does not find partials (PDFKit controller response)

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.

like image 492
Chicagogrrl Avatar asked Mar 23 '11 00:03

Chicagogrrl


3 Answers

You can also set :formats for render_to_string to prevent having to change your partial names.

html = render_to_string(:layout => false , :action => "show", :formats => :html)

This forces html, instead of pdf, format for the remained of the view rendering. Allowing you to use the same views/partials without change for HTML and PDF responses.

like image 107
Kris Avatar answered Nov 15 '22 20:11

Kris


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!

like image 20
alexmuller Avatar answered Nov 15 '22 21:11

alexmuller


You should specify full path to your template I think:

html = render_to_string(:template => "my_view_folder_name/show.html.erb")
like image 1
fl00r Avatar answered Nov 15 '22 20:11

fl00r