Reputation: 6334
We have a Django application, and we utilize HTML
to pdf
generation tool to build pdf documents. We have run into problems with fonts not existing on the server converting HTML to pdfs, and I want to add a unit test that can verify if a font exists on the hosting server.
From what I have learned, I should be using Tkinter's tkFont
module to get available fonts list, and confirm the fonts we are using are found within this list.
class VerifyFontsExistOnServer(BaseTransactionTestCase):
def test_if_font_exists(self):
import Tkinter
import tkFont
Tkinter.Tk()
installed_font_families =[i for i in tkFont.families()
if 'Helvetica' in i
or 'Courier' in i
or 'DejaVuSerif' in i
or 'OCRA' in i]
for font in installed_font_families:
log.info('{0}'.format(font))
But when I list the items out, I get Helvetica
as a font, but not Helvetica-Light. I believe this is part of this family, but is there a way to identify if this particular font style of the family exists?
Upvotes: 2
Views: 1230
Reputation: 6334
I ended up writing a shell based method to call the fc-list terminal command:
def verify_fonts_are_installed_for_statements():
import subprocess
from os import path
potential_locations = [
'/usr/bin/fc-list',
'/usr/sbin/fc-list',
'/usr/local/sbin/fc-list',
'/usr/local/bin/fc-list',
]
valid_path = None
for file_path in potential_locations:
if path.exists(file_path):
valid_path = file_path
break
if valid_path is None:
raise IOError('could not find fc-list to verify fonts exist.')
cmd = [valid_path]
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
if 'Helvetica Neue' not in output:
raise FontNotInstalledException('Helvetica Neue')
if 'Courier' not in output:
raise FontNotInstalledException('Courier')
log.debug('Courier and Helvetica Neue were found to be installed.')
Upvotes: 1