10

I'm using @react-pdf/renderer version "1.6.8". However, am unable to get the following characters: čćđ. Instead get empty spaces.

The characters are from the Croatian language and can be tested on their official page.

https://react-pdf.org/repl?example=page-wrap

Might someone know what to setup or how to approach the problem. Haven't found anything on their official docs.

Github issue: https://github.com/diegomura/react-pdf/issues/780

3 Answers 3

12

Try this

import { Page, Font, Text } from '@react-pdf/renderer';

// Register Font
Font.register({
  family: "Roboto",
  src:
    "https://cdnjs.cloudflare.com/ajax/libs/ink/3.1.10/fonts/Roboto/roboto-light-webfont.ttf"
});

// Create style with font-family
const styles = StyleSheet.create({
  page: {
    fontFamily: "Roboto"
  },
});

const MyDocument = () => (

    <Document > 
      <Page size="A4" style={styles.page} > <!--Add Font style to the page-->
          <Text >Some text čćđ</Text>
      </Page>
    </Document>
  )

it works for me, for the Polish language

2
  • My code is crashing a lot after adding this :( Getting nbind.externalList[num].dereference is not a function error. Commented Mar 1, 2021 at 17:40
  • This solves the special character problem but what if i need more then one FontWeight? 300, 400, 500, 600? How do i ad several font weights. NEed some Bold text in the PDF aswell. Commented Sep 8, 2022 at 5:23
4

Just imported custom fonts like this and it worked for me:

import font from '/styles/localFonts/Poppins-Medium.ttf

Font.register({ family: 'Poppins', src: font })
4

If you want to add several font weights then use this approach:

import { Font } from "@react-pdf/renderer"

Font.register({
  family: "Roboto",
  fonts: [
    { src: "https://cdnjs.cloudflare.com/ajax/libs/ink/3.1.10/fonts/Roboto/roboto-light-webfont.ttf", fontWeight: 300 },
    { src: "https://cdnjs.cloudflare.com/ajax/libs/ink/3.1.10/fonts/Roboto/roboto-regular-webfont.ttf", fontWeight: 400 },
    { src: "https://cdnjs.cloudflare.com/ajax/libs/ink/3.1.10/fonts/Roboto/roboto-medium-webfont.ttf", fontWeight: 500 },
    { src: "https://cdnjs.cloudflare.com/ajax/libs/ink/3.1.10/fonts/Roboto/roboto-bold-webfont.ttf", fontWeight: 600 },
  ],
})

const styles = StyleSheet.create({
  page: {
    fontFamily: "Roboto",
    backgroundColor: "#ffffff",
    padding: 24,
  }
})
2
  • 2
    how do i get similar links for inter font? Commented Jan 12, 2023 at 9:41
  • @SalilRajkarnikar I dont know but i would sugest to try find a CDN link for Inter font. If there is no such then use some font that exists on CDN. There is possibility to import font files from local/project file structure. But you know what? PDF generating is a pain in a**. So stick to Roboto and enjoy the time you saved :) Commented Jan 12, 2023 at 18:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.