render_to_string does not find partials (PDFKit controller response)

不问归期 提交于 2019-12-06 19:05:10

问题


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.


回答1:


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!




回答2:


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.




回答3:


You should specify full path to your template I think:

html = render_to_string(:template => "my_view_folder_name/show.html.erb")


来源:https://stackoverflow.com/questions/5399458/render-to-string-does-not-find-partials-pdfkit-controller-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!