ruby-on-rails-3.2wkhtmltopdfwkhtmltoimageimgkit

Using typekit in wkhtmltoimage / imgkit


I am on Rails 3.2.9. Is it possible for me to use typekit (or goggle fonts) for rendering the images? I used the following code, but the images are not rendering the right fonts. I suspected the pages were not loading.

html = "<head>"
html << '<script type="text/javascript" src="//use.typekit.net/dtf1xzc.js"></script>'
html << '<script type="text/javascript">try{Typekit.load();}catch(e){}</script>'
html << "</head>"
html << html2berendered

kit = IMGKit.new(html)
kit.stylesheets << Rails.root.join('app', 'assets','stylesheets' ,'cards.css').to_s

kit.to_png

Alternatively, I could try using custom fonts by installing them, but they do not load for some weird reason.


Solution

  • In any case, i realized it could not be done. So I installed custom fonts in my heroku machine using an extremely smart solution I found here: www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku

    Code as follows. Hope this would be useful for anyone who needs custom fonts in heroku/wkthtmltoimage

    if Rails.env.production? || Rails.env.staging?
      font_dir = File.join(Dir.home, ".fonts")
      Dir.mkdir(font_dir) unless Dir.exists?(font_dir)
    
      Dir.glob(Rails.root.join("vendor", "fonts", "*")).each do |font|
        target = File.join(font_dir, File.basename(font))
        File.symlink(font, target) unless File.exists?(target)
      end
    end