Ask Your Question
2

How can I resolve the error "Undefined font: dejavusanscondensedB" in Python's pyfpdf library? Should I use built-in fonts or add the font using FPDF.add_font() function beforehand?

asked 2023-06-19 01:23:33 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-19 01:50:02 +0000

lalupa gravatar image

You have two options:

  1. Use built-in fonts: Replace the font name "dejavusanscondensedB" with one of the built-in fonts in pyfpdf. You can choose from the following fonts: Courier, Helvetica, Times, Symbol, ZapfDingbats.

  2. Add the font using FPDF.addfont() function beforehand: If you want to use the specific font "dejavusanscondensedB", you need to add it to pyfpdf using the FPDF.addfont() function.

Here is an example of how to add a font using the FPDF.add_font() function:

from fpdf import FPDF

class MyPDF(FPDF):
    def __init__(self):
        super().__init__()
        self.add_font('DejaVuSansCondensed', '', 'DejaVuSansCondensed.ttf', uni=True)

    def create_pdf(self):
        self.add_page()
        self.set_font('DejaVuSansCondensed', '', 12)
        self.cell(0, 10, 'Hello World')
        self.output('output.pdf')

MyPDF().create_pdf()

Make sure to replace the font name, file name, and font size with your own values.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-19 01:23:33 +0000

Seen: 11 times

Last updated: Jun 19 '23