render_to_string does not find partials (PDFKit controller response)
Asked Answered
T

3

13

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.

Twitt answered 23/3, 2011 at 0:19 Comment(0)
D
20

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!

Dichogamy answered 23/4, 2011 at 12:58 Comment(0)
H
4

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.

Hellhound answered 28/5, 2012 at 13:11 Comment(0)
K
1

You should specify full path to your template I think:

html = render_to_string(:template => "my_view_folder_name/show.html.erb")
Kellner answered 23/4, 2011 at 13:2 Comment(1)
what about if you need to pass in locals and layout=false?Sprawl

© 2022 - 2024 — McMap. All rights reserved.