I recently updated one of my apps to Rails 3.1 and found that my PDF generating tests were failing because they were unable to find the CSS or images to use in the PDF.
So I started playing around, and here is what I got:
In your app, add a new initializer (or edit your existing wicked_pdf.rb initializer) and put the following:
As you can see in the code, I also replaced the background urls with correct references, this is assuming that you’re keeping your CSS images in the same location as your normal images (which I don’t see a reason why not but of course you can always tweak the script)
I had one issue with my generated PDF using pow.cx, my CPU would jump to 100% (on the pow process) and the page would timeout. After upgrading to the latest version of pow (0.3.2) using this command:
|
1 |
curl get.pow.cx | sh |
I was able to see my PDFs without an issue.
http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/
https://github.com/mileszs/wicked_pdf/issues/48
Small change to the above code to fix issue that prevented CSS from being rendered correctly.
Thank you, saved me a lot of time.
Glad I could help!
When I ran into “No such file or directory – …/public/stylesheets/application.css” I hoped someone else had been there first. Thanks for helping reduce my Rails 3.1 pains.
Hey! Same for me!
Have you got any solution for that?
I see that Wicked-pdf helpers have the module Asset that has the correct implementation for rails 3.1!
I tried working with the latest version of wicked_pdf and it still didn’t work without the code snipped above. I did make a small change to make it work though.
in my layout, should I use the specific wicked_pdf_stylesheet_link_tag
or the standard one:
!!! 5
%html{:dir => "ltr", :lang => "fr-FR", :class => "debugs"}
%head
%meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
= stylesheet_link_tag 'print.css', :media => :all
%body
#main-container
= yield
You do have to specify the
wicked_pdf_stylesheet_link_taginstead ofstylesheet_link_tag. That’s why you should always create a pdf specific layout file.[SOLVED] answer was in my question…. obviously I need to have a specific layout for the pdf, where I should use : = wicked_pdf_stylesheet_link_tag ‘templates’ ( :media => :all is of no use ..)
thanks a lot for your script … it works… just some ‘tricky lines’ to be modified in my view :
%table#header.t1
%tr
%td.thd10
%h6
SET ON:
%span.unbold 02/03/2012
is fine w html , not with pdf.. will modify it .. thanks again !!!!
Glad to hear it works for you and good luck with the trickiness!
Hi,
I implemented this but I’m getting this error on Heroku. Is this where assets are stored by default on Heroku?
ActionView::Template::Error (No such file or directory – file:///app/public/assets/application-e75b6a205fc26dca3eca97a537d84f09.css):
Sorry but this could is probably outdated due to rails 3.2 and asset pipeline, but if you read up in the wicked_PDF docs, you can find how to make it work with the asset pipeline.