[reportlab-users] Justifying simple text, TrueType and unicode

Charlie Clark reportlab-users@reportlab.com
Mon, 08 Mar 2004 15:07:21 +0100

Isn't it nice to be able to solve your own problems?
> I'm working on a small project to create bills on letter-headed PDFs
> So far I've got two questions:
> 1) Using Platypus - if I use canvas.drawImage in myFirstPage(), the image
> doesn't appear in the resulting PDF but any text inserted works fine. It
> works fine when I call insert it in a story. I can live with this but it's
> not the way it should be. Any explanations?

Solved this myself, at least! I was painting the image "off" the page. I got 
confused with exactly where on the page Reportlab was going to draw the 
image and forgot to deduct the height of the image from it's startint 
position. I assume images like this get stored in the PDF even if you can't 
see them.
> 2) Justifying small-blocks of text. I've been through the documentation
> several times. It seems I have two alternatives - work with several
> justified paragraphs or work with text objects. I am currently trying to
> use textobjects but it seems that there is no direct method to justify the
> text. Do I have to do this myself using charspace() and wordspace() based
> on calculations of line length?

I've looked through the source of Flowables.Paragraph and this does indeed 
look the case. Now all I need to do is understand exactly what's going on to 
do this myself with textObject().

I also had some fun with TrueType fonts and UTF-8. I worked through several 
e-mails and after some trial and error finally came up with something that
a) worked 
and b) I understood.

In order to use non-ascii text and TrueType fonts it may be necessary to 
encode the text.

Simple example which will fail with TrueType
c = canvas.Canvas()
text = "Düsseldorf"

to get this to work, you must call the encode method on a unicode string
text = u"Düsseldorf".encode("UTF-8")

Hope this is useful for others.