printer test ok
This commit is contained in:
parent
50908f237f
commit
c46d5e1ab0
@ -4,6 +4,9 @@ import sys
|
||||
from PyQt5.QtWidgets import QMessageBox
|
||||
|
||||
from .component import Component
|
||||
import platform
|
||||
if platform.system() == 'Windows':
|
||||
import win32print
|
||||
|
||||
|
||||
class Os_Label_Printer(Component):
|
||||
@ -31,24 +34,27 @@ class Os_Label_Printer(Component):
|
||||
with open(f"config/label_templates/{str(template)}.prn", "r", errors="surrogateescape") as f:
|
||||
label = f.read()
|
||||
# LABEL PRINT
|
||||
label = label.format(**context)
|
||||
label_file_contents = label.format(**context)
|
||||
label_file = "tmp/label.prn"
|
||||
with open(label_file, "w", errors="surrogateescape") as f:
|
||||
label = f.write(label)
|
||||
if self.platform == "windows":
|
||||
cmd = f'print /d:"{self.printer}" "{label_file}"'
|
||||
elif self.platform == "cups":
|
||||
cmd = f'lp -d "{self.printer}" "{label_file}"'
|
||||
ret = f.write(label)
|
||||
|
||||
if platform.system() == 'Windows':
|
||||
printer = win32print.OpenPrinter(self.printer)
|
||||
job = win32print.StartDocPrinter(printer, 1,
|
||||
("label", None, "RAW"))
|
||||
win32print.WritePrinter(printer, bytes(label_file_contents,encoding="utf8"))
|
||||
win32print.EndPagePrinter(printer)
|
||||
else:
|
||||
raise NotImplementedError(f"platform {self.platform!r} is not supported")
|
||||
if not self.simulate:
|
||||
p = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) # unsafe
|
||||
if p.returncode != 0:
|
||||
self.log.exception(f"failed to print: returncode: {p.returncode}\noutput:\n{p.stdout}")
|
||||
QMessageBox.critical(
|
||||
None,
|
||||
"Errore Stampante",
|
||||
f"Non e stato possibile stampare l'etichetta.\n\nErrore:\nreturncode: {p.returncode}\noutput:\n{p.stdout}"
|
||||
)
|
||||
return False
|
||||
if not self.simulate:
|
||||
cmd = f'lp -d "{self.printer}" "{label_file}"'
|
||||
p = subprocess.run(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True) # unsafe
|
||||
if p.returncode != 0:
|
||||
self.log.exception(f"failed to print: returncode: {p.returncode}\noutput:\n{p.stdout}")
|
||||
QMessageBox.critical(
|
||||
None,
|
||||
"Errore Stampante",
|
||||
f"Non e stato possibile stampare l'etichetta.\n\nErrore:\nreturncode: {p.returncode}\noutput:\n{p.stdout}"
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
Loading…
Reference in New Issue
Block a user