[reportlab-users] True Type fonts - newbie question

Robin Becker robin at reportlab.com
Mon May 16 05:33:51 EDT 2011


On 16/05/2011 10:08, Glenn Linderman wrote:
........

The getAvailableFonts is actually wrong and is being stupid. It turns out that
it defers to PdfDoc.getAvailableFonts which is also stupid and wrong. Both of
these ought to be named something like getUsedFonts or getActualizedFonts or
somesuch. Turns out that except for the standard 14 we don't internalize the
fonts until they're actually used.

Try the script below and you should actually see the font end up in the list. I
think availableFonts should refer to those that have been registered even if not
yet used.


import sys, os, re, getopt
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import LETTER, A4, LEGAL, ELEVENSEVENTEEN
from reportlab.lib.units import cm, mm, inch, pica
def list_available_fonts():
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Times-New-Roman', 'c:\\windows\\fonts\\times.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBd',
'c:\\windows\\fonts\\timesBd.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanIt',
'c:\\windows\\fonts\\timesI.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBI',
'c:\\windows\\fonts\\timesBI.ttf'))
pdf = Canvas(sys.stdout)
pdf.setFont('Times-New-Roman', 16)
pdf.drawString(72,72,'Hello World')
all_fonts = pdf.getAvailableFonts()
print all_fonts

list_available_fonts()


--
Robin Becker


More information about the reportlab-users mailing list