首页 文章

Reportlab pdfgen支持粗体truetype字体

提问于
浏览
2

我一直在使用reportlab pdfgen来创建用于打印的动态pdf文档 . 它已经很好地工作了很多年 .

我们正在筹集资金,并希望使用我们正在使用的“主题”字体生成pdf收据(特别是talldeco.ttf) .

我使用以下方法设置字体没问题:

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...

现在问题出现了:一些文本需要粗体和斜体,而talldeco只有一个文件(不像其他一些字体) . 我可以在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...

只是给出'TallDeco-Bold'的关键错误 .

有什么建议?

2 回答

  • 4

    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")
    
  • -2

    需要定义粗体,斜体和粗体字体 .

    pdfmetrics.registerFont(TTFont("TallDeco-Bold", ttfFile))
    pdfmetrics.registerFont(TTFont("TallDeco-Italic", ttfFile))
    pdfmetrics.registerFont(TTFont("TallDeco-BoldItalic", ttfFile))
    

    但因为它们都指向相同的 ttfFile 输出将看起来像默认的TallDeco,即没有粗体或斜体

相关问题