Download web fonts from Google with webfont-dl

If you’re using Google Fonts to improve your website appearance, your visitors are always forced to download the initial CSS file describing fonts from Google’s servers every time they load a page, even though requests to the actual fonts may be cached. This can make your site slower. There are benefits to serving fonts from Google’s servers (for example, Google sends differently hinted fonts for Windows and OS X to improve their readability), however if you want to avoid depending on Google, or to improve load times, you may want to host the fonts yourself.

Network request to Google Fonts

webfont-dl is a nice little utility that downloads the specified CSS file and fonts referenced by it, and then generates a new CSS file which points to those downloaded fonts. But that’s not all: it also inlines WOFF (the most common and widely supported — in modern browsers — webfont format) directly into the CSS file to save requests to server, making websites even faster. The stylesheet is the only file served if the target browser supports it.

Installing webfont-dl

To install webfont-dl, type:

npm install -g webfont-dl

(Webfont-dl is distributed via NPM, so you’ll need Node.js and NPM installed.)

How to download fonts

Let’s download Roboto font with it:

webfont-dl '' -o roboto.css

After running this command, you’ll end up with roboto.css in the current directory along with a few font files: .eot, .ttf, and .svg:


If you look into the .css file, you’ll see that .woff is embedded as data URI:

webfont-dl embedded WOFF file

Downloading partial fonts with specified charset

This works great even when you need only some letters from the font, for example, if you use them for a logo. Specify the text you need by adding &text=... to your Google Fonts request to get only those characters:

webfont-dl '' -o roboto-progville.css

and you’ll receive a tiny 2K CSS file with embedded font. If you don’t support old browsers that can’t render WOFF fonts — according to this table, IE 8 and earlier:

WOFF Font Format support in browsers

then you can delete all other referenced fonts from this CSS and leave just the embedded WOFF.

Source code and license

Webfont-dl on GitHub:
Author: Matt Mastracci.
License: MIT.


Comments are closed.