#!/usr/bin/python # -*- coding: utf-8 -*- """Hello-world for TTF font loading in Reportlab. This does seem to work, embedding the fonts (or perhaps the relevant parts of them) into the output file, and smoothly handling layout that switches fonts and even typeface sizes, and producing a funny page size. The output PDF file is only 53K, even though it includes text in both fonts, which are 71K and 74K respectively in TTF format; they don't even gzip that small. userguide/ch2a_fonts.py does document the font support as embedding subsets of fonts in the PDFs. I don't think it's embedding the Chinese font, but rather using the system font (if you have it). """ import os from reportlab.pdfgen.canvas import Canvas from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase.cidfonts import UnicodeCIDFont from reportlab import rl_config def main(): rl_config.warnOnMissingFontGlyphs = 1 # XXX not working my = lambda f: os.path.join(os.path.dirname(__file__), f) roman = my('etbook/et-book-roman-old-style-figures.ttf') rname = 'et-book-roman' pdfmetrics.registerFont(TTFont(rname, roman)) italic = my('etbook/et-book-display-italic-old-style-figures.ttf') itname = 'et-book-italic' pdfmetrics.registerFont(TTFont(itname, italic)) ch = 'STSong-Light' pdfmetrics.registerFont(UnicodeCIDFont(ch)) c = Canvas('hellofont.pdf', invariant=True, pagesize=(72*3, 72*2)) fontsize = 18 t = c.beginText(12, 63) t.setFont(rname, fontsize) t.textOut("No, fuck ") t.setFont(itname, fontsize * 1.5) t.textOut("your") t.setFont(rname, fontsize) t.textOut(" ") t.setFont(ch, fontsize) t.textOut("妈") #t.setFont(rname, fontsize) # uncomment for correctness; STSong has Roman t.textOut(" mother.") c.drawText(t) c.save() if __name__ == '__main__': main()