[reportlab-users] Font Helvetica always used?

Robin Becker robin at reportlab.com
Wed Jun 17 08:09:46 EDT 2015


On 14/06/2015 08:50, EasyClassPage via reportlab-users wrote:
> Hi all,
>
> I'm using version 3.1 of the python library and have a question concerning the embedded fonts. I only use external (and free) fonts (eg. DejaVu) for my documents. These fonts are correctly embedded in the final document. But in the font list there is also a font type "Helvetica". I do not use such a font. Is this something internal? How to rid of this font?
>
> Regards Klaus
.....
Hi Klaus,

it seems there is no obvious way to ensure that Helvetica is not referenced, but 
there is a tricky way.

Let's assume you want to use 'Arial' as your embedded font.

1) find a place to create a settings file. This can be

    i: reportlab/rl_local_settings.py
   ii: reportlab_settings.py  somewhere on the PYTHONPATH.
  iii: ~/.reportlab_settings.py for unix like systems.

2) put a module level definition in the file

canvas_basefontname = 'arial'

3) modify your startup script to add the following right at the top of module 
level execution ie before importing any other bits of reportlab

from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont
registerFont(TTFont('arial','Arial.ttf'))

the statement in 2 declares the name of the font to be used by default. The 
above statements ensure that the name is a registered font name.

Then run your script eg


from reportlab.pdfgen.canvas import Canvas
canv=Canvas('thello.pdf')
canv.drawString(72,72,'Hello World')
canv.save()


at canvas instantiation the default fontname is now 'arial' and not Helvetica. 
The Helvetica font is not used and we won't see it in the output PDF.

It seems we used to inject the default fonts automatically, but now it seems as 
though they are referenced if used and it seems we have to have an initial font.


If you don't want to mess with the settings overrides then this code at the 
start of your initial script should do the trick

from reportlab import rl_config
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont
registerFont(TTFont('arial','Arial.ttf'))
rl_config._SAVED['canvas_basefontname'] = 'arial'
rl_config._startUp()
-- 
Robin Becker


More information about the reportlab-users mailing list