[reportlab-users] Built-in versus embedded fonts

Robin Becker robin at reportlab.com
Wed Jun 24 06:14:00 EDT 2015


On 24/06/2015 07:17, Glenn Linderman wrote:
.............
> The rest is experimentation based on this discussion thread.
>
> I don't have the latest version of reportlab, but I think it is the latest one
> released.... there is a reportlab-3.1.8.dist-info in my site-packages, so I'll
> assume that is the version number.
>
> Glenn
........

Glenn, I'm not sure exactly where your PDFu class is going wrong, but
in latest code this works as expected using the new Canvas argument


############################################################
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
registerFont(TTFont('times','times.ttf'),)
registerFont(TTFont('times-bold','timesbd.ttf'),)
registerFont(TTFont('times-italic','timesi.ttf'),)
registerFont(TTFont('times-bolditalic','timesbi.ttf'),)
registerFontFamily('times',
		normal='times',
		bold='times-bold',
		italic='times-italic',
		boldItalic='times-bolditalic',)
sample = 'Hello World'
from reportlab.pdfbase.pdfmetrics import stringWidth
print "stringWidth(%r,'times',12) = %s" % (sample,stringWidth(sample, 'times',12))

canv = Canvas('thello.pdf',initialFontName='times')
print "canv.stringWidth(%r) = %s" % (sample,canv.stringWidth(sample))

canv.drawString(72,72,sample)
canv.save()
############################################################


this also works exactly as expected using the hacky rl_config stuff

############################################################
from reportlab import rl_config
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
rl_config._SAVED['canvas_basefontname'] = 'times'
rl_config._startUp()
registerFont(TTFont('times','times.ttf'))
registerFont(TTFont('times-bold','timesbd.ttf'))
registerFont(TTFont('times-italic','timesi.ttf'))
registerFont(TTFont('times-bolditalic','timesbi.ttf'))
registerFontFamily('times',
		normal='times',
		bold='times-bold',
		italic='times-italic',
		boldItalic='times-bolditalic')

sample = 'Hello World'
from reportlab.pdfbase.pdfmetrics import stringWidth
print "stringWidth(%r,'times',12) = %s" % (sample,stringWidth(sample, 'times',12))

canv = Canvas('thello1.pdf')
print "canv.stringWidth(%r) = %s" % (sample,canv.stringWidth(sample))

canv.drawString(72,72,sample)
canv.save()
############################################################

If I add these lines immediately prior to the final for loop in your example

myPDFinit()		
myPDF.register_fonts()
print myPDF.canv._fontname
print myPDF.rlfonts.keys()
for txt in ('012', 'abc', 'ABC'):

then I see this in the output

> C:\Users\rptlab\Downloads>rl-test.py
> Times-New-Roman
> ['Times-BoldItalic', 'Times-Italic', 'Times-Bold', 'Times']
> ('thiswid:', 30.0)
> Traceback (most recent call last):
>   File "C:\Users\rptlab\Downloads\rl-test.py", line 82, in <module>


so perhaps you should consider making the font set up code a bit more transparent
--
Robin Becker


More information about the reportlab-users mailing list