react-pdf: Loading and registering fonts is broken
Describe the bug
When attempting to load the source of a font, locally or from an external URL, an error comes back as Error: Unknown font format like in your example here
https://react-pdf.org/repl?example=font-register
To Reproduce Steps to reproduce the behavior including code snippet (if applies):
- Register a font
- See console errors & failure to load the doc
Expected behavior Fonts are loaded successfully and applied to the document.
Screenshots
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 8
- Comments: 36 (2 by maintainers)
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.
I am also unable to register fonts. code:
code, variation 2:
console:
UPDATE: I was able to get fonts to work using a .ttf url. If you’re using a google font you can do this by curling the google font url
Taking the .ttf url that comes back, and put that into the
srcFigured it out with a huge help from my colleague. With typescript you need declarations.d.ts file in your src folder.
If anyone still has an issue with font loading in Next.js, here is my workaround:
theme/fonts/pt-sans-regular.tff).fonts.d.tsin your typings folder with this content:Works fine for me with
PDFDownloadLink, but not working properly withPDFViewer. WithPDFViewerworks only after navigating to a page where the viewer is placed, but after refresh font is not applied.Thank you!!! Why isn’t this in the docs?
I had this problem using NextJS. I solved this problem using require and installing
next-fonts. So a downloaded the .tff and put it at the same folder. This was a really tricky problem, I tried using static folder and url. It was working at dev mode and local builds, but once I sent to production using AWS Amplify, my PDF Viewer was broken.Work fine for me!! Tysm 😄
I am having the same problem. I cannot get fonts to register with any of these methods and end up getting:
× Unhandled Rejection (Error): Unknown font formatThe fonts are from here: https://fonts.google.com/specimen/Roboto?preview.text_type=custom
Has anyone fixed this? We have tried doing it with codes below. Still getting the same error.
Currently I solved the problem by creating an “assets” folder inside the “public” folder. Inside assets I placed my fonts and my images and it is working.
Font.register({family:"Montserrat", src: "/assets/Montserrat.ttf"})This video was very useful! The solution is in min 7
Video
@DewangS Thanks same trouble here !
It works!
In case anyone encounters similar issue or any issue with font loading in fact, this may be helpful. It turned out the fonts I was trying to register had wrong metadata and pdf-react couldn’t recognise them as two different fonts. I have tried converting them from woff to ttf and back as some people were suggesting which didn’t help. At the end I ended up getting http://birdfont.org/ and editing the metadata and exporting the fonts in ttf format. Once I have done that registering of different weights and different fonts couldn’t be smoother.
Has anyone got lucky with registering multiple custom fonts?
I have been trying something like this…
It’s always the font that is first being used in the document that is loaded correctly and the other one turns out muggled up. I don’t think it’s the font issue as I tried swapping them around in the document and the result is the same. See the images attached…
Can anyone tell me which font formats are acceptable? I know TrueType, any others?