I had this issue as well. I was able to resolve it by importing the font file as a variable then using that as the src value.
import font from "./font.ttf"
Font.register({
family: "FamilyName",
format: "truetype",
src: font
});
const styles = StyleSheet.create({
page: {
fontFamily: "FamilyName",
},
});
this worked for me.
Answer from Design Freek on Stack Overflow
» npm install @react-pdf/fontkit
I had this issue as well. I was able to resolve it by importing the font file as a variable then using that as the src value.
import font from "./font.ttf"
Font.register({
family: "FamilyName",
format: "truetype",
src: font
});
const styles = StyleSheet.create({
page: {
fontFamily: "FamilyName",
},
});
this worked for me.
I had the same issue,fortunately I was using Google fonts and I solved it by using curl on font import url like so:
curl 'https://fonts.googleapis.com/css2?family=Cairo&display=swap'
And copying the ttf url from the response which shows as such
@font-face {
font-family: 'Cairo';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/cairo/v20/SLXVc1nY6HkvangtZmpcWmhzfH5lWWgsQQ.ttf) format('truetype');
}
- in my case, I import the fonts as normal and it works. You can download the google fonts here . However, it is probably that you have to set up the webpack configuration.
import FontUbuntuRegular from './styles/pdfFonts/ubuntuRegular.ttf';
import FontUbuntuItalic from '.7styles/pdfFonts/ubuntuItalic.ttf';
import FontUbuntu700 from './styles/pdfFonts/ubuntu700.ttf';
Font.register({
family: 'Ubuntu',
fonts: [
{
src: FontUbuntuRegular,
},
{
src: FontUbuntuItalic,
fontWeight: 'bold',
},
{
src: FontUbuntu700,
fontWeight: 'normal',
fontStyle: 'italic',
},
],
});
2. (RECOMMENDED) EASY, and simple, NO webpack configuration.
I found another way to add the src, it was to look for the Webfont.
- I had to scroll to make an API call with sort by ALPHA (alphabetic ordered) Google Api Fonts click Here to look for your font image example.
- I scrolled and founded image example
- I added manually the "S" to HTTP. before ("http://... ")=> after("https://...").
- I added to my code easily, and it is working perfectly.
Font.register({
family: 'Ubuntu',
fonts: [
{
src: 'https://fonts.gstatic.com/s/questrial/v13/QdVUSTchPBm7nuUeVf7EuStkm20oJA.ttf',
},
{
src: 'https://fonts.gstatic.com/s/questrial/v13/QdVUSTchPBm7nuUeVf7EuStkm20oJA.ttf',
fontWeight: 'bold',
},
{
src: 'https://fonts.gstatic.com/s/questrial/v13/QdVUSTchPBm7nuUeVf7EuStkm20oJA.ttf',
fontWeight: 'normal',
fontStyle: 'italic',
},
],
});
This worked for me!
Font.register({
family: 'Roboto',
fonts: [
{
src: '/fonts/Roboto-Regular.ttf',
fontWeight: 400,
},
{
src: '/fonts/Roboto-Bold.ttf',
fontWeight: 700,
}
]
})
const styles = StyleSheet.create({
page: {
fontFamily: 'Roboto',
}
})
» npm install @react-pdf/fontkit
» npm install @react-pdf/pdfkit
» npm install @react-pdf/font
After doing a lot of research this one works for me. Add:
"@react-pdf/renderer":" 2.1.0",
"@react-pdf/font": "2.2.0",
To your package.json dependencies.
Then below dependencies add:
"resolutions": {
"@react-pdf/font": "2.2.0"
},
If you already have a resolutions object you'll just need to add this version to it. Notice there is no ^ in the version number.
Then remove yarn.lock or package-lock.json and re-run yarn/npm install
If you are using npm & not yarn, you could try the following
- Update
@react-pdf/rendererfrom^2.2.0to3.0.0 - Add an
overrideskey to mypackage.jsonthat looks like;
"overrides": {
"@react-pdf/font": "2.2.1",
"@react-pdf/pdfkit": "2.1.0"
}
Your package.json file should look something like;
// other stuff in the package.json file
"dependencies": {
// other dependencies in the package.json file
"@react-pdf/renderer": "3.0.0"
}
"overrides": {
"@react-pdf/font": "2.2.1",
"@react-pdf/pdfkit": "2.1.0"
}
// other stuff in the package.json file