[reportlab-users] Setting TrueType fonts in table cell

Nils Smeds nils.smeds at gmail.com
Wed Sep 25 17:36:55 EDT 2013


Nils Smeds <nils.smeds <at> gmail.com> writes:

Here is a simple test case:
Modify the base_style array as indicated to expose the problem I see.

/Nils

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import MySQLdb

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4, inch,cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak
from PIL import Image
from datetime import datetime

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4, inch, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak


from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFontFamily

# My Liberation fonts are installed in: /usr/share/fonts/liberation
pdfmetrics.registerFont(TTFont('SansR',
'/usr/share/fonts/liberation/LiberationSans-Regular.ttf'))
pdfmetrics.registerFont(TTFont('SansB',
'/usr/share/fonts/liberation/LiberationSans-Bold.ttf'))
pdfmetrics.registerFont(TTFont('SansI',
'/usr/share/fonts/liberation/LiberationSans-Italic.ttf'))
pdfmetrics.registerFont(TTFont('SansBI',
'/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf'))
registerFontFamily('Sans',normal='SansR',bold='SansB',italic='SansI',boldIta
lic='SansBI')

filename='mytest.pdf'

page_width, page_height = A4
PDFdoc = SimpleDocTemplate(filename,pagesize=A4)
PDFdoc_elements=[]



from reportlab.lib.colors import black
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Table,TableStyle
from reportlab.platypus import Paragraph, SimpleDocTemplate, PageBreak

styles = getSampleStyleSheet()
styleH = styles['Heading1']
styleN = styles['Normal']

base_style=[('GRID', (0,0), (-1,-1), 1, (0,0,0)),
('FONT', (1,0), (1,-1), 'SansR'),
# ('FONT', (1,0), (1,-1), 'Courier'),
('BOX', (0,0), (-1,-1), 3, (0,0,0)),
('ALIGN',(1,0),(1,-1),'RIGHT'),
('ALIGN',(3,0),(3,-1),'RIGHT'),
('VALIGN',(0,0),(-1,-1),'TOP')]

data=[('ČOL1','ČOL2','ČOL3','COL4','COL5')]
data.append(['ČOL1','ČOL2','ČOL3','COL4','COL5'])
data.append(['ČOL1','ČOL2','ČOL3','COL4','COL5'])
data.append(['ČOL1','ČOL2','ČOL3','COL4','COL5'])
t=Table(data)
styles=list(base_style)
t.setStyle(TableStyle(styles))
PDFdoc_elements.append(t)
PDFdoc.build(PDFdoc_elements)

PDFcanvas=PDFdoc.canv

PDFcanvas.setStrokeColorRGB(132.0/255,0.0/255,50.0/255)
PDFcanvas.setFont("Courier", 20)
PDFcanvas.drawString(5*cm, page_height - 8*cm, 'Č TALLY HOOO!!! č')
PDFcanvas.setFont("SansB", 20)
PDFcanvas.drawString(5*cm, page_height - 10*cm, 'Č TALLY HOOO!!! č')

PDFcanvas.showPage()
PDFcanvas.save()




More information about the reportlab-users mailing list