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

Loading…
Cancel
Save