OTF fonts supported by Reportlab?

Mike Driscoll <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <CAO4gEAsYCeL3u+2ZG0=efhZtsqJs6ouSKwD-ytjFTHcyOv5HSw@mail.gmail.com>
Hi,

After writing a tutorial on fonts in Reportlab, I got a comment asking if
OTF fonts are supported. So far, I've found conflicting information.

This old thread seems to indicate that it should work:
http://two.pairlist.net/pipermail/reportlab-users/2008-August/007120.html

While this one says that if the font uses CFF format, Reportlab will spit
out an error:

http://stackoverflow.com/questions/895596/can-anyone-recomend-a-python-pdf-generator-with-opentype-otf-support

So I tried it. I went to http://www.fontsquirrel.com/ and downloaded the
first OTF font on there, specifically "Source Sans Pro Black". Then I ran
the attached code which gave me the following traceback:

reportlab.pdfbase.ttfonts.TTFError: TTF file
"C:\Users\mdriscoll\Desktop\rep-fonts\otf\SourceSansPro-Black.otf":
postscript outlines are not supported
File "c:\Users\mdriscoll\Desktop\rep-fonts\otfDemo.py", line 29, in <module>
  embedOTF(r"C:\Users\mdriscoll\Desktop\rep-fonts\otf.pdf")
File "c:\Users\mdriscoll\Desktop\rep-fonts\otfDemo.py", line 17, in embedOTF
  pdfmetrics.registerFont(TTFont("Source Sans Pro Black", otf_font))
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\ttfonts.py", line
1009, in __init__
  self.face = TTFontFace(filename, validate=validate,
subfontIndex=subfontIndex)
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\ttfonts.py", line
915, in __init__
  TTFontFile.__init__(self, filename, validate=validate,
subfontIndex=subfontIndex)
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\ttfonts.py", line
412, in __init__
  TTFontParser.__init__(self, file,
validate=validate,subfontIndex=subfontIndex)
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\ttfonts.py", line
190, in __init__
  isCollection = self.readHeader()
File "C:\Python26\Lib\site-packages\reportlab\pdfbase\ttfonts.py", line
253, in readHeader
  raise TTFError('%s file "%s": postscript outlines are not
supported'%(self.fileKind,self.filename))

Is there a way to make Reportlab accept this font or fonts like it?

Thanks,
Mike Driscoll

_______________________________________________
reportlab-users mailing list
[email protected]
http://two.pairlist.net/mailman/listinfo/reportlab-users
otfDemo.py (application/octet-stream, 1 KB)
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph
from reportlab.lib.units import mm

#----------------------------------------------------------------------
def embedOTF(path):
    """
    """
    style = getSampleStyleSheet()
    width, height = letter
    c = canvas.Canvas(path, pagesize=letter)
    otf_font = r"C:\Users\mdriscoll\Desktop\rep-fonts\otf\SourceSansPro-Black.otf"
    pdfmetrics.registerFont(TTFont("Source Sans Pro Black", otf_font))
    
    otf_string = '<font name="Source Sans Pro Black" size="16">%s</font>'
    otf_string = otf_string % "The quick brown fox jumps over the lazy..."
    
    p = Paragraph(otf_string, style=style["Normal"])
    p.wrapOn(c, width, height)
    p.drawOn(c, 20, 750, mm)
    
    c.save()
    
if __name__ == "__main__":
    embedOTF(r"C:\Users\mdriscoll\Desktop\rep-fonts\otf.pdf")
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.