简体   繁体   中英

Rails4 + PDFKit + Fonts + Heroku not working

I'am trying to solve a font issue on heroku using PDFkit since few days and nothing seem to work for me....

I've already did few solutions such as : Convert LucidaHW from Squirrel and do as follow ...

@font-face {
    font-family: 'lucida_handwritingitalic';
    src: url(data:application/x....

or this solution with .font on that website: http://www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku

or using :

kit.stylesheets << "#{Rails.root.join("public","stylesheets", "pdf", "pdf.css.scss")}"

I also moved my pdf.css.scss into vendor folder, into public folder and nothing ...

Right now, i am a bit confused because almost all of it work in dev mode (on my localhost) but nothing on heroku. I'am aware this is not the first ticket about that issue but nobody found a solution that worked for me.

I ran into a similar issue a few months back. This is what I'm doing right now and it has been working great for me:

1.Serve assets in base64:

add this helper method:

def asset_data_base64(path)
  asset = Rails.application.assets.find_asset(path)
  throw "Could not find asset '#{path}'" if asset.nil?

  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end

and in your template, include assets like this: (the example uses haml)

  = stylesheet_link_tag(asset_data_base64('pdf/pdf.css'))
  = javascript_include_tag(asset_data_base64('pdf/pdf.js'))

2.custom fonts

move all your fonts to a folder in your project. (I put mine under vendor/assets/fonts ) then add a new initializer file in your config/initializers

if Rails.env.production?
  font_dir = File.join(Dir.home, ".fonts")
  Dir.mkdir(font_dir) unless Dir.exists?(font_dir)

  Dir.glob(Rails.root.join("vendor","assets","fonts", "*")).each do |font|
    target = File.join(font_dir, File.basename(font))
    File.symlink(font, target) unless File.exists?(target)
  end
end

replace Rails.root.join("vendor","assets","fonts", "*") withe the path to the folder where you put all your font files.

then in your css, do not use font-face, use the font name directly, eg font-family: Gotham;

Also, do not user fallback fonts! In some versions of wkhtmltopdf, it always uses the fallback font if one is provided.

=============================================

  1. I learned the base64 trick from here: https://github.com/mileszs/wicked_pdf/issues/257
  2. Custom fonts solution was found here: http://www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku

Try putting the font in a .fonts folder in you app root and push it to Heroku - just refer to the font via the font family and not with a src. That's how I use custom fonts on Heroku with wkhtmltopdf and it works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM