question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to register Google fonts?

See original GitHub issue

I want to use Roboto Font from Google. Currently I am using this font family for my Reacty app by using the css though the Font.Register() api of react-pdf doesn’t allow CSS, how am going to use this font? So, far I even tried to download TTF font files from Google and placed them under my /public/fonts folder and tried to use my /public/fonts/Roboto.ttf as a source and it fails to compile. Any idea?

When I tried below …

Font.register(
    '/public/fonts/Roboto-Regular.ttf',
    { family: 'Roboto' },
  );

I got the below error …

Unhandled Rejection (Error): Unknown font format push…/node_modules/@react-pdf/fontkit/dist/fontkit.browser.es.js.fontkit$1.create node_modules/@react-pdf/fontkit/dist/fontkit.browser.es.js:68 FontSource._callee2$ src/font/font.js:44 41 | } else if (BROWSER || isUrl(this.src)) { 42 | const { headers, body, method = ‘GET’ } = this.options; 43 | const data = await fetchFont(this.src, { method, body, headers });

44 | this.data = fontkit.create(data); | ^ 45 | } else { 46 | this.data = await new Promise((resolve, reject) => 47 | fontkit.open(this.src, (err, data) =>

I also tried below though it refers to CSS NOT the actual font so, obviously it won’t work and I can’t find direct links to font files on Google Fonts …

Font.register( ‘https://fonts.googleapis.com/css?family=Serif’, { family: ‘Roboto’ }, );

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
DewangScommented, Dec 10, 2020

@linhnguyen11

Thanks very much for the tip. Yes, it worked…!!! below is my code extract if anyone else run in to this issue.

import Roboto from "../../../../assets/fonts/Roboto-Regular.ttf"

Font.register({
    family: 'Roboto',
    format: "truetype",
    src: Roboto
  });

const styles = StyleSheet.create({
    page: {
      marginTop: 0,
      paddingLeft: "2mm"
    },
    blankText: {
        marginBottom: -10
    },
    textName: {
      marginLeft: 12,
      paddingTop: 15,
      fontSize: 12,
      fontWeight: 'heavy',
      textAlign: "justify",
      fontFamily: "Roboto",
    }
});
4reactions
linhnguyen11commented, Dec 10, 2020

“Can you try importing the font directly in your import statements of JS file like import someFont from ‘someDir/Lato-Italic.ttf’; and then use someFont in your src attribute of Font.register(). The only disadvantage is that you have to import all the fonts individually.” #680

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Started with the Google Fonts API
This guide explains how to use the Google Fonts API to add fonts to your web pages. You don't need to do any...
Read more >
Using web fonts – Fonts Knowledge - Google Fonts
First we'll need to load the font files; then, we need to reference those files and assign weights and styles (although these first...
Read more >
CSS Google Fonts - W3Schools
How To Use Google Fonts. Just add a special style sheet link in the <head> section and then refer to the font in...
Read more >
How to Use Google Fonts and Custom Fonts in Jotform
Getting Your Google Fonts Import Code · Click the icon on the top-right corner of the page to open the Selected family panel....
Read more >
Designing a font from scratch — and submitting it to Google ...
Create an entirely new font, from scratch, and submit it to Google Fonts in under 24 hours. I had a couple of letters...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found