react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Unable to load local font file with Font.register in Next.js

Open fury0928 opened this issue 3 years ago • 31 comments

OS: Windows 11 React-pdf version: 3.1.6

Description: Hello, I've been trying to register a font for the document from the local file system (since I'm using Nex.js). What i've noticed is that if I use the following, the document is never generated

Font.register({ family: "Helvetica Neue", fonts: [{ src: "/public/fonts/helveticaNeue-std.woff2" }], });

I need to use local font because Helvetica font is not on google fonts, but it doesn't read the from the local Could you give me the solution for this?

Thank you

fury0928 avatar Mar 02 '23 16:03 fury0928

@diegomura , could you help me on this please? Thank you

fury0928 avatar Mar 06 '23 16:03 fury0928

@leonace924 remove public from the font path

jeetiss avatar Mar 06 '23 16:03 jeetiss

@jeetiss Main problem is font glyph doesn't work, like this symbol do you have any solution for this? I used Helvetica Neue font, it works fine on local but doesn't work on pdf even I import the same font

image

fury0928 avatar Mar 06 '23 16:03 fury0928

This shows same on playground too - https://react-pdf.org/repl

image

fury0928 avatar Mar 06 '23 17:03 fury0928

Still having an issue with this.

In NextJS, I can't do

import font from './font.ttf';

To load a local font, the doc recommends doing

import localFont from 'next/font/local'
const font = localFont({ src: './font.ttf' })

However, I am not able to use font in React-pdf's Font.register() like this

Font.register({
  family: "Font",
  src: font
});

Is there any way to make NextJS and React-PDF work together?

zdettwiler avatar Jul 25 '23 16:07 zdettwiler

Hi,

Is there any update?

tunjayhuseynov avatar Sep 08 '23 14:09 tunjayhuseynov

I am having the same issue... :(

denisprado avatar Sep 22 '23 16:09 denisprado

Same here

ferisjuan avatar Oct 04 '23 21:10 ferisjuan

any update ?

Ariuk0112 avatar Oct 23 '23 10:10 Ariuk0112

Though this is closed, let me just leave this here incase someone needs it. I managed to make it work through trial and error by registering like this:

Font.register({
  family: "FontName",
  src: "/fonts/font-file.otf",
});

I have my fonts on $root/public directory. Using relative path .../.../public/font/font-file didn't work for me. probably related to nextjs's webpack config. After figuring out the solution, I saw this documentation from next.js, pointing out how to access public files.

rafhpabustan avatar Nov 24 '23 20:11 rafhpabustan

same here , can't load the font (be it ttf, otf or woff2):

Screenshot 2023-12-10 at 12 45 17
import {
  Page,
  View,
  Document,
  StyleSheet,
  Text,
  Font,
} from "@react-pdf/renderer";

Font.register({
  family: "inter",
  src: "/fonts/Inter-Regular.otf",
});

// Create styles
const styles = StyleSheet.create({
  page: {
    backgroundColor: "#ffffff",
    padding: "48px",
    fontSize: "16px",
    fontFamily: "inter",
  },
  header: {
    height: "27%",
    display: "flex",
    alignItems: "stretch",
  },
  headerLeft: {
    width: "60%",
    display: "flex",
    flexDirection: "column",
    gap: "16px",
    justifyContent: "space-between",
  },
});


mmany avatar Dec 10 '23 11:12 mmany

Same issue. Can this be reopened @leonace924?

KayBeSee avatar Dec 11 '23 08:12 KayBeSee

@

Though this is closed, let me just leave this here incase someone needs it. I managed to make it work through trial and error by registering like this:

Font.register({
  family: "FontName",
  src: "/fonts/font-file.otf",
});

I have my fonts on $root/public directory. Using relative path .../.../public/font/font-file didn't work for me. probably related to nextjs's webpack config. After figuring out the solution, I saw this documentation from next.js, pointing out how to access public files.

This works for me, i moved my fonts file from app to public and directly use it on the register function like this: Font.register({ family: 'Calibri', format: 'truetype', src: "/fonts/calibri.ttf", });

isaico avatar Jan 20 '24 02:01 isaico

image it works, but only if I wrote string in src (NextJs 12.2.4, "@react-pdf/renderer": "3.3.8")

Sergey-lang avatar Feb 14 '24 08:02 Sergey-lang

Though this is closed, let me just leave this here incase someone needs it. I managed to make it work through trial and error by registering like this:

Font.register({
  family: "FontName",
  src: "/fonts/font-file.otf",
});

I have my fonts on $root/public directory. Using relative path .../.../public/font/font-file didn't work for me. probably related to nextjs's webpack config. After figuring out the solution, I saw this documentation from next.js, pointing out how to access public files.

Thanks so much ! it works for me !

ln-dev7 avatar Mar 07 '24 18:03 ln-dev7

@

Though this is closed, let me just leave this here incase someone needs it. I managed to make it work through trial and error by registering like this:

Font.register({
  family: "FontName",
  src: "/fonts/font-file.otf",
});

I have my fonts on $root/public directory. Using relative path .../.../public/font/font-file didn't work for me. probably related to nextjs's webpack config. After figuring out the solution, I saw this documentation from next.js, pointing out how to access public files.

This works for me, i moved my fonts file from app to public and directly use it on the register function like this: Font.register({ family: 'Calibri', format: 'truetype', src: "/fonts/calibri.ttf", });

Thanks this worked for me

aliBit68 avatar Apr 17 '24 17:04 aliBit68

I am having the same issue... :(

ashkankiani avatar May 06 '24 07:05 ashkankiani

I'm having this issue as well, even with the relative path that is supposed to start from the public directory. Currently running nextjs 14.2.4 and version 9.0.0 of react pdf. Tried "/fonts/my-font.otf" and various levels of drilling up directories with "../" without success. This works just fine for other relative references, like the @font-face lines in the website css, but for whatever reason fails when loading it with Font.register.

jaandrews avatar Jul 02 '24 09:07 jaandrews

I'm having this issue as well, even with the relative path that is supposed to start from the public directory. Currently running nextjs 14.2.4 and version 9.0.0 of react pdf. Tried "/fonts/my-font.otf" and various levels of drilling up directories with "../" without success. This works just fine for other relative references, like the @font-face lines in the website css, but for whatever reason fails when loading it with Font.register.

i have same problem in next 14.2.3 and add font.register do you find any solotion?

zahranaserii avatar Jul 20 '24 12:07 zahranaserii

I am having the same issue. Currently running nextjs 14.2.5 and react-pdf/renderer 3.4.4. When I use the I get the following error in the console. CleanShot 2024-07-26 at 11 25 03 CleanShot 2024-07-26 at 11 25 32

moitorrijos avatar Jul 26 '24 19:07 moitorrijos