josal
josal

Reputation: 452

Including custom fonts in .fonts folder in heroku, seems not recognizing them

Heroku support told me that in order to use custom fonts in my web app (not installed in the system, you can see the installed ones with fc-list in a bash console) I had to deploy a .fonts folder with all the fonts inside.

The problem is that I don't know how to do it. I mean, I don't know if the name of the file has to follow any special pattern for heroku or I must do something in my code to consider this fonts or it's something automatic if I include it in the folder...

The fact is that I tried changing the filename of the font in different ways and the font is not used at all.

To give you more details, the process were we're using the font is to convert a PDF to an image, more concretely, with the rghost gem. And the final image doesn't use the custom font at all.

Thanks in advance, any idea is welcome.

Upvotes: 5

Views: 3910

Answers (3)

Steven de Salas
Steven de Salas

Reputation: 21447

This article seems to point out a way of getting custom fonts working in Heroku

http://www.mobalean.com/blog/2011/08/02/pdf-generation-and-heroku

The short answer is that you can:

  1. Add them to the remote file system via GIT checkin
  2. Use Ruby to create the $HOME/.font directory (if it does not already exist)
  3. Use Ruby to create a symlink for each of the font files from the $HOME/.font folder pointing to where they are hosted in the file system (again, if these do not already exist)
  4. Voila!

Upvotes: 0

Patrick Oscity
Patrick Oscity

Reputation: 54674

I would just create a fonts/ directory in the root folder of the project and then load the fonts like this, for example in config/initializers/rghost.rb:

RGhost::Config::GS[:extensions] << "#{Rails.root}/fonts/LTSyntax.ttf"

The documentation states that you will also need a Fontmap file in the fonts directory, for example:

/Syntax (LTSyntax.ttf); 

Your problem is that the PDF references fonts that are not embedded into the PDF. You can obtain a list of fonts that are used in the PDF with xpdf, using the pdffonts command:

> pdffonts example.pdf
name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
Syntax                               Type 1            no  no  no       8  0

Alternatively, you can also use this script from pdf-reader.

Upvotes: 6

AJcodez
AJcodez

Reputation: 34156

The way I did it was to put all of the fonts in lib/assets/fonts.

Add these lines to config/application.rb

config.assets.paths << Rails.root.join('lib', 'assets', 'fonts')
config.assets.precompile += %w(.svg .eot .woff .ttf)

I then include the fonts in the stylesheets in lib/assets/stylehseets/fonts.css, for each font do something like:

@font-face {
  font-family: 'AvenirLTStd-Black';
  src: font-url('avenirltstdblack.eot');
  src: font-url('avenirltstdblack.eot?#iefix') format('embedded-opentype'),
  font-url('avenirltstdblack.woff') format('woff'),
  font-url('avenirltstdblack.ttf') format('truetype'),
  font-url('avenirltstdblack.svg#avenirltstdblack') format('svg');
  font-weight: normal;
  font-style: normal;
}

And include that file in the app/assets/stylesheets/application.css

*= require fonts

In your app css (scss) you can use:

font-family: 'AvenirLTStd-Black';

Then just recompile assets $ rake assets:precompile and redeploy. Let me know if that works for you, may have missed a step as I did this months ago!

Upvotes: 3

Related Questions