wicked_pdf + rails' asset pipeline + sass import in production
Asked Answered
J

2

6

I'm successfully using wicked_pdf with SASS in development. I'm including a single .scss file, which contains several import rules for other .sass and .scss files, via this helper:

def wicked_pdf_stylesheet_link_tag(*sources)
  sources.collect { |source|
    "<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css").body}</style>"
  }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
end

But switching to production the app still looks for the imported files which aren’t found.

I've added then a second manifest file to be pre–compiled in production.rb (config.assets.precompile += %w(pdf.css)) which contains a single require rule to pick up the mentioned .scss file. This file is compiled just fine but it seems that the helper doesn't pick up the right file in production and still looks to load the imported .sass files.

Has anyone experience how to solve this? The PDF creation requires absolute paths, which makes this task a bit more difficult.

Johansen answered 6/10, 2011 at 21:42 Comment(0)
H
8

I have wicked pdf working in development and production. This is the core of my wicked_pdf config:

I've updated WickedPdfHelper (loaded from initializers/wicked_pdf.rb) based on a wicked_pdf pull request from github user antti

module WickedPdfHelper
  def wicked_pdf_stylesheet_link_tag(*sources)
    sources.collect { |source|
      "<style type='text/css'>#{Rails.application.assets.find_asset(source+".css")}</style>"
    }.join("\n").html_safe
  end

  def wicked_pdf_image_tag(img, options={})
    asset = Rails.application.assets.find_asset(img)
    image_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_src_tag(jsfile, options={})
    asset = Rails.application.assets.find_asset(jsfile)
    javascript_include_tag "file://#{asset.pathname.to_s}", options
  end

  def wicked_pdf_javascript_include_tag(*sources)
    sources.collect{ |source| "<script type='text/javascript'>#{Rails.application.assets.find_asset(source+".js")}</script>" }.join("\n").html_safe
  end
end

then in app/assets/stylesheets/pdf.css I require a few sass stylesheets:

/* ...
*= require ./_colors
*= require_directory ./pdf
*= require_self
*/

(remember that if you're modifying initializers or anything in config/, you'll need to re-start your rails app to pull in the changes)

Halfprice answered 3/1, 2012 at 23:22 Comment(0)
G
2

I wrote a article on this at: http://anlek.com/2011/09/wicked_pdf-working-in-rails-3-1/

It's very similar to Philip's solution with a few modifications.

Gribble answered 14/2, 2012 at 18:25 Comment(1)
Cached version of broken link: web.archive.org/web/20160728085119/http://anlek.com/2011/09/…Derry

© 2022 - 2024 — McMap. All rights reserved.