map7
map7

Reputation: 5126

wicked_pdf using image tag helper in css file

I'm using the wicked_pdf plugin with a rails 3.2 project and the asset pipeline. I want to define my picture inside pdf.css.erb

Here is my controller code to make use of a layout file:

    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "Cars.pdf",
        :template => "reports/cars.html.haml",
        :page_size => "A4",
        :layout => "pdf.html",
        :handlers => [:haml]
      end
    end

Here is my layout file where I include my pdf.css.erb file.

!!!
%html
    %head
        = wicked_pdf_stylesheet_link_tag "pdf"
        = csrf_meta_tag
    %body
        .contentbg
            .logo
            .content= yield

Lastly here is my CSS

.logo {
    background: wicked_pdf_image_tag "logo.jpg";
    width: 100px;
    height: 80px;
}

This does not find the logo.jpg. If I put the wicked_pdf_image_tag "logo.jpg" line directly in my pdf.html.haml template layout file then it does render my logo in the pdf generated.

Is it possible to use the wicked_pdf_image_tag helper inside a css.erb file?

Upvotes: 2

Views: 3913

Answers (1)

Unixmonkey
Unixmonkey

Reputation: 18784

That's not going to work inside a css.erb file, firstly because it is missing erb tags, but mostly because wicked_pdf_image_tag will expand that out to this:

.logo {
  background: <img src="file://foo/bar/public/images/logo.jpg">;
}

Which is not valid css. Best bet would be to construct it by hand and something like this:

.logo {
  background: url(<%= Rails.root.join('assets','images','logo.jpg').to_s %>);
}

The reason to have the direct file access path is so wkhtmltopdf doesn't have to make a web request to get the assets, hence load more quickly.

Upvotes: 1

Related Questions