0

I know that there have been other questions about this, but I've read through them all and haven't figured this out yet. I'm using the PDFKit gem to allow users to generate .pdfs of content on my site. I can render a pdf by adding .pdf to the url, but CSS doesn't transfer along with it. I understand from Googling that it's because of the asset pipeline, but I haven't figured out how to make it work.

I've installed both of teh necessary gems:

gem 'pdfkit', '~> 0.6.2'
gem 'wkhtmltopdf-binary' 

In my application.rb file I added 'require pdfkit' at the top and the following to the application class:

    config.middleware.use PDFKit::Middleware

    config.cache_classes = true

    config.eager_load = true

I then ran rake middleware

I also added an initializer called pdfkit.rb with teh following code that I found, in order to fix the .pdf from endlessly loading:

ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
 "file://#{Rails.root.join('public')}"
else
 "#{request.protocol}#{request.host_with_port}"
end
}

At this point adding .pdf to a url creates the pdf, but doesn't render the CSS at all. The README on pdfkit's github says:

"Resources aren't included in the PDF: Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources."

My CSS is called in the head as usual:

<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>

How do I make an absolute path? I don't think I just replace the stylesheet_tag with an ordinary

 <link rel='stylesheet' href='bootstrap_and_customization.css.scss' type="text/css">
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
Tony Tambe
  • 573
  • 1
  • 10
  • 30
  • were you able to fix this issue? i am facing the same issue. pls checkout http://stackoverflow.com/questions/29436295/pdfkit-css-stylesheets-not-being-applied – Prasad Surase Apr 09 '15 at 03:39
  • No, unfortunately. I have that project on hold for now, but I will have to finish it eventually, so I'd love to find a fix. – Tony Tambe Apr 09 '15 at 17:27

0 Answers0