如何在python的reportlab Canvas中设置任何字体? [英] How to set any font in reportlab Canvas in python?

查看:60
本文介绍了如何在python的reportlab Canvas中设置任何字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 reportlab 创建 pdf.当我尝试使用以下方法设置字体时,出现 KeyError:

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)

但是如果我使用 'Courier' 而不是 'Tahoma' 就没有问题.我如何使用 Tahoma?

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

推荐答案

Perhabs Tahoma 是一种 TrueType 字体,您需要先注册它.根据 ReportLab 的用户指南,您需要这样做:

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!")

canvas 对象有一个 getAvailableFonts 方法,该方法应该返回所有当前注册(因此可用)的字体.

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

这篇关于如何在python的reportlab Canvas中设置任何字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆