Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot render standard fonts locally (Vite, React, TS) #2818

Open
mrjeanp opened this issue Jul 15, 2024 · 4 comments
Open

Cannot render standard fonts locally (Vite, React, TS) #2818

mrjeanp opened this issue Jul 15, 2024 · 4 comments

Comments

@mrjeanp
Copy link

mrjeanp commented Jul 15, 2024

Describe the bug

Standard fonts aren't being rendered using fontFamily on a Vite/React/TS project.

To Reproduce

  1. Setup local environment.
  2. Set fontFamily: "Times-Roman" on <Text /> component.

Attached screenshots show that loading the fonts from the public folder (with different name) works but directly referencing "Times-Roman" does not render the font. Using the REPL works though, am I missing something?.

Expected behavior
To correctly render standard font that is pre-installed in the system.

👇 When I reference fontFamily: "Times-Roman", doesn't work.

image
The Code
import {
  Document,
  Font,
  Page,
  StyleSheet,
  Text,
  View,
} from "@react-pdf/renderer";
import { FC } from "react";

import TimesNewRoman from './assets/Times-New-Roman.ttf';

Font.register({
  family: "TimesNewRoman",
  format: "truetype",
  fonts: [
    {
      // src: "/fonts/Times-New-Roman.ttf",
      src: TimesNewRoman,
      fontStyle: "normal",
      fontWeight: "normal",
    },
    {
      src: "/fonts/Times-New-Roman-Italic.ttf",
      fontStyle: "italic",
      fontWeight: "normal",
    },
    {
      src: "/fonts/Times-New-Roman-Bold.ttf",
      fontStyle: "normal",
      fontWeight: "bold",
    },
    {
      src: "/fonts/Times-New-Roman-Bold-Italic.ttf",
      fontStyle: "italic",
      fontWeight: "bold",
    },
  ],
});

type MyDocumentProps = {
  cv?: Record<string, any>;
};
const CVDocument: FC<MyDocumentProps> = (props) => {
  const { cv } = props;

  return (
    <Document>
      <Page size="A4" style={styles.page}>
        <View
          style={{
            textAlign: "center",
            paddingVertical: 24,
          }}
        >
          <Text
            style={{
              fontSize: 32,
              fontFamily: "TimesNewRoman",
            }}
          >
            {"Loaded Times New Roman"}
          </Text>
        </View>
        <View style={styles.section}>
          <Text style={{ fontFamily: "Times-Roman" }}>WTF?</Text>
        </View>
      </Page>
    </Document>
  );
};

export default CVDocument;

// Create styles
const styles = StyleSheet.create({
  page: {
    backgroundColor: "white",
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1,
  },
  name: {
    fontSize: 32,
    fontFamily: "TimesNewRoman",
  },
});
Main component: image
Project structure image

Desktop:

  • OS: [MacOS]
  • Browser [chrome]
  • React-pdf version [^3.4.4]
@knuula
Copy link

knuula commented Jul 16, 2024

I am having the same issue here. It is specifically popping up with elements that are inside of elements that are "inline-flex", "inline", or "inline-block"

@knuula
Copy link

knuula commented Jul 16, 2024

some more context on this --- the non-standard fonts are rendering in some situations.

if the parent element is a "flex" for example, the non-standard font is rendering correctly.

if the parent element is a "inline-flex", the non-standard font will default to some basic font or not show up at all.

@mrjeanp
Copy link
Author

mrjeanp commented Jul 17, 2024

I cloned the react-pdf-site repo and the REPL page has the same problem on a localhost. I don't understand why,

localhost:3000/repl
image

react-pdf.org/repl:
image

@mrjeanp
Copy link
Author

mrjeanp commented Jul 22, 2024

Submitted a PR to fix this #2827

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants