[reportlab-users] sizes of fonts and images

Andy Robinson andy at reportlab.com
Mon Oct 23 16:40:54 EDT 2006

Luc Saffre wrote:
> Hello,
> I try to render a text in monospaced font together with an image.
> And I want the image to be exactly as wide as the line of text.
> Images and fonts apparently use different units because the following
> script renders the image too big (its width is about 1.5 times the text
> width):
> import os
> from reportlab.pdfgen import canvas
> from reportlab.lib.units import mm
> filename="tmp.pdf"
> X=30
> canvas = canvas.Canvas(filename)
> canvas.setFont("Courier", 10, leading=11)
> t=canvas.beginText()
> t.setTextOrigin(20*mm,200*mm)
> t.textOut('-'*X)

If you are using Courier 10 point, the characters are actually 6 points 
wide.  All courier glyphs are 600 ems wide.  10 is the 'height', 
including whatever whitespace the designer wanted at the top.

In general, create the string you want and use canvas.stringWidth(text, 
fontName, fontSize) to get its width, then use this for the image width. 
  This will work for proportional fonts too.

- Andy

More information about the reportlab-users mailing list