[reportlab-users] ValueError: redefining named object
jo
jose.soares at sferacarta.com
Mon Feb 18 10:26:02 EST 2008
My propose is to print more than one page in the same document.
Infact the error is not raised if my pdf is only one page.
Probably there's something that I am doing wrong. :-\
I changed a little your sample to having it printing two pages.
It prints the first page without errors,
but the second time...
ValueError: redefining named object:
'toUnicodeCMap:SUBSET+OCRB10PitchBT-Regular+0'
----------------------------------------------------------------------------------------------------------------------------------------------------------------
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics, ttfonts
pdfmetrics.registerFont(ttfonts.TTFont("Rina", "rina.ttf"))
def page(line1,line2):
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(line1)
t.setFont('Rina',8)
t.setFillColor((0,0,1))
t.textOut(line2)
canv.drawText(t)
canv.showPage()
canv.save()
canv = canvas.Canvas('out.pdf')
page(line1='cruel',line2='world') #page 1
page(line1='wonderful',line2='world') #page 2
Robin Becker ha scritto:
> 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()
More information about the reportlab-users
mailing list