7

I have been using reportlab pdfgen to create dynamic pdf documents for printing. It has been working very well for a number of years.

We are having a fund raising event coming up, and wish to generate pdf receipts with the 'theme' font we are using (specifically talldeco.ttf).

I have set the font no problem using the following:

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

Now comes the issue: some of the text needs to be bold and italics, and the talldeco just comes with 1 file (unlike some of the other fonts). I can bold and italicize text in this font in openoffice.

Per the reportlab users guide (http://www.reportlab.com/software/opensource/rl-toolkit/guide/) page 53, it should be possible and they show some code and the results, but our software is using drawString calls instead of paragraphs. A test app based on the sample noted above:

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

Just gives a Key Error on 'TallDeco-Bold'.

Any suggestions?

2 Answers 2

7

TTFont has a subfontIndex parameter.

The following works for me (using reportlab 3.0 on OS X):

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
  • Even though it doesn't solve the original question, this method worked for me when using Consolas which does include the style variations as separate files.
    – Todd
    Sep 1, 2014 at 3:29
  • It looks to me that this does not answer the Q that the OP had. His Q has to do with a single TTF file. The answer given here makes explicit reference to a TTC file.
    – GaryMBloom
    Nov 26, 2019 at 17:22
-1

The bold, italic and boldItalic fonts need to be defined.

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

But because they all point to the same ttfFile the output will all look like the default TallDeco i.e. no bold or italic

1
  • This isn't any solution and depend on ttf file it might even end with errors like redefining named object: 'toUnicodeCMap:AAAAAA+Font'
    – DimmuR
    Sep 27, 2016 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.