Add new printing options, change image conversion for Brother QL

printers
This commit is contained in:
n07070
2026-11-12 17:26:50 +01:00
parent d254fa0d32
commit 72e6e93337
6 changed files with 80 additions and 24 deletions

View File

@@ -173,9 +173,13 @@ def web_print_sms():
"No signature found for this print, using default signature : %s ", str(e)
)
sign = configuration_file["defaults"]["signature"]
try:
do_cut = bool(request.form["cut"])
except werkzeug.exceptions.BadRequestKeyError:
do_cut = False
try:
web.print_sms(txt, sign)
web.print_sms(txt, sign, do_cut)
except RuntimeError as e:
app.logger.error("Whoops, we could not print an SMS because : %s ", str(e))
flash("Whoops, we could not print an SMS because :" + str(e), "error")
@@ -219,9 +223,20 @@ def web_print_img():
flash("Submitted file has no filename !", "error")
return redirect(url_for("index"))
try:
do_cut = bool(request.form["cut"])
except werkzeug.exceptions.BadRequestKeyError:
do_cut = False
try:
process_image = bool(request.form["process"])
except werkzeug.exceptions.BadRequestKeyError:
process_image = False
try:
app.logger.debug("Sending the image to the printer.")
web.print_image(file, sign)
web.print_image(file, sign, do_cut, process_image)
except RuntimeError as e:
app.logger.error("The image could not be printed because : %s ", str(e))
flash("The image could not be printed because : " + str(e), "error")
@@ -266,9 +281,16 @@ def api_print_sms():
"No signature found for this print, using default signature. : %s", str(e)
)
sign = configuration_file["defaults"]["signature"]
try:
do_cut = bool(request.form["cut"])
except werkzeug.exceptions.BadRequestKeyError:
do_cut = False
try:
# comment: We try to print the SMS
web.print_sms(txt, sign)
web.print_sms(txt, sign, do_cut)
except RuntimeError as e:
return str(e), 500
# end try
@@ -301,9 +323,19 @@ def api_print_image():
app.logger.error("Submitted file has no filename !")
return "Submitted file has no filename !", 400
try:
do_cut = bool(request.form["cut"])
except werkzeug.exceptions.BadRequestKeyError:
do_cut = False
try:
process_image = bool(request.form["process"])
except werkzeug.exceptions.BadRequestKeyError:
process_image = False
try:
app.logger.debug("Sending the image to the printer.")
web.print_image(file, sign)
web.print_image(file, sign, do_cut, process_image)
except RuntimeError as e:
return str(e), 500

View File

@@ -460,22 +460,35 @@ class BrotherPrinter(Printer):
"""Print text after converting it to an image"""
self._print_img(text_to_image(msg))
def _print_img(self, data):
def _print_img(self, data, process=False):
"""
Print a raster image via a Brother QL printer
"""
self.ready = False
if process:
try:
self.app.logger.debug("Proccessing the image")
processed_path = _process_image(self, data)
except RuntimeError as e:
self.app.logger.error(
"Error while processing the image, aborting print : %s", str(e)
)
raise e
else:
processed_path = data
self.app.logger.warning("Not proccessing the image")
try:
selected_backend = guess_backend(self.printer_info.identifier)
BACKEND_CLASS = backend_factory(selected_backend)['backend_class']
BACKEND_STRING_DESCR = self.printer_info.identifier
except Exception as e:
except Exception:
self.app.logger.error("Couldn't guess the backend to use from the printer string descriptor")
try:
im = Image.open(data)
im = Image.open(processed_path)
qlr = BrotherQLRaster(self.printer_info.model)
create_label(qlr, im, self.printer_info.label_type, threshold=70, cut=False, rotate=0)
create_label(qlr, im, self.printer_info.label_type, cut=False, dither=True)
# noinspection PyCallingNonCallable
be = BACKEND_CLASS(BACKEND_STRING_DESCR)

BIN
src/static/images/1x1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

@@ -6,13 +6,13 @@
<h3 class="card-header">Print a short message</h3>
<div class="card-body">
<form class="form-group" action="/web/print/sms" method="post">
<textarea class="form-control" type="text" name="txt" placeholder="4096 chars or less " maxlength="4096"></textarea>
<textarea class="form-control" type="text" name="txt" placeholder="4096 caractères maximum " maxlength="4096"></textarea>
<br>
<input class="form-control" type="text" name="signature" placeholder="Signature or pseudo" maxlength="200"><br>
<input class="form-control" type="text" name="signature" placeholder="Signature ou pseudo" maxlength="200"><br>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="cut" id="flexCheckDefault">
<input class="form-check-input" type="checkbox" value="True" id="flexCheckDefault" name="cut" checked>
<label class="form-check-label" for="flexCheckDefault">
Cut after print ?
Couper après impression ?
</label>
</div>
<br>
@@ -28,11 +28,17 @@
<div class="card-body">
<form enctype="multipart/form-data" class="form-group" action="/web/print/img" method="post">
<input class="form-control" type="file" name="img" required><br>
<input class="form-control" type="text" name="signature" placeholder="Signature or pseudo" maxlength="200"><br>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="cut" id="flexCheckDefault">
<input class="form-control" type="text" name="signature" placeholder="Signature ou pseudo" maxlength="200"><br>
<div class="form-check .form-check-inline">
<input class="form-check-input" type="checkbox" value="True" id="flexCheckDefault" name="cut" checked>
<label class="form-check-label" for="flexCheckDefault">
Cut after print ?
Couper après impression ?
</label>
</div>
<div class="form-check .form-check-inline">
<input class="form-check-input" type="checkbox" value="True" id="flexCheckDefault" name="process">
<label class="form-check-label" for="flexCheckDefault">
Traiter l'image ?
</label>
</div>
<br>

View File

@@ -16,7 +16,7 @@ class Web:
self.print_queue = print_queue
self.app = app
def print_sms(self, texte, sign: str) -> bool:
def print_sms(self, texte, sign: str, do_cut: bool) -> bool:
"""
Get text and a signature, prints the text and cuts after that.
"""
@@ -29,14 +29,18 @@ class Web:
# are added back to back, another task could be
# inserted between the two.
sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign))
cut = self.print_queue.enqueue(CutTask())
self.app.logger.info("Added a SMS task at position %s", sms)
if do_cut:
cut = self.print_queue.enqueue(CutTask())
self.app.logger.info("Added a CUT task at position %s", cut)
except Exception as e:
self.app.logger.error(e)
raise RuntimeError("Could not add SMS to queue, " + str(e)) from e
self.app.logger.info("Added two new tasks at position %s and %s", sms, cut)
return True
def print_image(self, image, sign: str) -> bool:
def print_image(self, image, sign: str, do_cut: bool, process_image: bool) -> bool:
"""
Get an image and a signature, prints the image and cuts after that.
"""
@@ -56,16 +60,17 @@ class Web:
secure_filename(image.filename),
),
signature=sign,
process=True,
process=process_image,
)
)
self.app.logger.info("Added a IMG task at position %s", img)
cut = self.print_queue.enqueue(CutTask())
if do_cut:
cut = self.print_queue.enqueue(CutTask())
self.app.logger.info("Added a CUT task at position %s", cut)
except Exception as e:
raise RuntimeError("Could not add IMG to queue" + str(e)) from e
self.app.logger.info("Added two new tasks at position %s and %s", img, cut)
return True
# def login(self, username: str, password: str) -> bool: