Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set any font in reportlab Canvas in python?

I'm using reportlab to create pdfs. When I try to set a font using the following method, I get a KeyError:

pdf = Canvas('test.pdf') pdf.setFont('Tahoma', 16) 

But if I use 'Courier' instead of 'Tahoma' there isn't a problem. How can I use Tahoma?

like image 601
srisar Avatar asked Feb 04 '11 15:02

srisar


People also ask

Is ReportLab free?

ReportLab is a free open-source document creation engine for generating PDF documents and custom vector graphics.


2 Answers

Perhabs Tahoma is a TrueType font, and you need to register it first. According to the user guide of ReportLab you need to do this:

from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont  pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf')) pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf')) pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf')) pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))  canvas.setFont('Vera', 32) canvas.drawString(10, 150, "Some text encoded in UTF-8") canvas.drawString(10, 100, "In the Vera TT Font!") 

The canvas object has a getAvailableFonts method that should return all currently registered (and therefore usable) fonts.

like image 125
Reiner Gerecke Avatar answered Sep 22 '22 20:09

Reiner Gerecke


Start at Reiner's answer.

It is perfect with one caveat.

Reportlab only searches for fonts in predefined folders:

TTFSearchPath = (             'c:/winnt/fonts',             'c:/windows/fonts',             '/usr/lib/X11/fonts/TrueType/',             '/usr/share/fonts/truetype',             '/usr/share/fonts',             #Linux, Fedora             '/usr/share/fonts/dejavu',      #Linux, Fedora             '%(REPORTLAB_DIR)s/fonts',      #special             '%(REPORTLAB_DIR)s/../fonts',   #special             '%(REPORTLAB_DIR)s/../../fonts',#special             '%(CWD)s/fonts',                #special             '~/fonts',             '~/.fonts',             '%(XDG_DATA_HOME)s/fonts',             '~/.local/share/fonts',             #mac os X - from             #http://developer.apple.com/technotes/tn/tn2024.html             '~/Library/Fonts',             '/Library/Fonts',             '/Network/Library/Fonts',             '/System/Library/Fonts',             ) 

If you're trying to use a ttf font that you've downloaded off of the internet, and would like that font available on all your servers, I would suggest the following:

  • Add the font to your project in any directory. e.g.: /project_root/app/lib/reportlabs/fonts/
  • Make sure you have something like BASE_DIR/ROOT_DIR in your settings:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
  • add the following line to a python file that generates pdf:

    import reportlab from django.conf import settings reportlab.rl_config.TTFSearchPath.append(str(settings.BASE_DIR) + '/app/lib/reportlabs/fonts') pdfmetrics.registerFont(TTFont('Copperplate', 'Copperplate-Gothic-Bold.ttf')) 
like image 38
Pranay Majmundar Avatar answered Sep 21 '22 20:09

Pranay Majmundar