[reportlab-users] Font Helvetica always used?

EasyClassPage easyclasspage at googlemail.com
Sat Jun 20 13:33:18 EDT 2015


Hi Robin,

thanks for finding and providing the workaround. This does the job for me:

...
# additional fonts
# ----------------
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont
registerFont(TTFont('DejaVuSans', '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'))
registerFont(TTFont('DejaVuSans-Bold', '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'))
registerFont(TTFont('DejaVuSans-Oblique', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Oblique.ttf'))
registerFont(TTFont('DejaVuSans-BoldOblique', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-BoldOblique.ttf'))

# workaround to supress unused default helvetica font (header)
# ------------------------------------------------------------
from reportlab import rl_config
rl_config._SAVED['canvas_basefontname'] = 'DejaVuSans'
rl_config._startUp()

# pdfgen modules
# --------------
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
...

Klaus


Am 17.06.2015 um 14:09 schrieb Robin Becker <robin at reportlab.com>:

> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20150620/998782a0/attachment.html>


More information about the reportlab-users mailing list