问题
I have a Rails app where I use wicked_pdf to generate PDF's. This is all fine and working locally, but when pushed to heroku the PDF does render, but without applying the stylesheet.
Especially for PDF rendering I have a CSS file: app/assets/stylesheets/pdf.css.scss
. And in form.pdf.haml
I load the stylesheet like this:
!!!
%html{lang: "en", "xml:lang" => "en", xmlns: "http://www.w3.org/1999/xhtml"}
%head
%meta{content: "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
= wicked_pdf_stylesheet_link_tag "pdf"
As said, it works great locally, but when pushed to heroku, I get this error:
ActionView::Template::Error (No such file or directory - /app/public/pdf.css)
What do I have to do to make this work on heroku?
Edit: I found this Gihub repo https://github.com/jordan-brough/heroku-pdf
, which is an example app for using wicked_pdf on heroku. It really helped by adapting the environment.rb
to serve a css file from the public
folder when requesting a PDF.
回答1:
I had a similar problem and it took me a while to crack. I ended up using the wkhtmltopdf-heroku gem and the following setup which works both locally and on heroku in addition to proper handling of the debug option:
In controller:
respond_to do |format|
format.html
format.pdf do
render pdf: @profile.name + Date.today.to_s(:number),
background: true,
encoding: 'utf8',
:show_as_html => params[:debug].present?
end
end
In show.pdf.erb:
<% if params[:debug].present? %>
<%= stylesheet_link_tag 'documents' %>
<% else %>
<%= wicked_pdf_stylesheet_link_tag 'documents' %>
<% end %>
... rest of view ...
In config/environments/production.rb:
config.assets.precompile += ['documents.css.scss.erb']
In config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( documents.css )
Hope that this will be of help to someone.
回答2:
I tried this solution and wicked_pdf
works. But the comment:
"Activating runtime compilation is not the solution, because of the performance hit we take" @David Tuite
and found answer in this comment with non-stupid-digest-assets
gem, and works perfectly.
回答3:
There is a GEM for the wkhtmltopdf binaries to work on heroku without you having to install any binaries in your own repository. It also comes packed with the OSX ( darwin ) binary for development. It worked with PDFKit and probably should work with WickedPDF as well
https://github.com/bradphelan/wkhtmltopdf-heroku
or in your Gemfile as
gem "wkhtmltopdf-heroku"
来源:https://stackoverflow.com/questions/20525387/wicked-pdf-stylesheet-not-working-on-heroku