添加在 ReportLab 中使用的字体
我正在尝试向 python ReportLab 添加字体,以便我可以将其用于函数。该功能是使用canvas.Canvas在PDF中绘制一堆文本,没什么复杂的,但是我需要添加固定宽度的字体来解决布局问题。
当我尝试使用我能找到的少量信息来注册字体时,这似乎有效。但是,当我尝试从 Canvas 对象调用 .addFont('fontname') 时,我不断收到
“PDFDocument 实例没有属性 'addFont'”
该功能是否未实现?如何访问 .getAvailableFonts 中列出的 10 种左右默认字体以外的字体?谢谢。
我想要实现的一些示例代码:
from reportlab.pdfgen import canvas
c = canvas.Canvas('label.pdf')
c.addFont('TestFont') #This throws the error listed above,
#regardless of what argument I use (whether it refers
#to a font or not).
c.drawString(1,1,'test data here')
c.showPage()
c.save()
为了注册字体,我尝试了
from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics
pdfmetrics.registerFont(TTFont('TestFont', 'ghettomarquee.ttf'))
addMapping('TestFont', 0, 0, 'TestFont')
“ghettomarquee.ttf”只是我周围的随机字体。
I'm trying to add a font to the python ReportLab so that I can use it for a function. The function is using canvas.Canvas to draw a bunch of text in a PDF, nothing complicated, but I need to add a fixed width font for layout issues.
When I tried to register a font using what little info I could find, that seemed to work. But when I tried to call .addFont('fontname') from my Canvas object I keep getting
"PDFDocument instance has no attribute 'addFont'"
Is the function just not implemented? How do I get access to fonts other than the 10 or so default ones that are listed in .getAvailableFonts? Thanks.
Some example code of what I'm trying to make happen:
from reportlab.pdfgen import canvas
c = canvas.Canvas('label.pdf')
c.addFont('TestFont') #This throws the error listed above,
#regardless of what argument I use (whether it refers
#to a font or not).
c.drawString(1,1,'test data here')
c.showPage()
c.save()
To register the font, I tried
from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics
pdfmetrics.registerFont(TTFont('TestFont', 'ghettomarquee.ttf'))
addMapping('TestFont', 0, 0, 'TestFont')
where 'ghettomarquee.ttf' was just a random font I had lying around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setFont
设置您要使用的字体名称,以及drawString
。如果您在文档中使用该字体,ReportLab 将自动嵌入该字体,在您以名称全局注册该字体后,无需手动添加它。
setFont
to set the font name you're going to use, anddrawString
.ReportLab will automatically embed the font if you use it in the document, you don't have to manually add it after you've registered the font globally under a name.