I am using the Open Sans font from Google fonts in my application using this import rule:
http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800"
When testing locally and I have no internet connection, the font no longer works.
What I have tried:
I have downloaded this font and set it up inside of my CSS file for #font-face,
but it shows all characters are different.
To fix your issue.
1.You first have to download the .ttf file from google fonts.
2.Put that file in your project and then add following to your css.
3.In url, specify the path of your .ttf file.
#font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
src: local('Open Sans Light'), local('OpenSans-Light'), url('your link to .ttf file') format('woff2');
}
use below code to import Google Web Font in CSS file.
#import url('http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800%22');
1st put this inside your head tag
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800%22' rel='stylesheet' type='text/css' />
All you need to do is add the font name to your CSS styles. For example:
h1 { font-family: 'Open Sans', Arial, serif; font-weight: 400; }
Related
When i open devtools in chrome or firefox, find my .page-header__hero-motivation and see render font is Comic Sans, not Caveat.
#font-face {
font-family: "Caveat";
src: url("../fonts/Caveat-Regular.woff2") format(woff2),
url("../fonts/Caveat-Regular.woff") format(woff);
font-weight: 400;
font-style: normal;
font-display: swap;
}
.page-header__hero-motivation {
font-family: "Caveat", "Comic Sans MS", cursive;
font-size: 18px;
color: #8E80A9;
}
My website tree
index.html
css -> style.css
fonts -> Caveat-Regular.woff2 & Caveat-Regular.woff
take this font from googlefonts and convert in online to woff&woff2.
chrome devtools
It seems that Comic Sans is the generic family not the font family. Don't set both, just set either font or generic family, also see https://developer.mozilla.org/en-US/docs/Web/CSS/font-family
Tell me if it works, otherwise post the entire code please.
OMG, i kill about 5 hours on that...
I forgot quotes in format #font-face
wrong
format(woff2)
right
format("woff2")
I have downloaded the Twitch font, which is Twitchy.TV.
I'm trying to use it, but when I type the font, it doesn't recognize it.
I think it might be because Twitchy (.) TV. The period is messing it up.
I have tried putting it with "" or ''.
<div class="navigation">
<div class="left">
<img src="Logo.png" id="logoImage">
<h1>TWITCH</h1>
</div>
</div>
.left h1 { font-size: 22px; color: #fff; font-family: Twitchy.TV; }
Have you included a #font-face declaration in your CSS?
Nowadays the best format for using fonts on the web is .woff or woff2 files, if you don't have your font in this format there are a lot of web font converters available.
#font-face {
font-family: 'Twitchy.TV';
src: url('Twitchy.TV.woff2') format('woff2'),
url('Twitchy.TV.woff') format('woff')
}
(Assuming you have font files in the same directory as your stylesheet named Twitchy.TV.woff and/or Twitchy.TV.woff2)
If you're just testing the font during development and have it locally installed, you can specify a local installation like this:
#font-face {
font-family: 'Twitchy.TV';
src: local('Twitchy.TV');
}
Then you are able to use it like:
.left h1 {
font-family: 'Twitchy.TV';
}
Ensure that you have imported the font if it is from an external source!
If you have downloaded the font, make sure that you have added it to the list of font-families in your computer. This can be done in the Control Panel.
You need to import the font at the top of your CSS using #font-face. This allows you to use non-websafe fonts in your website. the src: url can be either a path in the local storage or on an external site.
#font-face {
font-family: "Twitchy.TV";
src: url("path/to/font.ttf");
}
You can use the font like this
.left h1 {
font-family: "Twitchy.TV";
}
first of all upload you font here https://transfonter.org/ and then you will have generated font prepared and add all your fonts to assets folder and in style.css or style.scss open downloaded folder from transfonter and copy what's inside stylesheets.scss and add to your style.css and then make sure to be correct path to fonts assets/fonts for exameple
#font-face {
font-family: 'Roboto';
src: url('Roboto-Black.woff2') format('woff2'),
url('Roboto-Black.woff') format('woff');
font-weight: 900;
font-style: normal;
}
And then in style.css use it, example
h1 {
color: red;
font-family: 'Roboto', sans-serif; //Here you add default font in case the browser
can't find Roboto font
}
I've tried to setup "Sans Forgetica Regular" font to be the used font for entire a webpage after installing the font on my desktop of cource, however, it didn't work!
Does there a way to make it work please?
<body style="font-family:'Sans Forgetica Regular'">Hello</body>
Or
<body style="font-family:'SansForgetica-Regular'">Hello</body>
You declare it in the CSS like this:
#font-face {font-family: Sans Forgetica Regular; src: url('Sans-Forgetica-Regular.otf'); }
Then try it:
<body style="font-family:'Sans Forgetica Regular'">Hello</body>
You need to use the #font-face rule:
In this example, the user's local copy of "Sans Forgetica Regular" is
used; if the user does not have that font installed (two different
names are tried), then the downloadable font named
"SansForgetica-Regular.woff" is used instead:
CSS
#font-face {
font-family: 'Sans Forgetica Regular';
src: local('Sans Forgetica Regular'),
local('Sans-Forgetica-Regular'),
url('SansForgetica-Regular.woff') format('woff');
}
body {
font-family: 'Sans Forgetica Regular';
}
HTML (Avoid using inline style)
<body>
...
</body>
I'm trying to import fonts with Google Fonts with #import and #font-face, in my CSS. I used to only use #font-face with downloaded fonts, but since it takes time to load, I preferred to use the Google Fonts method.
I didn't wanted the "bold" version of Roboto as the "bold" font in my website, so I used #font-face. However, I'm not sure of the way to use it. Is this correct to use #import and #font-face like this?
#import url('https://fonts.googleapis.com/css?family=Roboto:400,400i,500,500i');
#font-face
{
font-family: Roboto;
src: url("https://fonts.googleapis.com/css?family=Roboto:400");
}
#font-face
{
font-family: Roboto;
font-weight: bold;
src: url("https://fonts.googleapis.com/css?family=Roboto:500");
}
#font-face
{
font-family: Roboto;
font-style: italic;
src: url("https://fonts.googleapis.com/css?family=Roboto:400i");
}
#font-face
{
font-family: Roboto;
font-weight: bold;
font-style: italic;
src: url("https://fonts.googleapis.com/css?family=Roboto:500i");
}
I've the feeling I'm importing the fonts twice... but it doesn't even work! (The page displays the default browser font)
Thank you for reading and taking time to help me.
you can use like this in your stylesheet
#import url('https://fonts.googleapis.com/css?family=Roboto:400,400i,500,500i');
h2{
font-family:Roboto;
}
#font-face make sense when you loading font from files on your server. If you importing from google, use only import and write right properties to elements you want.
#import url('https://fonts.googleapis.com/css?family=Roboto:400,400i,500,500i');
h2 {
font-family:Roboto;
font-weight: 500;
}
I am using wkhtmltopdf to convert HTML files in PDF format; it gives surprisingly good results, rendering the PDF exactly as WebKit would do.
I am using Google Web Fonts to give users the possibility to customize the appearence of the document they edited, offering them the possibility to choose between a few fonts. It also works perfectly in a browser.
Problem is, I don't get the Google Fonts working when converting such HTML files to PDF with wkhtmltopdf. I read other people had the same issue.
Could anyone please help me fixing this?
EDIT: declaring #font-face directly in the CSS does not work either.
To convert HTML to PDF by wkhtmltopdf try to avoid woff font face. Use the truetype format of the Google Web Fonts with base64 encode.
Recently I tried to use a Google web font from Google Web Fonts. In the browser it shows correctly but it doesn't show after converting HTML to PDF.
After searching the web extensively, at last, I found tools to encode fonts to the base64 format and also got CSS for #font-face.
Read the solution here.
A simple solution: Base64-encode your font and embed it in the CSS:
#font-face {
font-family: 'OpenSans';
src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQA...
}
FontSquirrel can do this for you through the advanced options of their Webfont Generator; Google and other fonts can be encoded with this tool.
I've used this solution with pdfkit and wicked_pdf, which work through wkhtmltopdf, so I presume this should work with the native wkhtmltopdf as well.
I had this same problem and solved it by downloading the fonts and running it off of a local file in my web server with this font-face declaration:
#font-face {
font-family: 'PT Sans';
src: url( '/public/inc/fonts/PTS55F-webfont.eot');
src: url( '/public/inc/fonts/PTS55F-webfont.eot?#iefix') format('embedded-opentype'),
url( '/public/inc/fonts/PTS55F-webfont.woff') format('woff'),
url( '/public/inc/fonts/PTS55F-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
I was able to find this font on Font Squirrel, but your mileage may vary.
I think what happens with the fonts off of Google is that it's trying to load only the format that it thinks this browser wants, which for WebKit is woff (I believe). But wkhtmltopdf can't embed a woff font in a PDF, and so defaults back to sans-serif. By declaring all of them, the TrueType font is included, which is what the PDF actually uses.
Also, you need to define the font you want used as the first one in any font-family CSS definition or wkhtmltopdf will just ignore it.
I've just tested that using the #importnotation works:
<style>
#import 'https://fonts.googleapis.com/css?family=Montserrat';
</style>
I'm using django-wkhtmltopdf version 2.0.3
UPDATE: still working in django-wkhtmltopdf 3.3.0
I've been struggling with this for about two days now, my advice to you if none of the answers above work for you:
Install the font (ttf) in the machine that hosts the webserver and just require it in the css just like you would do with a common font.
body{
font-family: 'Lato';
}
Now wkhtmltopdf should be able to include the font
I must have tried a gazillion variations to get this work (from local). I am trying to get Open Sans Condensed embedded into a pdf via WKHtmlToPdf.
I think it is important that you download and install the Open Sans Condensed ttf directly from google and install it. It is also important to reboot to allow other services access after install. I downloaded the ttf from font-squirrel originally and condensed was listed as "Open Sans" in windows/fonts, which is wrong, if you look after the google install it is listed as "Open Sans Condensed Light" so even google's local('Open Sans Cond Light') script is wrong.
As mentioned before you need to be explicit with the stretch and weights, so this is what has worked for me:
#font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans');
}
#font-face {
font-family: 'Open Sans Condensed';
font-stretch:condensed;
font-style: normal;
font-weight: 300;
src: local('Open Sans Condensed Light');
}
##font-face { font-family: 'Open Sans Condensed Bold'; font-stretch:condensed; font-style: normal; font-weight: 700; src: local('Open Sans Condensed'), local('Open Sans Condensed Bold');}
In my case I just had to add this option
--javascript-delay 1000
and Google Font text started to show up.
[wkhtmltopdf 0.12.4 (with patched qt)]
The following works for me. Notice the inclusion of extended character sets (if you need them):
<style type="text/css">
#import url(https://fonts.googleapis.com/css?family=Open+Sans:300,600&display=swap&subset=cyrillic,cyrillic-ext,greek,greek-ext,latin-ext,vietnamese);
</style>
Make sure you're not declaring what font you are printing with in your print stylesheet.
Using #import was key to getting Google Fonts working with wkhtmltopdf but then I had issues with CJK (Chinese, Japanese, and Korean) characters not appearing. The solution was to switch to version 2 of the Google Font API (https://fonts.googleapis.com/css2).
Below is the code snippet I inserted into my HTML to get all the languages I needed to be supported via a test PDF. Keep in mind the performance implications of loading dozens of remote fonts.
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans";
[lang="sq"] {font-family: 'Noto Sans', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic";
[lang="ar"] {font-family: 'Noto Sans Arabic', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Bengali";
[lang="bn"] {font-family: 'Noto Sans Bengali', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Georgian";
[lang="ka"] {font-family: 'Noto Sans Georgian', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Gujarati";
[lang="gu"] {font-family: 'Noto Sans Gujarati', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Hebrew";
[lang="he"] {font-family: 'Noto Sans Hebrew', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Devanagari";
[lang="hi"] {font-family: 'Noto Sans Devanagari', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Serif+Tibetan";
[lang="bo"] {font-family: 'Noto Serif Tibetan', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Gurmukhi";
[lang="pa"] {font-family: 'Noto Sans Gurmukhi', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Nastaliq+Urdu";
[lang="ur"] {font-family: 'Noto Nastaliq Urdu', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+Tai+Viet";
[lang="vi"] {font-family: 'Noto Sans Tai Viet', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+SC";
[lang="zh-hans"] {font-family: 'Noto Sans SC', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+TC";
[lang="zh-hant"] {font-family: 'Noto Sans TC', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+JP";
[lang="ja"] {font-family: 'Noto Sans JP', sans-serif !important;}
</style>
<style>
#import "https://fonts.googleapis.com/css2?family=Noto+Sans+KR";
[lang="ko"] {font-family: 'Noto Sans KR', sans-serif !important;}
</style>