The first thing I needed to do was to get my hands on the font files. You can find lots of open source fonts here: http://mirror.csclub.uwaterloo.ca/CTAN/fonts/. I got what I believe to be the Computer Modern fonts from here: http://mirror.csclub.uwaterloo.ca/CTAN/fonts/amsfonts.zip. This zip file contains two folders of interest:
- afm (Adobe Font Metrics)
- pfb (Printer Font Binary)
import os
from reportlab.pdfbase import pdfmetrics
afmdir = 'path to afm files'
pfbdir = 'path to pfb files'
afmfiles = os.listdir(afmdir)
pfbfiles = os.listdir(pfbdir)
for j in xrange(len(afmfiles)):
afmfiles[j] = os.path.splitext(afmfiles[j])[0]
for j in xrange(len(pfbfiles)):
pfbfiles[j] = os.path.splitext(pfbfiles[j])[0]
afmfiles = set(afmfiles)
pfbfiles = set(pfbfiles)
commonfiles = afmfiles.intersection(pfbfiles)
fontnames = []
for afmfile in commonfiles:
filename = afmdir + afmfile + '.afm'
f = open(filename, 'r')
try:
f.readline()
f.readline()
line = f.readline().rstrip()
fontnames.append(line[9:])
finally:
f.close()
for j, ffile in enumerate(commonfiles):
afmfile = afmdir+ffile+'.afm'
pfbfile = pfbdir+ffile+'.pfb'
pdfmetrics.registerTypeFace(pdfmetrics.EmbeddedType1Face(afmfile, pfbfile))
pdfmetrics.registerFont(pdfmetrics.Font(fontnames[j], fontnames[j], 'WinAnsiEncoding'))
print pdfmetrics.getRegisteredFontNames()
A couple of points to note:- To register the font you need to give it the fontname. This is found directly in the afm file (3rd line).
- I am using the 'WinAnsiEncoding' which means not all the fonts loaded will be usable.
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate, Image, Table, TableStyle, Spacer, PageBreak
from reportlab.platypus.doctemplate import _doNothing
from reportlab.lib.units import inch
import reportlab.lib.colors as colors
import defs
import locale
locale.setlocale(locale.LC_ALL, 'English_Canada.1252')
registeredFonts = defs.registerAMSfonts()
PDF_VIEWER = 'C:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe'
PDF_FILENAME = 'C:\\Users\\Joel\\Documents\\Python\\ReportLab\\rev3\\TestFonts.pdf'
elements = []
doc = BaseDocTemplate('TestFonts.pdf', pagesize=letter)
f0 = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id=None, showBoundary=0)
template = PageTemplate(id='test', frames=[f0], onPageEnd=_doNothing)
doc.addPageTemplates([template])
val1 = locale.format('%9.2f', -15197114.48, grouping=True)
val2 = locale.format('%9.2f', -16790937.16, grouping=True)
data = [['Index', 'Value 1', 'Value 2', 'FontName']]
for j in range(len(registeredFonts)):
data.append(['%d'%j, val1, val2, registeredFonts[j]])
tablestyle = TableStyle([
('ALIGNMENT',(0,0),(-1,-1), 'RIGHT'),
('FONTSIZE', (0,0),(-1,-1), 10),
('VALIGN', (0,0),(-1,-1), 'BOTTOM'),
('INNERGRID',(0,0),(-1,-1), 0.25, colors.black),
('BOX', (0,0),(-1,-1), 0.5, colors.black)])
for j in xrange(len(registeredFonts)):
tablestyle.add('FONTNAME',(0,j),(3,j),registeredFonts[j])
table1 = Table(data,
colWidths=doc.width/4.0,
rowHeights=18,
style=tablestyle)
elements.append(table1)
elements.append(PageBreak())
doc.build(elements)
import subprocess
process = subprocess.Popen([PDF_VIEWER, '/A', 'view=FitH', PDF_FILENAME], shell=False, stdout=subprocess.PIPE)
process.wait()
I am using the "subprocess" command to have Python automatically launch Adobe's Acrobat Reader to view my newly created document. The results look something like this:

No comments:
Post a Comment