[reportlab-users] UTF8 Encoded Strings

Tim Roberts timr at probo.com
Wed Oct 31 15:51:48 EDT 2007


Rich Shepard wrote:

>

> Are these available for reuse by others ... particularly me? :-)


Hmmm, I'll give that some thought.


>

>> I try to collect each section of the page into a function. Then I'll

>> have something like:

>>

>> def drawLeftHandSide( self, left, top, width, height ):

>> # Each piece returns its "bottom". That becomes the next "top".

>> top = self.drawCorporateLogo( left, top, width, height )

>> top -= inch / 4

>> top = self.drawTopSummary( left, top, width, height )

>>

>> def drawRightHandSide( canvas, left, top, width, height ):

>> ...

>>

>> def drawFrontPage( self, left, top, width, height ):

>> self.drawLeftHandSide( left, top, width/2, height )

>> self.drawRightHandSide( left+width/2, top, width/2, height )

>

> Why does drawRightHandSide reference the canvas while the other two

> reference self?


Because when I started out typing that example, I had them all pass a
canvas around, and I decided partway through that they should be members
of a class instead, and I forgot to change that one. That isn't
cut-and-pasted from a real-life example -- that's typed on the fly.



> What I really need to learn better is understanding where the cursor

> is at

> all times, and where text will print relative to that. I get the

> impression

> from my trial-and-error development today that the cursor remains at the

> lead of a block of printed text rather than at the end. Of course, I may

> well be totally wrong ... it wouldn't be the first time.


Remember that the PDF file itself does not have the concept of a
"cursor". That's strictly a by-product of the ReportLab text object,
which I don't use, in part because of this confusion. For the canvas
methods, which I use, every drawing function has to specify an X and a
Y. Keeping track the location is entirely up to me.



> Now that the file is actually attached to a message, if you have time

> to comment on it that would be very helpful.


I agree that it is kind of confusing. It really looks like you are
fighting the automatic tracking of the caret, and that you might be
better off calling the canvas methods directly (drawString,
drawRightString, drawCentredString). That would eliminate the
conceptual issue of mixing canvas calls (i.e., line), which take effect
immediately, and textobject calls (i.e., moveCursor) which don't take
effect until canvas.drawText.

Also, I wanted to point out something that might be non-obvious:

> ratingHead = """Importance

> Value Definition

> """

>

> ratingTab = """1 Equal importance

> 3 Weak importance of one over the other

> 5 Strong importance of one over the other

> 7 Demonstrated importance of one over the other

> 9 Absolute importance of one over the other

> 2, 4, 6, 8 Intermediate values between the two adjacent definitions

> """

>

> instructPara = """Using the table below, determine your preference for either the first

> item of the pair (on the left), or the second (on the right). Use the

> rating table to determine the strength of your preference, then fully

> fill in the rectangle for that preference strenth on the same line as

> the component pairs. If you prefer the second more than the first,

> also completely fill in the last box on the right."""


The indentation here becomes are part of the string. The first line of
"ratingHead" has no leading white space. The second line starts with 20
blanks. The third line consists of ONLY 20 blanks.

Same with instructPara. The first line has no leading white space. The
rest of the lines have 18 leading blanks.

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



More information about the reportlab-users mailing list