Reputation: 404
How to use Fontawesome with Reportlab? I have some error and don't find any solution
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
pdffile = 'test.pdf'
ttffile = '/somepath/fonts/fontawesome-webfont.ttf'
c = canvas.Canvas(pdffile)
pdfmetrics.registerFont(TTFont('fontawesome', ttffile))
c.setFont('fontawesome', 32)
c.drawString(10, 150, "test")
c.showPage()
c.save()
.
Traceback (most recent call last):
File "/somepath/test.py", line 9, in <module>
pdfmetrics.registerFont(TTFont('fontawesome', ttffile))
File "/usr/local/lib/python3.3/dist-packages/reportlab/pdfbase/ttfonts.py", line 989, in __init__
self.face = TTFontFace(filename, validate=validate, subfontIndex=subfontIndex)
File "/usr/local/lib/python3.3/dist-packages/reportlab/pdfbase/ttfonts.py", line 895, in __init__
TTFontFile.__init__(self, filename, validate=validate, subfontIndex=subfontIndex)
File "/usr/local/lib/python3.3/dist-packages/reportlab/pdfbase/ttfonts.py", line 394, in __init__
self.extractInfo(charInfo)
File "/usr/local/lib/python3.3/dist-packages/reportlab/pdfbase/ttfonts.py", line 469, in extractInfo
psName = names[6].replace(b" ", b"-") #Dinu Gherman's fix for font names with spaces
TypeError: Can't convert 'bytes' object to str implicitly
Reportlab = 3.0
Python = 3.3.2
Upvotes: 1
Views: 1737
Reputation: 199
I hope that you have found the answer to your question or solved the problem differently. But I think that this answer can be useful to others. You did almost everything right. But FontAwesome is an iconic font. With it, you can not print text. In order to use FontAwesome icons, you must write the Unicode code. Here is an example code.
# !usr/bin/python
# -*- coding: utf-8 -*-
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas
pdfmetrics.registerFont(
TTFont("FontAwesome", "fontawesome-webfont.ttf"))
canvas = Canvas("pdffile.pdf", pagesize=letter)
canvas.setFont("FontAwesome", 30)
icon = u"\uf1e3" # fa-futbol-o
canvas.drawString(100,100,icon)
canvas.save()
Unicode code is specified on the page of the desired icon or on this table.
Upvotes: 1
Reputation: 404
Do not use fontawesome-webfont.ttf
from site. Convert FontAwesome.otf
to .ttf
using everythingfonts.com
Upvotes: 2