[reportlab-users] split string by width.

Tim Roberts timr at probo.com
Fri Nov 5 16:28:57 EDT 2010


Tim Roberts wrote:

> Juan Efren Castillo wrote:

>> I need to print a single long string.

>>

>> reportlab.lib.utils.SimpleSplit works splitting by blank spaces, but

>> this string has no spaces

>>

>> My solution was to divide string into "chunks"

>>

>> y = 0

>> for t in chunks(r, MAXCHARS):

>> p.drawString(d.x, page_size[1] - d.y + y, t)

>> y -= p._leading

>>

>>

>> How ever I don't like this solution because MAXCHARS is always

>> different depending on Font and Size of the text.

>>

>> Any ideas how to calculate MAXCHARS ? or another solution to this?

> The only solution is to do it by hand, checking one character at a time

> until the string is too long: I assume "p" is your canvas and "r" has

> the incoming string. I might choose different variable names...

>

> # Assume maxWidth has the maximum width.

> tmpstr = ""

> for c in r:

> if p.stringWidth(s+c) > maxWidth:


That should be:

if p.stringWidth(tmpstr+c) > maxWidth:

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list