2013-01-17 293 views
2

我一直在使用reportlab pdfgen创建用于打印的动态PDF文档。多年来它一直工作得很好。Reportlab pdfgen支持粗体truetype字体

我们正在筹集资金,希望通过我们正在使用的“主题”字体(特别是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... 

既然说到了问题:一些文本必须是粗体和斜体,和talldeco只是配备了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... 

只是在'TallDeco-Bold'上给出了一个关键错误。

有什么建议吗?

回答

-2

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

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

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

+0

这不能解决任何问题 – Alvaro

+0

这不是任何解决方案,取决于TTF文件甚至可能与类似的错误结束'重新定义命名对象:'toUnicodeCMap:AAAAAA + Font'' – DimmuR

3

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") 
+0

尽管它不能解决原始问题,但是当使用Consolas时,此方法适用于我,它包含作为单独文件的样式变体。 – Todd