【问题标题】:Adding a font for use in ReportLab添加用于 ReportLab 的字体
【发布时间】:2011-02-09 00:19:07
【问题描述】:

我正在尝试向 python ReportLab 添加一种字体,以便我可以将它用于功能。该功能是使用canvas.Canvas在PDF中绘制一堆文本,没什么复杂的,但是我需要为布局问题添加一个固定宽度的字体。

当我尝试使用我能找到的少量信息注册字体时,这似乎奏效了。但是当我试图从我的 Canvas 对象中调用 .addFont('fontname') 时,我不断得到 ​​p>

"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' 只是我随便乱放的一种字体。

【问题讨论】:

    标签: python fonts reportlab


    【解决方案1】:
    c.setFont('TestFont')
    c.drawString(1,1,'test data here')
    

    setFont 设置您要使用的字体名称,drawString

    如果您在文档中使用该字体,ReportLab 会自动嵌入该字体,您在一个名称下全局注册该字体后无需手动添加。

    【讨论】:

    • 你的意思是c.setFont('TestFont') 在第一行吗?
    猜你喜欢
    • 2017-01-14
    • 2014-05-22
    • 2020-01-24
    • 2012-12-31
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多