Closed DewangS closed 3 years ago
This is clearly not working with Google fonts (TTF and woff2) I even tried to extract the gstatic link from the CSS and still getting the same error. i.e. unknown font format. So, the question is how do we use different font styles in a single document without being able to register the font? I would really appreciate if someone can please get back with even a YES or NO answer to this feature ?
Font.Register(
'https://fonts.gstatic.com/s/roboto/v20/KFOkCnqEu92Fr1MmgVxMIzIXKMnyrYk.woff2',
{ family: 'Roboto'})
"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."
@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",
}
});
@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", } });
Hi Thanks for your conclusion Do you know how to add fonts with bold, medium, italic ? Thanks
@nanokoko89 not sure about individual TTF as when you download the Roboto font family from Google, you do get various TTF like Roboto-Bold.ttf, Roboto-Medium.ttf etc .. I normally use the fontweight CSS styling to make it bold.
FTW Just found a better solution in https://stackoverflow.com/a/70577891
Resulting in something like:
Font.register({
family: 'Inter',
fonts: [
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyeMZhrib2Bg-4.ttf',
fontWeight: 100,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyfMZhrib2Bg-4.ttf',
fontWeight: 200,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuOKfMZhrib2Bg-4.ttf',
fontWeight: 300,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf',
fontWeight: 400,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuI6fMZhrib2Bg-4.ttf',
fontWeight: 500,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuGKYMZhrib2Bg-4.ttf',
fontWeight: 600,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuFuYMZhrib2Bg-4.ttf',
fontWeight: 700,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyYMZhrib2Bg-4.ttf',
fontWeight: 800,
},
{
src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuBWYMZhrib2Bg-4.ttf',
fontWeight: 900,
},
],
});
This actually works for me, and coincidentally I wanted the same font 'Inter'. And all weights are also accessible. Thank you for the solution.
FTW Just found a better solution in https://stackoverflow.com/a/70577891
Resulting in something like:
Font.register({ family: 'Inter', fonts: [ { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyeMZhrib2Bg-4.ttf', fontWeight: 100, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyfMZhrib2Bg-4.ttf', fontWeight: 200, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuOKfMZhrib2Bg-4.ttf', fontWeight: 300, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf', fontWeight: 400, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuI6fMZhrib2Bg-4.ttf', fontWeight: 500, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuGKYMZhrib2Bg-4.ttf', fontWeight: 600, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuFuYMZhrib2Bg-4.ttf', fontWeight: 700, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyYMZhrib2Bg-4.ttf', fontWeight: 800, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuBWYMZhrib2Bg-4.ttf', fontWeight: 900, }, ], });
bless you, wanted the same font
FTW Just found a better solution in https://stackoverflow.com/a/70577891
Resulting in something like:
Font.register({ family: 'Inter', fonts: [ { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyeMZhrib2Bg-4.ttf', fontWeight: 100, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyfMZhrib2Bg-4.ttf', fontWeight: 200, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuOKfMZhrib2Bg-4.ttf', fontWeight: 300, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf', fontWeight: 400, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuI6fMZhrib2Bg-4.ttf', fontWeight: 500, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuGKYMZhrib2Bg-4.ttf', fontWeight: 600, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuFuYMZhrib2Bg-4.ttf', fontWeight: 700, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyYMZhrib2Bg-4.ttf', fontWeight: 800, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuBWYMZhrib2Bg-4.ttf', fontWeight: 900, }, ], });
it worked here too... thank you
@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", } });
It didn't work form me, got following error
Cannot find module './Roboto-Regular.ttf' or its corresponding type declarations.
FTW Just found a better solution in https://stackoverflow.com/a/70577891 Resulting in something like:
Font.register({ family: 'Inter', fonts: [ { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyeMZhrib2Bg-4.ttf', fontWeight: 100, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyfMZhrib2Bg-4.ttf', fontWeight: 200, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuOKfMZhrib2Bg-4.ttf', fontWeight: 300, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf', fontWeight: 400, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuI6fMZhrib2Bg-4.ttf', fontWeight: 500, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuGKYMZhrib2Bg-4.ttf', fontWeight: 600, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuFuYMZhrib2Bg-4.ttf', fontWeight: 700, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyYMZhrib2Bg-4.ttf', fontWeight: 800, }, { src: 'http://fonts.gstatic.com/s/inter/v12/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuBWYMZhrib2Bg-4.ttf', fontWeight: 900, }, ], });
bless you, wanted the same font
I got this error
Could not resolve font for Inter, fontWeight 800
@Elham13 register your fonts with fontWeight { src: RobotoBoldItalic, fontWeight: 'bold', fontStyle: 'italic', },
@Elham13 register your fonts with fontWeight
{ src: RobotoBoldItalic, fontWeight: 'bold', fontStyle: 'italic', },
Works for me, thankssss!
In C# if the font is downloaded into specific folder, i.e wwwroot/fonts QuestPDF FontManager
can register all TTF font types in the folder and handle the family names and styles:
var cairoStream = File.OpenRead("wwwroot/fonts/Cairo/Cairo-VariableFont_slnt,wght.ttf");
var latoStream = File.OpenRead("wwwroot/fonts/Lato/Lato-Regular.ttf");
FontManager.RegisterFont(cairoStream);
FontManager.RegisterFont(latoStream);
For TTF URLs of Google Fonts, check this gist.
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 ....
I got the below error ...
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' }, );