[reportlab-users] Newbie -- How to center text in a paragraph

Andy Robinson andy at reportlab.com
Thu Jun 22 17:17:48 EDT 2006

Jim Steil wrote:
> Ok, I've got that all working now, but now I have a couple of other 'funny'
> things going on.  
> 1.  I have 3 paragraphs in a frame.  I set the style on the first one to be
> centered and set the other 2 paragraphs to be left justified.  But, all
> three paragraphs are centered.
>     section5P1 = Paragraph(s5Text1, styleCentered)
>     section5P2 = Paragraph(s5Text2, style)
>     section5P3 = Paragraph(s5Text3, style)

Looking below, my hunch is that you have modified the normal style
in the standard stylesheet to be centered, and 'styleCenteredd' and 
'style' are references to the same centered object.  You should actually 
create a new style object.

> 2.  In my paragraph text, I was using the <b> and <i> tags to bold and
> italicize.  It was working fine until I changed my font to Arial.  Now none
> of my font decoration stuff is working.
> pdfmetrics.registerFont(TTFont('Arial', 'arial.ttf'))
> pdfmetrics.registerFont(TTFont('Arial Bold', 'arialbd.ttf'))
> pdfmetrics.registerFont(TTFont('Arial Bold Italic', 'arialbi.ttf'))
> pdfmetrics.registerFont(TTFont('Arial Italic', 'ariali.ttf'))
> baseFont = 'Arial'
> styleCentered = styles['Normal']
> styleCentered.alignment = 1
> styleCentered.fontName = baseFont
> text = '<b>This is my bold text</b>'
> paragraph = Paragraph(text, styleCentered)

What you have done here is to register 4 fonts, but as yet it has no 
knowledge that they are in the same 'family', so it doesn't know what to 
do when encountering a <b> or <i> tag.  The system has builtin knowledge 
of the 'font families' for Helvetica, Courier and Times but no others.
So you need to tell it which combinations of facename/bold/italic map to 
which actual font files:

from reportlab.lib.fonts import addMapping
addMapping('Arial', 0, 0, 'Arial')
addMapping('Arial', 0, 1, 'Arial Italic')
addMapping('Arial', 1, 0, 'Arial Bold')
addMapping('Arial', 1, 1, 'Arial Bold Italic')

The user guide mentions this for Rina, which is a poor example
as there were no variants and all we wanted to do was allow <b> and <i> 
through harmlessly.

- Andy

More information about the reportlab-users mailing list