2013-01-17 287 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