I find it very difficult to get PDFKit to work on Heroku. I read the tutorials found here , here and here , but still cannot figure out how to display pdf correctly. I have the same problem described in previous links, where the application works fine in development and freezes in production on Heroku. I tried to add the following (obtained from textbooks).
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 %>
When I do this, the created PDF files have a minimal style and do not contain content created from javascript. It would be (in my opinion) that none of the Bootstrap scripts would be precompiled in application.css. Right now, here is the content
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 .
I tried to manually add some Scash Bootstrap components to the application.css application pipeline, but got the link here .
I don't understand how to make this work, so any help would be appreciated.
EDIT
I played a lot with this and noticed that when Javascript and CSS are inserted into the string, all quotes are โreplacedโ according to the source displayed in Chrome. I tried adding .gsub (/ "/, /" /), but that did not work. Does anyone know why I see โinstead,โ or if itโs even a problem.
EDIT 2
I was able to display the screen a little better by making the following changes.
<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>
however, the style is still confusing, so I'm still looking for a solution.