Wicked_PDF working in Rails 3.1

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)

Possible Issues

Pow.cx

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:

I was able to see my PDFs without an issue.

Additional Sources:

http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/
https://github.com/mileszs/wicked_pdf/issues/48

Small update: (14/02/2012)

Small change to the above code to fix issue that prevented CSS from being rendered correctly.

11 Comments

  1. Thank you, saved me a lot of time.

  2. 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.

  3. 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_tag instead of stylesheet_link_tag. That’s why you should always create a pdf specific layout file.

  4. [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 !!!!

  5. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">