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

Error: Unknown font format

Open ArseniySushCorp opened this issue 3 years ago • 1 comments

Describe the bug Import fonts is broken, when pass the file path.

To Reproduce https://react-pdf.org/repl?example=font-register

ArseniySushCorp avatar Jul 26 '22 04:07 ArseniySushCorp

This was fixed in #1934, we are just waiting for a new release of react-pdf now.

carlobeltrame avatar Aug 16 '22 14:08 carlobeltrame

Looks like that change shipped, but the example is still broken.

EvHaus avatar Sep 25 '22 19:09 EvHaus

This can be fixed by adding the fonts to the public folder in the project root as the source needs to be absolute. ie. expose it at https://react-pdf.org/Roboto-Regular.ttf

b-bot avatar Oct 12 '23 11:10 b-bot

For Next.JS, this fixed the issue for me.

  • Place the fonts in the public directory. I put mine in /public/fonts.

import { PropsWithChildren } from "react";

import { Document, Font, Page, StyleSheet, View } from "@react-pdf/renderer";

const defaultFont = "NunitoSans"; // Set the font family name
 // Regular 400 font. The URL should be publicly accessible
const regularFont = `${process.env.NEXT_PUBLIC_APP_URL}/fonts/nunito-sans.woff2`;

Font.register({
  family: defaultFont,
  fonts: [
    {
      src: regularFont,
      fontWeight: 400,
    },
  ],
});

// Reference font
const styles = StyleSheet.create({
  page: {
    fontFamily: defaultFont,
  },
});

const PdfDocument: React.FC<PropsWithChildren> = ({ children }) => {
  return (
    <Document>
      <Page style={styles.page}>
        <View>{children}</View>
      </Page>
    </Document>
  );
};

export default PdfDocument;

paschaldev avatar Jan 14 '24 17:01 paschaldev

https://github.com/diegomura/react-pdf/issues/2223#issuecomment-1943292145

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