如何在python的reportlab Canvas中设置任何字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4899885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set any font in reportlab Canvas in python?
提问by srisar
I'm using reportlab to create pdfs. When I try to set a font using the following method, I get a KeyError:
我正在使用 reportlab 创建 pdf。当我尝试使用以下方法设置字体时,我得到一个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?
但是如果我使用'Courier'而不是'Tahoma'没有问题。我如何使用塔霍马?
采纳答案by Reiner Gerecke
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:
Perhabs Tahoma 是一种 TrueType 字体,您需要先注册它。根据 ReportLab 的用户指南,您需要这样做:
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 getAvailableFontsmethod that should return all currently registered (and therefore usable) fonts.
canvas 对象有一个getAvailableFonts方法应该返回所有当前注册的(因此可用的)字体。
回答by Dark Matter
By adding DejaVuSans Font to application solved my problem. Here is the snippet of code
通过向应用程序添加 DejaVuSans 字体解决了我的问题。这是代码片段
pdfmetrics.registerFont(TTFont('DejaVuSans','DejaVuSans.ttf'))
And use UTF8 for all coding.:)
并使用 UTF8 进行所有编码。:)
回答by Pranay Majmundar
Start at Reiner's answer.
从Reiner 的回答开始。
It is perfect with one caveat.
这是完美的一个警告。
Reportlab only searches for fonts in predefined folders:
Reportlab 只搜索预定义文件夹中的字体:
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:
如果您尝试使用从 Internet 下载的 ttf 字体,并希望该字体在您的所有服务器上可用,我建议如下:
- 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'))
- 将字体添加到任何目录中的项目中。例如:/project_root/app/lib/reportlabs/fonts/
确保您的设置中有类似 BASE_DIR/ROOT_DIR 的内容:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))将以下行添加到生成 pdf 的 python 文件中:
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'))

