st-ten-1/src/components/os_label_printer.py

102 lines
4.0 KiB
Python
Raw Normal View History

2022-07-26 14:46:27 +00:00
import os
2022-07-26 14:56:26 +00:00
import platform
2022-06-01 16:37:19 +00:00
import subprocess
import sys
2022-09-26 11:26:42 +00:00
import traceback
2022-06-01 16:37:19 +00:00
from PyQt5.QtWidgets import QMessageBox
from .component import Component
2023-07-12 17:37:57 +00:00
if platform.system().lower() == "windows":
2022-07-26 14:37:22 +00:00
import win32print
2022-06-01 16:37:19 +00:00
class Os_Label_Printer(Component):
def __init__(self, config=None, name=None):
super().__init__(config=config, name=name, threaded=False)
2022-07-26 14:09:01 +00:00
self.simulate = False
2022-06-01 16:37:19 +00:00
if "--sim-os-label-printer" in sys.argv:
self.simulate = True
2022-07-26 14:46:27 +00:00
2022-06-01 16:37:19 +00:00
def config_changed(self):
2022-06-29 09:02:58 +00:00
self.platform = self.config[self.name]["platform"]
2022-06-01 16:37:19 +00:00
# for windows:
# cmd
# wmic printer list brief
# powershell
# Get-Printer
# for cups (linux, osx)
# lpstat -p -d
2022-06-29 09:02:58 +00:00
self.printer = self.config[self.name]["printer"]
2022-06-01 16:37:19 +00:00
@Component.reconfig_on_error
2022-07-25 13:36:42 +00:00
def print_label(self, template, context=None):
if context is None:
context = {}
2023-09-03 15:54:15 +00:00
custom_label_folder=f"config/label_templates/{str(self.config.machine_id)}/"
standard_label_folder=f"config/label_templates/"
if os.path.exists(custom_label_folder):
label_folder = custom_label_folder
else:
label_folder = standard_label_folder
label_path = label_folder+str(template)
2023-09-05 14:39:01 +00:00
if os.path.exists(label_path):
pass
else:
label_path = standard_label_folder+str(template)
2022-06-01 16:37:19 +00:00
# LOAD LABEL TEMPLATE
2023-09-03 15:54:15 +00:00
with open(label_path, "r", errors="surrogateescape", encoding='iso-8859-1') as f:
2022-09-15 13:25:22 +00:00
label_file_contents = f.read()
2022-06-01 16:37:19 +00:00
# LABEL PRINT
2022-09-15 13:25:22 +00:00
#label = label.format(**context)
2022-10-04 14:22:37 +00:00
label_file_contents = label_file_contents.replace("{BCODE}", "{YY}{MO}{DD}{HH}{MI}{SN6}")
2022-09-15 13:25:22 +00:00
for key, val in context.items():
2022-09-26 11:26:42 +00:00
key = "{" + key + "}"
2022-09-15 13:25:22 +00:00
label_file_contents = label_file_contents.replace(key, val)
2023-07-12 17:37:57 +00:00
if platform.system().lower() == "windows":
2022-09-14 14:53:55 +00:00
try:
printer = win32print.OpenPrinter(self.printer)
job = win32print.StartDocPrinter(printer, 1, ("label", None, "RAW"))
2022-09-15 13:25:22 +00:00
win32print.WritePrinter(printer, bytes(label_file_contents, encoding="utf8", errors="surrogateescape"))
2022-09-14 14:53:55 +00:00
win32print.EndPagePrinter(printer)
2022-09-15 13:25:22 +00:00
return label_file_contents
2022-09-14 14:53:55 +00:00
except Exception as e:
self.log.exception(traceback.format_exc())
QMessageBox.critical(
None,
"Errore Stampante",
f"Non e stato possibile stampare l'etichetta.\n\nErrore:\n{e}"
)
return None
2022-06-01 16:37:19 +00:00
else:
2022-09-14 14:53:55 +00:00
try:
os.makedirs("tmp", exist_ok=True)
label_file = "tmp/label.prn"
with open(label_file, "w", errors="surrogateescape") as f:
2022-10-25 13:38:18 +00:00
f.write(label_file_contents)
2023-09-03 08:00:31 +00:00
if platform.system().lower() == "windows":
2022-09-14 14:53:55 +00:00
cmd = f'print /d:"{self.printer}" "{label_file}"'
else:
2023-09-03 08:00:31 +00:00
cmd = f'lp -d "{self.printer}" "{label_file}"'
2022-09-14 14:53:55 +00:00
if not self.simulate:
p = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) # unsafe
if p.returncode != 0:
2023-09-03 08:00:31 +00:00
self.log.exception(f"failed to print: return code: {p.returncode}\noutput:\n{p.stdout}")
2022-09-14 14:53:55 +00:00
QMessageBox.critical(
None,
"Errore Stampante",
f"Non e stato possibile stampare l'etichetta.\n\nErrore:\nreturncode: {p.returncode}\noutput:\n{p.stdout}"
)
return None
2022-10-25 13:38:18 +00:00
return label_file_contents
2022-09-14 14:53:55 +00:00
except Exception as e:
self.log.exception(traceback.format_exc())
QMessageBox.critical(
None,
"Errore Stampante",
f"Non e stato possibile stampare l'etichetta.\n\nErrore:\n{e}"
)
return None
return None