switch from slimer.js and firefox to weasyprint for rendering html to pdf

workspace
Brendan Howell 6 years ago
parent c6fed3bbf1
commit 844677d1a9

@ -14,6 +14,7 @@ import threading
import lmdb import lmdb
import PIL import PIL
import weasyprint
import zmq import zmq
from escpos import printer from escpos import printer
from mako.template import Template from mako.template import Template
@ -149,11 +150,11 @@ class Bureau(object):
self.api = {} self.api = {}
modpath = os.path.dirname(__file__) modpath = os.path.dirname(__file__)
slimerjs = os.path.join(modpath, "..", "lib", "slimerjs", "slimerjs") #slimerjs = os.path.join(modpath, "..", "lib", "slimerjs", "slimerjs")
renderer = os.path.join(modpath, "..", "slimerjs", "rasterize.js") #renderer = os.path.join(modpath, "..", "slimerjs", "rasterize.js")
self.slimerjs = os.path.abspath(slimerjs) #self.slimerjs = os.path.abspath(slimerjs)
self.html2pdf = self.slimerjs + " --headless " + \ #self.html2pdf = self.slimerjs + " --headless " + \
os.path.abspath(renderer) + " " # os.path.abspath(renderer) + " "
mypath = inspect.getfile(self.__class__) mypath = inspect.getfile(self.__class__)
self.mdir = os.path.dirname(mypath) self.mdir = os.path.dirname(mypath)
@ -316,14 +317,14 @@ class Bureau(object):
# lpname = kwargs.get("printer", "default") # lpname = kwargs.get("printer", "default")
htmlfile, htmlpath = tempfile.mkstemp(".html") #htmlfile, htmlpath = tempfile.mkstemp(".html")
htmlfile = os.fdopen(htmlfile, "w") #htmlfile = os.fdopen(htmlfile, "w")
# run template with kwargs # run template with kwargs
templfile = os.path.join(self.mdir, template) templfile = os.path.join(self.mdir, template)
self.log.debug("printing with template: %s", templfile) self.log.debug("printing with template: %s", templfile)
templ = Template(filename=templfile, strict_undefined=True) templ = Template(filename=templfile, strict_undefined=True)
htmlfile.write(templ.render_unicode(**kwargs)) #htmlfile.write(templ.render_unicode(**kwargs))
htmlfile.close() #htmlfile.close()
#.encode('utf-8', #.encode('utf-8',
# 'replace')) # 'replace'))
@ -338,8 +339,9 @@ class Bureau(object):
# TODO: make paper size a config variable # TODO: make paper size a config variable
pdfpath = tempfile.mkstemp(".pdf")[1] pdfpath = tempfile.mkstemp(".pdf")[1]
self.log.debug("rendering with: " + self.html2pdf + htmlpath + " " + pdfpath) self.log.debug("rendering with: " + self.html2pdf + htmlpath + " " + pdfpath)
subprocess.call(self.html2pdf + htmlpath + " " + pdfpath + weasyprint.HTML(string=templ.render_unicode(**kwargs)).write_pdf(pdfpath)
" A4 1920px", shell=True) #subprocess.call(self.html2pdf + htmlpath + " " + pdfpath +
# " A4 1920px", shell=True)
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

Loading…
Cancel
Save