16

In a project I work on, I need to generate a one page HTML file which contains everything inside it (JavaScript code, CSS, Images, etc.). So, there should be no outside references.

I am also planning to use Bootstrap but I am not sure if it is possible to embed font files inside HTML just like CSS.

Any idea?

tugberk
  • 57,477
  • 67
  • 243
  • 335

2 Answers2

26

Yes, that's actually possible. You can embed the font as a base64 encoded font like this:

@font-face{
    font-family: FontName;
    src: url(data:font/ttf;base64,THESTRING… ) format('truetype');
}

But it's not advisable as the base64 string can get rather big and cause performance issues.

damian
  • 5,314
  • 5
  • 34
  • 63
  • yes, this is what I want! thx! I wouldn't do this is if it was actually for a web page but a tool will generate an HTML report and everything needs to be in it. – tugberk Mar 30 '15 at 15:06
  • the problem was that I didn't know whether this is possible but now I kinow :) – tugberk Mar 30 '15 at 15:10
11

Yes you can. You must convert your font to a BASE64 byte and embed with Data URI, like this:

@font-face {
    font-family: 'yourfontname';
    src: url(data:application/x-font-woff;charset=utf-8;base64,**your base64 here**) format('woff');
    font-weight: normal;
    font-style: normal;
}

You can use this site to convert your font to Base64: Base64 encoder

cababunga
  • 3,090
  • 15
  • 23
Ricardo Pontual
  • 3,749
  • 3
  • 28
  • 43