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

fontWeight doesn't work #796

Closed
yellowBanano opened this issue Dec 18, 2019 · 29 comments
Closed

fontWeight doesn't work #796

yellowBanano opened this issue Dec 18, 2019 · 29 comments
Labels

Comments

@yellowBanano
Copy link

Describe the bug
I tried to change the text style of a component with CSS property fontWeight.
Nothing has changed.
Use https://react-pdf.org/repl?example=font-register to check the accuracy, but repl won't launch.

To Reproduce
Steps to reproduce the behavior including code snippet (if applies):

  1. Go to https://react-pdf.org/repl?example=font-register
  2. See error

Expected behavior
Expected to see how font become bold

Desktop (please complete the following information):

  • OS: Ubuntu 19.04
  • Browser: Firefox 71.0
  • React-pdf version: 1.6.8
@cympak01
Copy link

cympak01 commented Dec 26, 2019

@yellowBanano fontWeight won't apply if you didn't register proper font.
Code below did the work:

Font.register({
family: 'Open Sans',
fonts: [
{ src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-regular.ttf' },
{ src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-600.ttf', fontWeight: 600 }
]
});

@juby-gif
Copy link

juby-gif commented Jun 5, 2020

+1

1 similar comment
@bartmika
Copy link

bartmika commented Jun 5, 2020

+1

@mnowotnik
Copy link

mnowotnik commented Jan 4, 2021

+1, Still doesn't work in 2.0.0-beta.18

workaround:
register font weight with mangled family name, e.g. Calibri-300

@eyeamkd
Copy link

eyeamkd commented Apr 12, 2021

i'm using the latest version and still facing this issue

@diegomura diegomura added the bug label Apr 15, 2021
@diegomura
Copy link
Owner

Fixed in latest commits!

@ljmerza
Copy link

ljmerza commented Sep 6, 2022

this doesnt work even with the default fonts built in. are you saying i have to import all the bold fonts even for the default fonts built in?

@Ahmad495
Copy link

This issue is still unresolved on the latest version.

@casestudy
Copy link

Any solution just yet? Still have the issue.

@kiliancabrera
Copy link

any solution?

1 similar comment
@mjbour
Copy link

mjbour commented Feb 23, 2023

any solution?

@Ahmad495
Copy link

Ahmad495 commented Mar 30, 2023

I used this package here link to get font weight working. One more important thing we must have to register font family first.
const companyPara = <span style={{ fontFamily: 'Roboto', fontWeight: 'bold' }}>Your Text</span>; const companyParaHTML = ReactDOMServer.renderToStaticMarkup(companyPara);

after this use tag from react-pdf-html to render in pdf

<Html style={{ fontSize: 10 }}>{companyParaHTML}</Html>

@ua-knight
Copy link

I had the same issue. Thanks to all who commented. Following advices above, this was what worked for me:

First, run the register code somewhere central in the app.

Font.register({
  family: 'Open Sans',
  fonts: [
    { src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-regular.ttf' },
    { src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-600.ttf', fontWeight: 600 }
  ]
})

And then add the font family where font weight is used in the document pdf.

const styles = StyleSheet.create({
    title: { fontFamily: 'Open Sans', fontSize: 20, fontWeight: 600 }, // doesn't work without fontFamily
})

@sebastianfdz
Copy link

Like @ua-knight said above:

IT DOESN'T WORK if 'fontFamily' is not specified inside style tag.

Took me a while too.

@ahmedibra28
Copy link

I had the same issue. Thanks to all who commented. Following advices above, this was what worked for me:

First, run the register code somewhere central in the app.

Font.register({
  family: 'Open Sans',
  fonts: [
    { src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-regular.ttf' },
    { src: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/open-sans-600.ttf', fontWeight: 600 }
  ]
})

And then add the font family where font weight is used in the document pdf.

const styles = StyleSheet.create({
    title: { fontFamily: 'Open Sans', fontSize: 20, fontWeight: 600 }, // doesn't work without fontFamily
})

It worked.
Thank you.

@sychd
Copy link

sychd commented Feb 21, 2024

You can use the default fonts provided by the library without needing to register another font if they fit your needs. The default fonts are listed here: https://react-pdf.org/fonts

For example:

<Text style={{ fontFamily: 'Helvetica-Bold' }}>
    Welcome
</Text>

@Umesh-Bhati
Copy link

You can use the default fonts provided by the library without needing to register another font if they fit your needs. The default fonts are listed here: https://react-pdf.org/fonts

For example:

<Text style={{ fontFamily: 'Helvetica-Bold' }}>
    Welcome
</Text>

not working for me
My project is using next 14.1.4

@Umesh-Bhati
Copy link

someone find another solution because all the above-suggested solutions are not working yet

My project uses Nextjs 14.1.1 with Shadcn UI that uses remix UI

@paystrup
Copy link

+1

@chetansgit
Copy link

It was working before in version 14.0.3, but when I updated to the next version, it stopped working again. Even after reverting to the previous version, it still doesn't work

@tgram-3D
Copy link

tgram-3D commented Jun 6, 2024

Same deal here. Worked in 3.1.14 but not working in 3.4.4. Even the built-in fonts are not working at all.

@Psarada84
Copy link

Same here.. I had to revert react-pdf/renderer to 3.2.2 from 3.4.4.

@rodericktech
Copy link

@Psarada84 Which version did you revert to? (Looks like 3.2.2 is not a release) - Hoping it might be a solution for me because I have tried versions from 3.1.2 up - on vanilla React 18.3 - and none of those I've tried so far have worked for me even with the 'Helvetica-Bold' built-in example shown above.

@rodericktech
Copy link

rodericktech commented Jun 24, 2024

Never mind the above - I found a solution for this (using the default fonts at least) here - #2750 - putting in an override to revert @react-pdf/layout to 3.11.5. This works at least as far up as version 3.4.2 of the renderer for me.

@stevec01
Copy link

stevec01 commented Aug 7, 2024

HI I have tried all of the above and i cannot have a single word in bold within a sentence. Any Ideas, thanks

@JacobWeisenburger
Copy link

Has this been solved? Because I can't seem to figure out how to make some text bold.

@MustehsanGhouri
Copy link

MustehsanGhouri commented Aug 27, 2024

Working Solution for Text Alignment and Wrapping in @react-pdf/renderer

This approach works effectively, allowing you to nest View components and manage text styles, alignment, and wrapping.

import React from "react";
import { Text, View, StyleSheet } from "@react-pdf/renderer";

const ExampleComponent = () => {
  const styles = StyleSheet.create({
    container: {
      flexDirection: "row",
      flexWrap: "wrap",
      alignItems: "center", // Aligns text vertically in the center
    },
    text: {
      margin: 6, // Handles spacing between words
      fontSize: 14,
      textAlign: "justify",
      color: "#000000",
      fontWeight: "normal",
      marginLeft: -3, // Adjust margin to align text correctly
    },
    textBold: {
      margin: 6, // Handles spacing between words
      fontSize: 14,
      textAlign: "justify",
      color: "#000000",
      fontWeight: "bold",
    },
  });

  return (
    <View style={styles.container}>
      <Text style={styles.text}>Hello</Text>
      <Text style={styles.textBold}>World</Text>
      <Text style={styles.text}>. Again, I am not bold, WOW</Text>
    </View>
  );
};

export default ExampleComponent;


![issue](https://github.com/user-attachments/assets/7f3078df-9294-40c6-badd-a2a01fc647c1)

@MustehsanGhouri
Copy link

issue

@ivanbicalho
Copy link

After checking multiple threads here the only way it worked for me was overriding the react-pdf/layout + using Times-Bold:

"dependencies": {
    "@react-pdf/renderer": "^3.4.4",
    ....
}
"overrides": {
    "@react-pdf/layout": "3.11.5"
}
const styles = StyleSheet.create({
    bold: {
      fontFamily: "Times-Bold",
    },
    ....

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

No branches or pull requests