logo
Published on john.parnefjord.se (http://john.parnefjord.se)

Fixing font error in renderpm

By johnp
Created 2009-01-16 08:37

If you are planning to generate PDF:s programatically in Python you have most likely ran into ReportLab. This is an amazing toolkit to create PDF:s. ReporLab has an addon called renderPM that lets you generate images as well in different formats. When I tried to make a PNG-file I ran into an error with some fonts missing. The code I tried to run was just a simple snippet:

#!/usr/bin/python

from reportlab.lib import colors
from reportlab.graphics.shapes import *
d = Drawing(400, 200)

d.add(Rect(50, 50, 300, 100, fillColor=colors.yellow))
d.add(String(150,100, 'Hello World', fontSize=18, fillColor=colors.red))

from reportlab.graphics import renderPM
renderPM.drawToFile(d, 'example.png', 'PNG')

Running this threw the following the error:

C:\Sourcecode\code-library\trunk\python>barcode-png.py
Warn: Can't find .pfb for face 'Times-Roman'
Traceback (most recent call last):
  File "C:\Sourcecode\code-library\trunk\python\barcode-png.py", line 11, in <module>
    renderPM.drawToFile(d, 'example1.png', 'PNG')
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 634, in drawToFile
    c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL, showBoundary=showBoundary)
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 620, in drawToPMCanvas
    draw(d, c, 0, 0, showBoundary=showBoundary)
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 49, in draw
    R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderbase.py", line 200, in draw
    self.initState(x,y)  #this is the push()
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 86, in initState
    self.applyState()
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 80, in applyState
    self._canvas.setFont(s['fontName'], s['fontSize'])
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 359, in setFont
    _setFont(self._gs,fontName,fontSize)
  File "C:\SDK\Python25\lib\site-packages\reportlab\graphics\renderPM.py", line 212, in _setFont
    raise RenderPMError, "Can't setFont(%s) missing the T1 files?\nOriginally %s: %s" % (fontName,s1,s2)
reportlab.graphics.renderPM.RenderPMError: Can't setFont(Times-Roman) missing the T1 files?
Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be string, not None 

The solution is to download the fonts from the reportlab site. I downloaded both these files:

http://www.reportlab.org/ftp/pfbfer.zip [1]
http://www.reportlab.org/ftp/fonts/pfbfer.zip [2]

Now, unzip the files to the fonts directory in reportlab. In my case I have installed Python on my Windows-box under C:\SDK\Python25. That would mean that the fonts should be unzipped to:

C:\SDK\Python25\Lib\site-packages\reportlab\fonts

 

 


Source URL:
http://john.parnefjord.se/node/51