如何将Fontawesome与Reportlab一起使用? 我有一些错误,没有找到任何解决方案
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
答案 0 :(得分:1)
答案 1 :(得分:1)
我希望您找到问题的答案或以不同的方式解决问题。但我认为这个答案对其他人有用。 你做的几乎一切正确。但FontAwesome是一种标志性的字体。有了它,您无法打印文本。要使用FontAwesome图标,您必须编写Unicode代码。 这是一个示例代码。
# !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代码或on this table。