[reportlab-users] ValueError: redefining named object

Robin Becker robin at reportlab.com
Mon Feb 18 09:22:19 EST 2008


jo wrote:

> Robin Becker ha scritto:

>> jo wrote:

>>>

>>> I see the line that cause this error is this one:

>>> self.ss['tx'].setFont('OCR', 10)

>>>

>>> seems reportlab can performs setFont() only the first time but this

>>> happen

>>> only when the font is defined by user.

>>> I wonder if there's a workaround to avoid this error.

>>>

>>> def display(self):

>>> self.ss['tx'] = self.ss['cnvs'].beginText()

>>> self.ss['eli'] = self.ss['cnvs'].beginPath()

>>> self.ss['tx'].setHorizScale(100)

>>> self.ss['tx'].setTextOrigin(0,0)

>>> self.ss['tx'].moveCursor(

>>> (self.ss['borderx'] + (self.ss['pxriga'] * 31)),

>>> (self.ss['bordery'] + (self.ss['pxriga'] * 1))

>>> )

>>> self.ss['tx'].textOut("printing...")

>>> self.ss['cnvs'].drawText(self.ss['tx'])

>>> self.ss['cnvs'].showPage()

>>> self.ss['cnvs'].save()

>>>

>>> jo

>> .......

>> try:

>> self.ss['tx'].setFont('OCR', 10)

>> except:

>> pass

>>

>> or better

>>

>> def startup(self):

>> # do things required only once

>> self.ss['tx'].setFont('OCR', 10)

>>

> Thank you, Robin for your help.

>

> Your tip should be a solution only if I use the same font and the same

> size in whole document,

> infortunately I have to use more than one font and more than one size in

> the entire document,

> because reportlab uses the last font setting, I am not able to change

> font and size.

>

> if I use 'OCR', size 10 to print the 1st line and I need to change the

> size from 10 to 8 to print the 2nd line,

> I can't do that.

.......
I'm probably being stupid, but have you registered these fonts before attempting
to use them. Looked more closely at your example and your're actually using a
textobject to do this output. That being said you should be able to change the
font back and forth as many times as you want.

This certainly works for me

from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics, ttfonts
pdfmetrics.registerFont(ttfonts.TTFont("Rina", "rina.ttf"))
canv = canvas.Canvas('out.pdf')
t=canv.beginText(72,72)
t.setFont('Rina',10)
t.setFillColor((1,0,0))
t.textOut('hello')
t.setFont('Rina',12)
t.setFillColor((0,1,0))
t.textOut(' cruel')
t.setFont('Rina',8)
t.setFillColor((0,0,1))
t.textOut(' World!')
canv.drawText(t)
canv.showPage()
canv.save()
--
Robin Becker


More information about the reportlab-users mailing list