feat: 提交上次完成

This commit is contained in:
caoqianming 2025-09-10 15:26:22 +08:00
parent 12cebc3a2e
commit be409273bb
6 changed files with 4 additions and 192 deletions

1
.gitignore vendored
View File

@ -12,4 +12,5 @@ media/*
dist/*
build/*
temp/*
xx/GxPrint.exe

View File

@ -2,7 +2,7 @@
a = Analysis(
['PrintAgent.py'],
['GxPrint.py'],
pathex=[],
binaries=[],
datas=[],
@ -22,7 +22,7 @@ exe = EXE(
a.binaries,
a.datas,
[],
name='PrintAgent',
name='GxPrint',
debug=False,
bootloader_ignore_signals=False,
strip=False,
@ -35,4 +35,5 @@ exe = EXE(
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=['001.ico'],
)

View File

@ -1,181 +0,0 @@
"""
@time:2024-07018
@author:zhang
@param:pdf_file
PDF文件打印
"""
import win32print
import win32api
import os
import threading
from flask import Flask, request, jsonify
from PyPDF2 import PdfWriter, PdfReader
from flask_cors import CORS
import ctypes
import pystray
from PIL import Image
from pystray import MenuItem
# import requests
import socket
import logging
from logging.handlers import RotatingFileHandler
import sys
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
log_path = os.path.join(CUR_DIR, 'printer.log')
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(filename)s:%(lineno)d] [%(levelname)s]- %(message)s')
file_hander = RotatingFileHandler(log_path, maxBytes=1024*1024*10, backupCount=1)
file_hander.setFormatter(formatter)
file_hander.setLevel(logging.INFO)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
console_handler.setLevel(logging.DEBUG)
logger.addHandler(file_hander)
logger.addHandler(console_handler)
log_command = f'start "" powershell -Command "Get-Content -Path \'{log_path}\' -Tail 10 -Wait"'
# 创建一个Flask应用
app = Flask(__name__)
CORS(app, supports_credentials=True)
app.json.ensure_ascii = False
class MyPrint:
def __init__(self) -> None:
self.is_one_instance = False
self.is_working = False
self.icon = None # 下标
self.check_one_instance()
def check_one_instance(self):
try:
global s
s = socket.socket()
host = socket.gethostname()
s.bind((host, 20309))
self.is_one_instance = True
except Exception:
self.is_one_instance = False
def close(self):
self.is_working = False
self.icon.stop()
logger.info("程序退出")
sys.exit(0)
def log(self):
os.system(log_command)
def run(self):
if not self.is_one_instance:
return
menu = (MenuItem('查看日志', self.log))
image = Image.open("001.ico")
title = "打印机"
self.icon = pystray.Icon(title, image, "打印机", menu)
threading.Thread(target=self.icon.run, daemon=True).start()
app.run(port=8080, debug=False, host='0.0.0.0')
@app.route('/print/', methods=['POST', 'OPTIONS'])
def pdf_printer():
if request.method == 'OPTIONS': # 跨域请求预检
return jsonify({"message": "OK"}), 200
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
file.save(temp_pdf_path)
if printer_name:
win32print.OpenPrinter(printer_name)
win32print.SetDefaultPrinter(printer_name)
else:
return jsonify({"err_msg": "打印机名称不正确","err_code": "printer_name_error"}), 400
win32api.ShellExecute(
0,
"print",
temp_pdf_path,
None,
".",
0
)
logger.info("打印成功")
except Exception as e:
logger.error(f"打印失败: {e}")
return jsonify({"err_msg": "打印失败", "err_code": "print_failed"}), 500
finally:
t = threading.Thread(target=del_file, args=(temp_pdf_path,))
t.start()
# # 清除打印文件
return jsonify({}), 200
def del_file(path):
import time
time.sleep(5)
if os.path.exists(path):
os.remove(path)
def save_pdf_from_binary(pdf_binary, file_path):
"""将二进制数据写入 PDF 文件"""
with open(file_path, 'wb') as file:
file.write(pdf_binary)
@app.route('/prints/', methods=['POST', 'OPTIONS'])
def str_printer():
if request.method == 'OPTIONS': # 跨域请求预检
return jsonify({"message": "OK"}), 200
print_commands = request.json.get('printer_commands', None)
print_name = request.json.get('printer_name', None)
tsclibrary = ctypes.WinDLL(".//TSCLIB.dll")
try:
tsclibrary.openportW(print_name)
except Exception as e:
logger.error(f"打印机名称无效: {e}")
return jsonify({"err_msg": "打印机名称无效","err_code": "printer_name_error"}), 400
tsclibrary.clearbuffer()
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()
logger.info("打印成功")
except Exception as e:
logger.error(f"打印失败: {e}")
return jsonify({"err_msg": "打印失败", "err_code": "print_failed"}), 500
return jsonify({}), 200
if __name__ == '__main__':
MyPrint().run()

View File

@ -1,9 +0,0 @@
[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

BIN
xx/001.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

BIN
xx/TSCLIB.dll Normal file

Binary file not shown.