Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use google web fonts with phantomjs

I'm using phantomjs version 1.9.7 which I believe is supposed to support web fonts. I have inserted the font with google web fonts however it is just displaying my fallback font when I automate a screengrab. The webfont is displaying fine in all my browsers. Are there any workarounds for this?

like image 542
alexrogers Avatar asked May 01 '14 08:05

alexrogers


Video Answer


1 Answers

I have struggled with this issue for several hours. Well, there is a simple reason for this controversy: user-agent!

Some services such as Google Fonts are returning different CSS content based on user-agent. When you call a webpage which includes Google Fonts with default PhantomJs user-agent, Google would return TTF version which is supported in PhantomJs.

However, if you set a custom user agent (Chrome, FF, etc.), Google Fonts would return .woff2 version. .woff2 is not supported in PhantomJs 2.x. Obviously, fonts are not going to be loaded.

So, for users who are testing PhantomJs without setting a generic user-agent, Google Fonts is working. If they set for example Google Chrome user-agent, it does not work.

So, you have two options:

  1. Avoid setting a custom user-agent if it is possible.
  2. Avoid using dumb-smart font providers like Google Fonts which does not outputting all font versions in CSS and let the browser decide what it needs.
like image 147
Kousha Avatar answered Oct 23 '22 07:10

Kousha