[reportlab-users] TTF-Font Support

9 views
Skip to first unread message

Tom Py

unread,
Jan 23, 2013, 8:41:23 AM1/23/13
to reportlab-users
Hi Listusers,
 
currently I try to embed a TT-font (Futura-Font) in my report as described in the user guide (v2.6, 3.5 TrueType Font Support).
 
But all I get is a pdf-file with a blank page an an error message ('An error exists on this page. Acrobat may not display ...').
 
When I convert the ttf-file to an afm-/pfb-file and embed the font as described in section 3.3 (Using non-standard Type 1 fonts), I get the message 'Cannot extract the embedded font "FuturaBT-Light". Some characters may noct display or print correctly.
 
In both cases "warnOnMissingFontGlyphs" is deactivated.
 
Any ideas?
 
 
Thanks for help,
 
Tom
 
 

Tim Roberts

unread,
Jan 23, 2013, 1:27:23 PM1/23/13
to reportlab-users
Tom Py wrote:
>
> currently I try to embed a TT-font (Futura-Font) in my report as
> described in the user guide (v2.6, 3.5 TrueType Font Support).
>
> But all I get is a pdf-file with a blank page an an error message ('An
> error exists on this page. Acrobat may not display ...').

Can you show us your code? TrueType fonts as a rule work fine, so I'm
guessing there's some setup problem.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users

Tom Py

unread,
Jan 24, 2013, 5:12:37 AM1/24/13
to reportlab-users
Hi Tim,
 
thats the code:
 
import os
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, landscape

actFolder = os.getcwd() + os.sep

fontFile = 'futural.ttf'
fontName = 'FuturaBT-Light'

pdfmetrics.registerFont(TTFont(fontName, actFolder + fontFile))

myCanvas = canvas.Canvas("test.pdf", pagesize = landscape(A4))
myCanvas.setFont(fontName, 10.)
myCanvas.drawCentredString(100, 100, 'Hello World')

myCanvas.showPage()
myCanvas.save()
#myCanvas.getpdfdata()
 
In this case, everything works fine. But when I use 'myCanvas.getpdfdata()', I'll get this error:
 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\fonttest.py", line 23, in ftest
    myCanvas.getpdfdata()
  File "C:\Program Files (x86)\Python273\lib\site-packages\reportlab\pdfgen\canvas.py", line 1201, in getpdfdata
    return self._doc.GetPDFData(self)
  File "C:\Program Files (x86)\Python273\lib\site-packages\reportlab\pdfbase\pdfdoc.py", line 248, in GetPDFData
    fnt.addObjects(self)
  File "C:\Program Files (x86)\Python273\lib\site-packages\reportlab\pdfbase\ttfonts.py", line 1126, in addObjects
    pdfFont.ToUnicode = doc.Reference(cmapStream, 'toUnicodeCMap:' + baseFontName)
  File "C:\Program Files (x86)\Python273\lib\site-packages\reportlab\pdfbase\pdfdoc.py", line 536, in Reference
    raise ValueError, "redefining named object: "+repr(name)
ValueError: redefining named object: 'toUnicodeCMap:AAAAAA+FuturaBT-Light'

 
Thanks for help. :)
2013/1/23 Tim Roberts <ti...@probo.com>

Andy Robinson

unread,
Jan 24, 2013, 6:40:42 AM1/24/13
to reportlab-users
On 24 January 2013 10:12, Tom Py <mailta...@gmail.com> wrote:
> myCanvas.showPage()
> myCanvas.save()
> #myCanvas.getpdfdata()
>
> In this case, everything works fine. But when I use 'myCanvas.getpdfdata()',


Hmmm. We do certain things 'on save'. getpdfdata() isequivalent to
this, but in memory. I guess we never expected this to be executed
twice. Can you reproduce the problem by
(a) no save, but calling getpdfdata() twice?
(b) no getpdfdata, and calling save() twice?

Feel free to add this to Dinu's bug tracker. If we can reproduce it
we will fix it (but possibly next week - busy at the moment).

- Andy

Marius Gedminas

unread,
Jan 24, 2013, 6:56:15 AM1/24/13
to reportl...@lists2.reportlab.com
On Thu, Jan 24, 2013 at 11:12:37AM +0100, Tom Py wrote:
> Hi Tim,
>
> thats the code:
>
> import os
> from reportlab.pdfbase import pdfmetrics
> from reportlab.pdfbase.ttfonts import TTFont
> from reportlab.pdfgen import canvas
> from reportlab.lib.pagesizes import A4, landscape
>
> actFolder = os.getcwd() + os.sep
>
> fontFile = 'futural.ttf'
> fontName = 'FuturaBT-Light'
>
> pdfmetrics.registerFont(TTFont(fontName, actFolder + fontFile))
>
> myCanvas = canvas.Canvas("test.pdf", pagesize = landscape(A4))
> myCanvas.setFont(fontName, 10.)
> myCanvas.drawCentredString(100, 100, 'Hello World')
> myCanvas.showPage()
> myCanvas.save()
> #myCanvas.getpdfdata()
>
> In this case, everything works fine. But when I use
> 'myCanvas.getpdfdata()', I'll get this error:
>
> Traceback (most recent call last):
...
> ValueError: redefining named object: 'toUnicodeCMap:AAAAAA+FuturaBT-Light'

I can reproduce this if (and only if) I call both myCanvas.save() and
then myCanvas.getpdfdata().

I can also reproduce by calling myCanvas.save() twice (which is
pointless). Or by calling myCanvas.getpdfdata() twice.

Marius Gedminas
--
"I may not understand what I'm installing, but that's not my job. I
just need to click Next, Next, Finish here so I can walk to the next
system and repeat the process"
-- Anonymous NT Admin
signature.asc

Andy Robinson

unread,
Jan 24, 2013, 7:27:29 AM1/24/13
to reportlab-users
On 24 January 2013 11:56, Marius Gedminas <mar...@gedmin.as> wrote:
> I can also reproduce by calling myCanvas.save() twice (which is
> pointless). Or by calling myCanvas.getpdfdata() twice.

Thanks! I have logged it in Dinu's tracker - which is actually quite nice.

- Andy

Dinu Gherman

unread,
Jan 24, 2013, 8:54:51 AM1/24/13
to reportlab-users
Am 24.01.2013 um 13:27 schrieb Andy Robinson:

> Thanks! I have logged it in Dinu's tracker - which is actually quite nice.

Have you? Once I see it there, I'll mark the day as a holiday in my
calendar. ;-)

Regards,

Dinu

Andy Robinson

unread,
Jan 24, 2013, 9:21:51 AM1/24/13
to reportlab-users
On 24 January 2013 13:54, Dinu Gherman <ghe...@darwin.in-berlin.de> wrote:
> Am 24.01.2013 um 13:27 schrieb Andy Robinson:
>
>> Thanks! I have logged it in Dinu's tracker - which is actually quite nice.
>
> Have you? Once I see it there, I'll mark the day as a holiday in my
> calendar. ;-)
>

Hmm. It turns out that unless I sign up, it silently appears to
accept issues, but they don't show up.

Suggested policy: link to the thread in the email archive.

I just added it using a personal bitbucket account; we'll look into a
ReportLab one.

- Andy

Tom Py

unread,
Mar 19, 2013, 4:29:36 AM3/19/13
to reportlab-users
Hi,

regarding my question at the beginning the Font-Support works for me, but only when I print out the pdf directly (Font is originally an otf-font, convertet into afm and pfb-files).

# direct output
def createReport()
    ....
    addFonts = ['FuturaStd-Book', 'FuturaStd-Light']

    for font in addFonts:
        fontFolder = 'C:\\fonts\\'
        afmFile = os.path.join(fontFolder, font + '.afm')
        pfbFile = os.path.join(fontFolder, font + '.pfb')
        print afmFile, pfbFile
        fontFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
        fontName = font
        pdfmetrics.registerTypeFace(fontFace)
        theFont = pdfmetrics.Font(font, fontName, 'WinAnsiEncoding')
        pdfmetrics.registerFont(theFont)

    .....

    canvas.showPage()
    return canvas.save()

# create the report
pdfStream = createReport()

But I have first put the pdf into a DB. And from there I give the file to the user. It works with the reportlab standard fonts (Helvetica), but not with additional fonts (Futura in my case).

# via DB
def createReport()
    ....
    addFonts = ['FuturaStd-Book', 'FuturaStd-Light']

    for font in addFonts:
        fontFolder = 'C:\\fonts\\'
        afmFile = os.path.join(fontFolder, font + '.afm')
        pfbFile = os.path.join(fontFolder, font + '.pfb')
        print afmFile, pfbFile
        fontFace = pdfmetrics.EmbeddedType1Face(afmFile, pfbFile)
        fontName = font
        pdfmetrics.registerTypeFace(fontFace)
        theFont = pdfmetrics.Font(font, fontName, 'WinAnsiEncoding')
        pdfmetrics.registerFont(theFont)

        .....
        return canvas.getpdfdata()

# create the report
pdfStream = createReport()

# save the report in DB
saveReport(pdfStream):
        sqlStatement = """exec storedProcedure @content='%s' """ % (pdfStream.replace("'","''").replace("\r\n","\n"))
        executeNonQuery(sqlStatement)
        return

In this case the pdf opens with the error: 'Cannot extract the embedded font "FuturaStd-Book". Some characters may not display or print correctly.' (Note: this way works fine with standard fonts, but not with the addidional font.).


Any idea, why this do not work with the additional font?


Thanks for support, Tom




2013/1/24 Andy Robinson <an...@reportlab.com>
Reply all
Reply to author
Forward
0 new messages