Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wicked_pdf + rails' asset pipeline + sass import in production

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.

like image 778
polarblau Avatar asked Oct 06 '11 21:10

polarblau


2 Answers

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)

like image 192
philip_kobernik Avatar answered Sep 29 '22 21:09

philip_kobernik


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.

like image 33
Andrew K Avatar answered Sep 29 '22 21:09

Andrew K