[reportlab-users] Finding out the width/height of text
Meitham Jamaa
meitham at reportlab.com
Mon Jan 18 08:52:26 EST 2010
Jotham wrote:
> Hello,
>
> I am wanting to use ReportLab to generate both a bitmap (for live preview in
> a client application) and PDF for dispatch to a printer. I have a client
> application that allows some basic alterations to be made by the user, for
> this to work I need to be able to find out some geometry information for a
> given piece of text at a given font and style. I can't see how to do this
> in ReportLab. Ideally I would be able to render out just the text in
> question to a bitmap surface of some kind, work with that, and once
> everything is finalised generate the PDF.
If you text is one line string then you can use
from reportlab.pdfbase.pdfmetrics import stringWidth
textWidth = stringWidth(text, fontName, fontSize)
If your text was multi-lines, assuming you are working in a rectangular area with defined width,
then do
from reportlab.lib.utils import simpleSplit
lines = simpleSplit(text, fontName, fontSize, maxWidth)
lines is a list of all the lines of your paragraph, if you know the line spacing value then the
height of the paragraph can be calculated as lineSpacing*len(lines)
I hope this helps
Meitham
More information about the reportlab-users
mailing list