copy fonts to a dir scanned by fontconfig instead of manually injecting configs

workspace
Brendan Howell 5 years ago
parent 1f278f811a
commit 667a9bfba9

@ -7,6 +7,7 @@ import json
import logging import logging
import os.path import os.path
import random import random
import shutil
import string import string
import subprocess import subprocess
import tempfile import tempfile
@ -166,7 +167,6 @@ class Bureau(object):
self.commands = {} self.commands = {}
self.api = {} self.api = {}
modpath = os.path.dirname(__file__)
mypath = inspect.getfile(self.__class__) mypath = inspect.getfile(self.__class__)
self.mdir = os.path.dirname(mypath) self.mdir = os.path.dirname(mypath)
@ -175,6 +175,17 @@ class Bureau(object):
os.mkdir(basepath) os.mkdir(basepath)
os.chdir(basepath) os.chdir(basepath)
# make sure fonts are copied
fontpath = os.path.join(basepath, "fonts")
fontsrc = os.path.join(self.mdir, "fonts")
if os.path.exists(fontsrc):
fontlist = glob.glob(os.path.join(fontsrc, "*.otf"))
fontlist += glob.glob(os.path.join(fontsrc, "*.ttf"))
for fontfile in fontlist:
fontbase = os.path.basename(fontfile)
if not os.path.exists(os.path.join(fontpath, fontbase)):
shutil.copy(fontfile, fontpath)
self.load_config() self.load_config()
# setup log file # setup log file
@ -355,20 +366,7 @@ class Bureau(object):
pdfpath = tempfile.mkstemp(".pdf")[1] pdfpath = tempfile.mkstemp(".pdf")[1]
self.log.debug("rendering to: " + pdfpath) self.log.debug("rendering to: " + pdfpath)
# manually add local fonts weasyprint.HTML(string=templ.render_unicode(**kwargs)).write_pdf(pdfpath)
fontconfig = FontConfiguration()
fontsdir = os.path.join(self.mdir, "fonts")
fontlist = []
for font in glob.glob(fontsdir + "/*.otf"):
fontlist.append(("local", "file://" + font))
self.log.debug("adding font " + font)
for font in glob.glob(fontsdir + "/*.ttf"):
fontlist.append(("local", "file://" + font))
self.log.debug("adding font " + font)
fdict = { "src": fontlist }
fontconfig.add_font_face(fdict,
weasyprint.default_url_fetcher)
weasyprint.HTML(string=templ.render_unicode(**kwargs)).write_pdf(pdfpath, font_config=fontconfig)
subprocess.call("lpr -o sides=two-sided-long-edge -o InputSlot=Upper " + pdfpath, shell=True) subprocess.call("lpr -o sides=two-sided-long-edge -o InputSlot=Upper " + pdfpath, shell=True)
# TODO: make this asynchronous # TODO: make this asynchronous

@ -23,6 +23,9 @@ class Management:
if not os.path.exists(basepath): if not os.path.exists(basepath):
os.mkdir(basepath) os.mkdir(basepath)
os.chdir(basepath) os.chdir(basepath)
fontpath = os.path.join(basepath, "fonts")
if not os.path.exists(fontpath):
os.mkdir(fontpath)
self.procs = {} self.procs = {}
self.org_chart = [] self.org_chart = []

Loading…
Cancel
Save