Chi*_*rrl 12 ruby-on-rails pdfkit render-to-string
Ruby 1.8.7,Rails 3.0.4,PDFKit 0.5.0
我正在尝试使用PDFKit创建PDF而不使用中间件,因此我可以禁用javascript(那里有一个手风琴动作,隐藏了大量应该在PDF上的信息).但是,每当我尝试时,它都会失败,因为它表示我的视图(show.html.erb)中的部分缺失:
使用{:locale => [:en,:en],:formats => [:pdf],:handlers => [:erb,:rjs,:builder,:rhtml,:rxml]}缺少部分程序/详细信息
如果我删除对partials的引用,它工作正常.我也尝试将部分与show.html.erb放在同一目录中无济于事.这是我的控制器的show动作中的代码:
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
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,并保持部分?
编辑:现在我已经这样做了:
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
end
Run Code Online (Sandbox Code Playgroud)
这样做的缺点是为我生成的每个PDF都关闭了javascript,但它现在就可以了.关于使部分工作仍然使用render_to_string的原始问题的任何答案仍然受到赞赏.
ale*_*ler 20
今天早上我遇到了这个问题,并在寻找解决方案时遇到了你的问题.
控制器提取:
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
Run Code Online (Sandbox Code Playgroud)
constitution.pdf.haml
提取:
=render :partial => 'shared/constitution'
Run Code Online (Sandbox Code Playgroud)
错误:
Missing partial shared/constitution with {:locale=>[:en, :e ...
Run Code Online (Sandbox Code Playgroud)
过了一会儿,我的头靠在墙上,我猜了一下,改为constitution.pdf.haml
:
=render :partial => 'shared/constitution.html.haml'
Run Code Online (Sandbox Code Playgroud)
关于Rails,我只知道一小部分.难道真的那样(不像我正常的Haml视图),PDFKit需要文件扩展吗?这对我来说是固定的!