182

I have a template and it has a reference to a Google font like this:

<link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300' rel='stylesheet' type='text/css'>

How can I download it and set it up to use in my pages which are running offline all the time?

unor
  • 92,415
  • 26
  • 211
  • 360
user2147954
  • 2,045
  • 3
  • 15
  • 10

12 Answers12

411

Check out google webfonts helper

It lets you download every web font of Google and suggests css code for the implementation. This tool also allows you to simply download all formats at once without the hassle.

Ever wanted to know where Google hosts their webfonts? This service might be handy if you want to download all .eot, .woff, .woff2, .svg, .ttf files of a font variant directly from google (normally your User-Agent would determine the best format).

Also take a look at their Github page.

dovid
  • 6,354
  • 3
  • 33
  • 73
Marco Kerwitz
  • 5,294
  • 2
  • 18
  • 17
  • 31
    I just wonder why google haven't done this in the first place? +1 – tftd May 14 '15 at 10:45
  • 4
    This works great, Make sure that you declare the respective content type while linking from a local file `Content-type: text/css; charset: UTF-8` – Clain Dsilva Sep 12 '17 at 03:32
  • If you are using webpack or some other tool that packs CSS/SASS imports then this approach is the best. [This question](https://stackoverflow.com/questions/42708257/google-fonts-webpack/48310258#48310258) describes the process in a bit more detail. – Pace Jan 17 '18 at 21:33
  • 4
    [one-liner](https://gist.github.com/smnbbrv/a9f0a87b430be2e7acb48a836d99638b), run in the target directory: `curl -o f.zip "https://google-webfonts-helper.herokuapp.com/api/fonts/roboto?download=zip&subsets=latin,latin-ext&variants=regular,700" && unzip f.zip && rm f.zip` – smnbbrv Nov 23 '18 at 10:21
  • 2
    Url is now changed to https://gwfh.mranftl.com – Giorgos Betsos Nov 30 '22 at 18:31
107

Just go to Google Fonts - http://www.google.com/fonts/ , add the font you like to your collection, and press the download button. And then just use the @fontface to connect this font to your web page. Btw, if you open the link you are using, you'll see an example of using @fontface

http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300

For an example

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 300;
  src: local('Open Sans Light'), local('OpenSans-Light'), url(http://themes.googleusercontent.com/static/fonts/opensans/v6/DXI1ORHCpsQm3Vp6mXoaTaRDOzjiPcYnFooOUGCOsRk.woff) format('woff');
}

Just change the url address to the local link on the font file, you've downloaded.

You can do it even easier.

Just download the file, you've linked:

http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300

Name it opensans.css or so.

Then just change the links in url() to your path to font files.

And then replace your example string with:

<link href='opensans.css' rel='stylesheet' type='text/css'>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Dmitriy Butenko
  • 1,374
  • 1
  • 8
  • 7
  • 25
    Unlike Google fonts when server by Google, this approach relies on WOFF format alone and is thus essentially more limited. Besides, Google distributes their fonts in TTF format, not WOFF. – Jukka K. Korpela Apr 10 '13 at 17:32
  • 29
    Be careful, fonts.googleapis generate different @font-face declarations for different browsers. – Huseyin Yagli Jun 09 '14 at 14:34
  • 1
    Can you link to the TTF format instead of the WOFF in the fontface? – WJA May 15 '15 at 12:10
  • 10
    This is NOT correct because Google sends a browser-specific CSS file from http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300 So if you open this with Chrome you would get a different @font-face from opening it with IE or any other browser. WOFF2 fonts for example will only be sent to Firefox and Chrome as other browsers do not support them (yet). http://caniuse.com/#feat=woff2 – Joost van der Laan Apr 30 '16 at 13:14
  • 7
    Ignore this answer regardless of the accepted status. It is incorrect as Joost van der Laan has pointed out here. See the high-score answer by @marco-kerwitz below which should be marked as the correct answer. – Appurist - Paul W Jan 20 '18 at 21:18
  • Thanks for the comments, I confirm that the results on the visited page are browser-specific. If you want to work around that, you can run `curl "https://YOUR GOOGLEAPIS URL HERE" >> fonts.css` in your terminal and get your results in the `fonts.css` file. Not sure if you're still missing fonts this way, but at least it's standalone. (Ofc you still have to download the external font files used manually) – George Dimitriadis Dec 23 '19 at 16:30
48

Found a step-by-step way to achieve this (for 1 font):
(as of Sep-9 2013)

  1. Choose your font at http://www.google.com/fonts
  2. Add the desired one to your collection using "Add to collection" blue button
  3. Click the "See all styles" button near "Remove from collection" button and make sure that you have selected other styles you may also need such as 'bold'...
  4. Click the 'Use' tab button on bottom right of the page
  5. Click the download button on top with a down arrow image
  6. Click on "zip file" on the the popup message that appears
  7. Click "Close" button on the popup
  8. Slowly scroll the page until you see the 3 tabs "Standrd|@import|Javascript"
  9. Click "@import" tab
  10. Select and copy the url between 'url(' and ')'
  11. Copy it on address bar in a new tab and go there
  12. Do "File > Save page as..." and name it "desiredfontname.css" (replace accordingly)
  13. Decompress the fonts .zip file you downloaded (.ttf should be extracted)
  14. Go to "http://ttf2woff.com/" and convert any .ttf extracted from zip to .woff
  15. Edit desiredfontname.css and replace any url within it [between 'url(' and ')'] with the corresponding converted .woff file you got on ttf2woff.com; path you write should be according to your server doc_root
  16. Save the file and move it at its final place and write the corresponding <link/> CSS tag to import these in your HTML page
  17. From now, refer to this font by its font-family name in your styles

That's it. Cause I had the same problem and the solution on top did not work for me.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1111161171159459134
  • 1,216
  • 2
  • 18
  • 28
25

The other answers are not wrong, but I found this to be the fastest way.

  1. Download the zip file from Google fonts and unzip it.
  2. Upload the font files 3 at a time to http://www.fontsquirrel.com/tools/webfont-generator
  3. Download the results.

Results contain all font formats: woff, svg, ttf, eot.

AND as an added bonus they generate the css file for you too!

sym3tri
  • 3,747
  • 1
  • 29
  • 25
  • +1 for an answer for which I didn't have to think while reading it. It would be awesome to find a direct google font converter without having to upload them previously. Example utility: when doing offline website management. – Meetai.com Aug 14 '14 at 00:44
  • This is very convenient to have the different font formats generated for you. But anyone using it be aware that the css generated is not very smart, it will generate two(or more) styles of the same font as separate font families, instead of one font family with different font weights. But its pretty easy to fix it yourself in the css. – Ken Sung Sep 04 '14 at 08:22
  • 2
    No converter needed. Look at Marco Kerwitz's answer – Herr_Schwabullek Apr 09 '15 at 11:22
  • I was able to upload 10 fonts in one go so it seem limitation of 3 fonts has been removed or increased – Adrian Hedley Dec 20 '18 at 09:59
11

If you'd like to explicitly declare your package dependencies or automate the download, you can add a npm package to pull in google fonts and serve locally.

Fontsource

Fontsource allows you to:

Self-host Open Source fonts in neatly bundled NPM packages.

You can search npm for scope:fontsource <typefacename> to browse the available fonts like @fontsource/roboto or @fontsource/open-sans and install like this:

$ npm install @fontsource/roboto    --save 
$ npm install @fontsource/open-sans --save 

Font Downloaders

For a more generic use case, there are several npm packages that will deliver fonts in two steps, first by installing the library, and then by pointing it to the font name and options you'd like to include.

Further Reading:

KyleMit
  • 30,350
  • 66
  • 462
  • 664
4

3 steps:

  1. Download your custom font on Goole Fonts and down load .css file ex: Download http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,300 and save as example.css
  2. Open file you download (example.css). Now you must download all font-face file and save them on fonts directory.
  3. Edit example.css: replace all font-face file to your .css download

Ex:

@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 400;
  src: local('Open Sans Italic'), local('OpenSans-Italic'), url(http://fonts.gstatic.com/s/opensans/v14/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2) format('woff2');
  unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
}

Look at src: -> url. Download http://fonts.gstatic.com/s/opensans/v14/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2 and save to fonts directory. After that change url to all your downloaded file. Now it will be look like

@font-face {
  font-family: 'Open Sans';
  font-style: italic;
  font-weight: 400;
  src: local('Open Sans Italic'), local('OpenSans-Italic'), url(fonts/xjAJXh38I15wypJXxuGMBvZraR2Tg8w2lzm7kLNL0-w.woff2) format('woff2');
  unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
}

** Download all fonts contain .css file Hope it will help u

duydb
  • 41
  • 1
2

Essentially you are including the font into your project.

@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: normal;
src: url('path/to/OpenSans.eot');
src: local('Open Sans'), local('OpenSans'), url('path/to/OpenSans.ttf') format('truetype');
medina
  • 766
  • 1
  • 11
  • 31
2

When using Google Fonts, your workflow is divided in 3 steps : "Select", "Customize", "Embed". If you look closely, at the right end of the "Use" page, there is a little arrow which allows you to download the font currently in your collection.

After that, and once the font is installed on your system, you just have to use it like any other regular font using the font-family CSS directive.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Pierre-Adrien
  • 2,836
  • 29
  • 30
2

1)Go at google fonts

2)select your font and it's a style

3)then you will get a link tag, side of link click on import

  1. copy its link present inside the URL(' here ') and paste it on the browser search bar.

  2. And then you will get something like this

  3. and then download it and save it where your project files are there!

7)and then include it in your HTML link tag or import it in your CSS file (https://i.stack.imgur.com/NvJ60.jpg) (https://i.stack.imgur.com/XwK0l.jpg)

8)and then use ton family mentioned in google fonts

0

I followed duydb's answer to produce the python code below that automates this process.

import requests

with open("example.css", "r") as f:
    text = f.read()
    urls = re.findall(r'(https?://[^\)]+)', text)

for url in urls:
    filename = url.split("/")[-1]
    r = requests.get(url)
    with open("./fonts/" + filename, "wb") as f: 
        f.write(r.content)
    text = text.replace(url, "/fonts/" + filename)

with open("example.css", "w") as f:
        f.write(text)

Hope this helps with some of the copy-paste death.

Wytamma Wirth
  • 543
  • 3
  • 12
-1

You need to download the font and reference it locally.

Download the CSS from the link you posted, then download all of the WOFF files and (if needed) convert them to TTF.

Then change the CSS from the link you posted to include the fonts locally.

From

url(http://themes.googleusercontent.com/static/fonts/opensans/v6/
DXI1ORHCpsQm3Vp6mXoaTXhCUOGz7vYGh680lGh-uXM.woff)

To

url(/path/to/font/font.woff)

Voila! There might be some more you need to do but the above is the basics. This article explains a little better.

Mattios550
  • 372
  • 1
  • 5
  • 18
-3

just download the font and extract it in a folder. then link that font. the below code worked for me properly.

body { 
  color: #000; 
  font-family:'Open Sans';
  src:url(../../font/Open_Sans/OpenSans-Light.ttf); 
}