我很难让PDFKit在Heroku上工作。我已经阅读了here,here和here的教程,但仍然无法弄清楚如何正确显示pdf。我遇到了上一个链接中描述的相同问题,其中应用程序在开发中正常工作,并在Heroku上生产。我尝试添加以下内容(从教程中获得)。
application_helper.rb
def request_from_pdfkit?
# when generating a PDF, PDFKit::Middleware will set this flag
request.env["Rack-Middleware-PDFKit"] == "true"
end
# We have to reference the actual output of the assets pipeline, which according
# to http://guides.rubyonrails.org/asset_pipeline.html is application-<md5>.css and
# application-<md5>.js. We have to dynamically find these file names and correctly
# reference them.
def get_mdf5_file(base_file)
stripped_name = base_file.basename.to_s.gsub(base_file.extname, "")
Dir.glob("#{base_file.parent}/*#{base_file.extname}").each do |file|
return file if file.match(/#{stripped_name}-[\d\w]+#{base_file.extname}/)
end
return base_file
end
application.html.erb
<% if request_from_pdfkit? %>
<style type="text/css" media="all">
<%= File.read(get_mdf5_file(Rails.root.join("public","assets","application.css")) %>
</style>
<script type="text/javascript">
<%= File.read(get_mdf5_file(Rails.root.join("public","assets","application.js"))) %>
</style>
<% else %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<% end %>
当我这样做时,生成的PDF具有最小的样式,并且没有从javascript生成的内容。它(基于我的预感)似乎没有任何Bootstrap scss被预编译到application.css中。现在,这是
的内容application.css
/*
*= require_self
*= require dataTables/jquery.dataTables
*= require dataTables/jquery.dataTables.bootstrap
*= require dataTables/src/demo_table_jui
*= require jquery.ui.all
*= require jquery.jqplot
*= require_tree .
*/
的application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap
//= require bootstrap-multiselect
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap
//= require jquery.jqplot
//= require plugins/jqplot.cursor
//= require plugins/jqplot.BezierCurveRenderer
//= require plugins/jqplot.bubbleRenderer
//= require plugins/jqplot.barRenderer
//= require plugins/jqplot.categoryAxisRenderer
//= require plugins/jqplot.canvasTextRenderer
//= require plugins/jqplot.canvasAxisLabelRenderer
//= require plugins/jqplot.highlighter
//= require plugins/jqplot.canvasOverlay
//= require plugins/jqplot.cursor
//= require plugins/jqplot.enhancedLegendRenderer
//= require plugins/jqplot.dateAxisRenderer
//= require plugins/jqplot.canvasAxisTickRenderer
//= require_tree .
我尝试将一些Bootstrap scss组件手动添加到application.css资产管道,但是引用了错误here。
我不知道如何让它发挥作用,所以任何帮助都会受到赞赏。
修改
我一直在玩这个很多,并注意到当Javascript和CSS内联插入时,根据Chrome上显示的来源,所有引号“都被替换为”。我尝试添加.gsub(/“/,/”/),但这不起作用。有谁知道我为什么看到“而不是”,或者这甚至是一个问题。
编辑2
通过实施以下更改,我能够让显示效果更好
<script type="text/javascript">
<%= raw(File.read(get_mdf5_file(Rails.root.join("public", "assets", "application.js"))).gsub("</script>", "")) %>
</script>
<style type="text/css" media="all">
<%= raw File.read(get_mdf5_file(Rails.root.join("public", "assets", "application.css"))) %>
</style>
然而造型仍然混乱,所以我仍在寻找解决方案。
答案 0 :(得分:2)
我想出来了。我不得不降级到wkhtmltopdf 0.10 rc2。在我这样做之后,我能够修改样式以使PDF看起来不错。
答案 1 :(得分:1)
我认为你可以通过做(Rails4,YMMV)来简化:
<%= Rails.application.assets.find_asset('pdfs.css').to_s.html_safe %>
使用wkhtmltox-0.12.0。
一些细节: https://github.com/sstephenson/sprockets#accessing-assets-programmatically
感谢:https://stackoverflow.com/a/11767185/1869825
还有:Rails 3 command to embed text of my CSS stylesheets in head of my HTML