Dan*_*Dan 5 django pdf-generation reportlab truetype
我一直在使用reportlab pdfgen 来创建用于打印的动态pdf 文档。多年来,它一直运行良好。
我们即将举行筹款活动,并希望使用我们正在使用的“主题”字体(特别是 talldeco.ttf)生成 pdf 收据。
我使用以下方法设置字体没问题:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
ttfFile = "/usr/share/fonts/truetype/ttf-tall-deco/TALLDECO.TTF"
pdfmetrics.registerFont(TTFont("TallDeco", ttfFile))
p.setFont("TallDeco", 18) # Was Times-Bold...
Run Code Online (Sandbox Code Playgroud)
现在问题来了:一些文本需要加粗和斜体,而高装饰只带有 1 个文件(与其他一些字体不同)。我可以在 openoffice 中将此字体的文本加粗和斜体。
根据 reportlab 用户指南 (http://www.reportlab.com/software/opensource/rl-toolkit/guide/) 第 53 页,这应该是可能的,它们显示了一些代码和结果,但我们的软件正在使用 drawString 调用而不是段落。基于上述示例的测试应用程序:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFontFamily
ttfFile = "/usr/share/fonts/truetype/ttf-tall-deco/TALLDECO.TTF"
pdfmetrics.registerFont(TTFont("TallDeco", ttfFile))
registerFontFamily('TallDeco',normal='TallDeco',bold='TallDeco-Bold',italic='TallDeco-Italic',boldItalic='TallDeco-BoldItalic')
p.setFont("TallDeco-Bold", 18) # Was Times-Bold...
Run Code Online (Sandbox Code Playgroud)
只是在“TallDeco-Bold”上给出一个关键错误。
有什么建议?
小智 7
TTFont 有一个subfontIndex参数。
以下对我有用(在 OS X 上使用 reportlab 3.0):
menlo_path = "/System/Library/Fonts/Menlo.ttc"
pdfmetrics.registerFont(ttfonts.TTFont("Menlo", menlo_path,
subfontIndex=0))
pdfmetrics.registerFont(ttfonts.TTFont("Menlo-Bold", menlo_path,
subfontIndex=1))
pdfmetrics.registerFont(ttfonts.TTFont("Menlo-Italic", menlo_path,
subfontIndex=2))
pdfmetrics.registerFont(ttfonts.TTFont("Menlo-BoldItalic", menlo_path,
subfontIndex=3))
pdfmetrics.registerFontFamily("Menlo", normal="Menlo", bold="Menlo-Bold",
italic="Menlo-Italic",
boldItalic="Menlo-boldItalic")
Run Code Online (Sandbox Code Playgroud)
Joa*_*oao -1
需要定义粗体、斜体和boldItalic 字体。
pdfmetrics.registerFont(TTFont("TallDeco-Bold", ttfFile))
pdfmetrics.registerFont(TTFont("TallDeco-Italic", ttfFile))
pdfmetrics.registerFont(TTFont("TallDeco-BoldItalic", ttfFile))
Run Code Online (Sandbox Code Playgroud)
但因为它们都指向同一个ttfFile,所以输出看起来都像默认的 TallDeco,即没有粗体或斜体
| 归档时间: |
|
| 查看次数: |
3468 次 |
| 最近记录: |