2

I'm trying to use the python package fpdf2 to generate a pdf using my python app.

But it gives me the error python pyfpdf Undefined font: dejavusanscondensedB - Use built-in fonts or FPDF.add_font() beforehand when I try to use a custom font.

here are my code

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.add_font(fname="DejaVuSansCondensed.ttf")
pdf.set_font('DejaVuSansCondensed', size=14)
    .......
    ......
pdf.output(f"{instance.app_user.first_name} {instance.app_user.last_name} - {time}.pdf")  

frankfalse
  • 1,553
  • 1
  • 4
  • 17
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116

1 Answers1

0
pdf.add_font(fname="DejaVuSansCondensed.ttf")

In this way, this instruction is going to look for the font inside the default fonts of the lib, you have to send the path to the font as parameter. It can be a relative or an absolute one.

Also, i found that not all fonts.ttf files are acepted by FPDF2, its weird, at this moment im only using similar fonts from google (https://fonts.google.com/specimen/Open+Sans).

See ya

UPDATE

You have to add the font to the instance of fpdf that you are using.

pdf = FPDF()

pdf = FPDF(orientation = 'P', unit = 'cm',format = (27.09, 36.12))
pdf.add_font(family= "Roboto", style = "B" , fname = r"C:\Users\Administrator\fonts\Roboto\Roboto-Bold.ttf")
Carlost
  • 478
  • 5
  • 13