szabgab / code-maven.com

The content of the Code-Maven site
https://code-maven.com/
30 stars 54 forks source link

Adding new fonts #96

Open Justinjay282 opened 4 years ago

Justinjay282 commented 4 years ago

Hi sir @szabgab, I see a code of how to write a text in pdf using python here https://code-maven.com/creating-pdf-files-using-python. my concern is this

from reportlab.pdfgen import canvas

pdf_file = 'hello_world_fonts.pdf'

can = canvas.Canvas(pdf_file)

print(can.getAvailableFonts())
# 'Courier', 'Courier-Bold', 'Courier-BoldOblique', 'Courier-Oblique',
# 'Helvetica', 'Helvetica-Bold', 'Helvetica-BoldOblique', 'Helvetica-Oblique',
# 'Symbol',
# 'Times-Bold', 'Times-BoldItalic', 'Times-Italic', 'Times-Roman',
# 'ZapfDingbats'

can.setFont("Helvetica", 24)
can.drawString(20, 400, "Hello")
can.drawString(40, 360, "World")

can.setFont("Courier", 16)
can.drawString(60, 300, "How are you?")

can.showPage()
can.save()

How can I add a font on the list? like Cambria. thank you

szabgab commented 4 years ago

I am not good at fonts, but do you have it installed on your computer? Where is it located?

Justinjay282 commented 4 years ago

@szabgab, thank you for your reply. I managed to solve this by downloading another font with a ttf extension file name. I just got confused since the only true type fonts that can be registered using report lab are on this list https://nationalband.com/wp-content/uploads/2015/04/fontlist.pdf. I just tried to download a font with a ttf extension and then it works. I appreciate your reply thank you!