diff --git a/001.ico b/001.ico new file mode 100644 index 0000000..fd7f092 Binary files /dev/null and b/001.ico differ diff --git a/PrintAgent.py b/PrintAgent.py index 0182c8c..4a960e5 100644 --- a/PrintAgent.py +++ b/PrintAgent.py @@ -86,7 +86,7 @@ class SnPost: return menu = (MenuItem('退出', self.close), MenuItem('查看日志', self.log)) - image = Image.open("favicon.ico") + image = Image.open("001.ico") title = "打印机" self.icon = pystray.Icon(title, image, "打印机", menu) threading.Thread(target=self.icon.run, daemon=True).start() @@ -96,12 +96,11 @@ class SnPost: def pdf_printer(): if request.method == 'OPTIONS': # 跨域请求预检 return jsonify({"message": "OK"}), 200 - # if 'file' not in request.files: - # return jsonify({"error_message": "No file part", "error_code": 400}), 400 file = request.files['file'] printer_name = request.form.get('printer_name', None) # Save the file temporarily temp_pdf_path = os.path.join(os.path.dirname(__file__), 'temp_pdf.pdf') + logger.info(f"Saving file to {temp_pdf_path}") try: # Read the PDF binary @@ -109,7 +108,8 @@ def pdf_printer(): if printer_name: win32print.OpenPrinter(printer_name) win32print.SetDefaultPrinter(printer_name) - win32print.GetDefaultPrinter() + else: + return jsonify({"error_message": "打印机名称不正确","error_code": "printer_name_error"}), 400 win32api.ShellExecute( 0, @@ -121,6 +121,7 @@ def pdf_printer(): ) except Exception as e: import traceback + logger.error(traceback.format_exc()) print(traceback.format_exc()) return jsonify({"error_message": str(e), "error_code": "print_failed"}), 500 finally: @@ -145,32 +146,35 @@ def save_pdf_from_binary(pdf_binary, file_path): def str_printer(): if request.method == 'OPTIONS': # 跨域请求预检 return jsonify({"message": "OK"}), 200 - print_commands = request.json.get('print_commands', None) - print_name = request.json.get('print_name', None) - print(print_commands) + print_commands = request.json.get('printer_commands', None) + print_name = request.json.get('printer_name', None) tsclibrary = ctypes.WinDLL(".//TSCLIB.dll") if print_name: + logger.info("打印机名称: %s", print_name) tsclibrary.openportW(print_name) else: - print_name = win32print.GetDefaultPrinter() - tsclibrary.openportW(print_name) + logger.error("打印机名称无效") + return jsonify({"error_message": "打印机名称无线","error_code": "printer_name_error"}), 400 tsclibrary.clearbuffer() - for item in print_commands: - if 'WINTEXT' in item: - item_list = item.replace('WINTEXT ', '').split(',') - tsclibrary.windowsfontW( - item_list[0], - item_list[1], - item_list[2], - item_list[3], - item_list[4], - item_list[5], - item_list[6], - item_list[7]) - else: - tsclibrary.sendcommandW(item) - print(item, "33333") - tsclibrary.closeport() + try: + for item in print_commands: + if 'WINTEXT' in item: + item_list = item.replace('WINTEXT ', '').split(',') + tsclibrary.windowsfontW( + item_list[0], + item_list[1], + item_list[2], + item_list[3], + item_list[4], + item_list[5], + item_list[6], + item_list[7]) + else: + tsclibrary.sendcommandW(item) + tsclibrary.closeport() + except Exception as e: + print(e) + logger.error("打印错误: %s", str(e)) return jsonify({}), 200 diff --git a/PrintAgent.spec b/PrintAgent.spec index 5c9adfe..8776e29 100644 --- a/PrintAgent.spec +++ b/PrintAgent.spec @@ -29,7 +29,7 @@ exe = EXE( upx=True, upx_exclude=[], runtime_tmpdir=None, - console=True, + console=False, disable_windowed_traceback=False, argv_emulation=False, target_arch=None, diff --git a/favicon.ico b/favicon.ico deleted file mode 100644 index f6f458e..0000000 Binary files a/favicon.ico and /dev/null differ diff --git a/printer.log b/printer.log index e69de29..6c48673 100644 --- a/printer.log +++ b/printer.log @@ -0,0 +1,9 @@ +[2024-07-23 14:22:33,040] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:22:41,597] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:23:38,476] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:26:43,987] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:26:49,117] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:44:09,522] [PrintAgent.py:156] [ERROR]- ӡЧ +[2024-07-23 14:45:04,932] [PrintAgent.py:157] [ERROR]- ӡЧ +[2024-07-23 14:46:16,085] [PrintAgent.py:154] [INFO]- ӡ: GP-3150TN +[2024-07-23 14:47:39,217] [PrintAgent.py:153] [INFO]- ӡ: GP-3150TN