180 lines
6.5 KiB
Python
180 lines
6.5 KiB
Python
from flask import Flask, request, jsonify
|
|
import os
|
|
import threading
|
|
import queue
|
|
import pdf2image
|
|
from brother_ql.conversion import convert
|
|
from brother_ql.backends.helpers import send
|
|
from brother_ql.raster import BrotherQLRaster
|
|
from PIL import Image
|
|
import mimetypes
|
|
import logging
|
|
from logging.handlers import RotatingFileHandler
|
|
import time
|
|
import usb.core # For USB device re-initialization
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Set up logging
|
|
log_file = 'log.txt'
|
|
log_handler = RotatingFileHandler(log_file, maxBytes=10*1024*1024, backupCount=5) # 10MB per log file, keep 5 backups
|
|
log_handler.setLevel(logging.INFO)
|
|
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
|
log_handler.setFormatter(log_formatter)
|
|
app.logger.addHandler(log_handler)
|
|
|
|
# Create a queue to hold the files for processing
|
|
file_queue = queue.Queue()
|
|
|
|
# Directory to save uploaded files (you can modify this path)
|
|
UPLOAD_FOLDER = './uploads'
|
|
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
|
|
|
# Printer configuration
|
|
printer_model = "QL-1100"
|
|
backend = 'pyusb'
|
|
printer = 'usb://0x04f9:0x20a7'
|
|
|
|
# Create a lock to manage printer access
|
|
printer_lock = threading.Lock()
|
|
|
|
# Convert PDF to image
|
|
def convert_pdf_to_image(pdf_path):
|
|
"""Converts a PDF to a PIL Image"""
|
|
try:
|
|
# Convert PDF to image (get the first page)
|
|
images = pdf2image.convert_from_path(pdf_path)
|
|
if images:
|
|
return images[0] # We will only print the first page for now
|
|
return None
|
|
except Exception as e:
|
|
app.logger.error(f"Error converting PDF: {str(e)}")
|
|
return None
|
|
|
|
# Reinitialize the printer by disconnecting and reconnecting
|
|
def reinitialize_printer():
|
|
try:
|
|
app.logger.info("Reinitializing printer...")
|
|
usb_device = usb.core.find(idVendor=0x04f9, idProduct=0x20a7)
|
|
if usb_device:
|
|
usb_device.reset()
|
|
app.logger.info("Printer reinitialized successfully.")
|
|
else:
|
|
app.logger.error("Failed to find printer device.")
|
|
except Exception as e:
|
|
app.logger.error(f"Error reinitializing printer: {str(e)}")
|
|
|
|
# Function to print address labels
|
|
def print_address_label(pdf_path):
|
|
try:
|
|
# Convert PDF to image
|
|
image = convert_pdf_to_image(pdf_path)
|
|
if not image:
|
|
raise Exception("Failed to create label images")
|
|
|
|
target_width = 1164
|
|
target_height = 1660
|
|
image = image.convert("RGB")
|
|
image = image.resize((target_width, target_height), Image.LANCZOS)
|
|
|
|
qlr = BrotherQLRaster(printer_model)
|
|
qlr.exception_on_warning = True
|
|
|
|
app.logger.info("Converting image to printer instructions...")
|
|
instructions = convert(
|
|
qlr=qlr,
|
|
images=[image], # Pass as a list with the single image
|
|
label='102x152',
|
|
threshold=70.0,
|
|
dither=False,
|
|
compress=False,
|
|
red=False,
|
|
dpi_600=False,
|
|
hq=True,
|
|
cut=True
|
|
)
|
|
|
|
# Try to acquire the printer lock with a longer timeout
|
|
max_retries = 5 # Retry up to 5 times
|
|
retry_delay = 5 # Retry every 5 seconds
|
|
for attempt in range(max_retries):
|
|
if printer_lock.acquire(timeout=30): # 30 seconds timeout to allow more time for the printer
|
|
try:
|
|
app.logger.info(f"Attempting to send print job (Attempt {attempt + 1})...")
|
|
send(
|
|
instructions=instructions,
|
|
printer_identifier=printer,
|
|
backend_identifier=backend,
|
|
blocking=True # Ensure blocking, so we wait for the print to complete
|
|
)
|
|
app.logger.info("Print job sent successfully")
|
|
break
|
|
except Exception as e:
|
|
app.logger.error(f"Error during printing: {str(e)}")
|
|
if attempt < max_retries - 1:
|
|
app.logger.info(f"Retrying after {retry_delay} seconds...")
|
|
time.sleep(retry_delay)
|
|
else:
|
|
app.logger.error("Max retries reached. Failed to send print job.")
|
|
reinitialize_printer() # Reinitialize printer after retries
|
|
else:
|
|
app.logger.error(f"Failed to acquire printer lock (Attempt {attempt + 1}). Printer is busy.")
|
|
if attempt < max_retries - 1:
|
|
app.logger.info(f"Retrying after {retry_delay} seconds...")
|
|
time.sleep(retry_delay)
|
|
else:
|
|
app.logger.error("Printer is still busy after retries.")
|
|
raise Exception("Printer is busy. Try again later.")
|
|
|
|
except Exception as e:
|
|
app.logger.error(f"Error during label printing: {str(e)}")
|
|
|
|
# Worker thread that processes files from the queue
|
|
def process_queue():
|
|
while True:
|
|
# Wait until a file is available in the queue
|
|
file_path = file_queue.get()
|
|
if file_path is None: # Shutdown signal
|
|
break
|
|
# Process the file (convert and print)
|
|
app.logger.info(f"Processing file: {file_path}")
|
|
print_address_label(file_path)
|
|
# Signal that the task is done
|
|
file_queue.task_done()
|
|
|
|
# Start the worker thread
|
|
thread = threading.Thread(target=process_queue, daemon=True)
|
|
thread.start()
|
|
|
|
@app.route('/upload', methods=['POST'])
|
|
def upload_file():
|
|
# Check if a file is part of the request
|
|
if 'file' not in request.files:
|
|
app.logger.warning('No file part in request')
|
|
return jsonify({'error': 'No file part'}), 400
|
|
|
|
file = request.files['file']
|
|
|
|
if file.filename == '':
|
|
app.logger.warning('No selected file')
|
|
return jsonify({'error': 'No selected file'}), 400
|
|
|
|
# Check file type (ensure it's a PDF)
|
|
mime_type, _ = mimetypes.guess_type(file.filename)
|
|
if mime_type != 'application/pdf':
|
|
app.logger.warning(f"Invalid file type: {mime_type}. Only PDF files are allowed.")
|
|
return jsonify({'error': 'Only PDF files are allowed'}), 400
|
|
|
|
# Save the file to the uploads folder
|
|
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
|
file.save(file_path)
|
|
app.logger.info(f"File {file.filename} uploaded and saved to {file_path}")
|
|
|
|
# Add the file path to the queue for processing
|
|
file_queue.put(file_path)
|
|
|
|
return jsonify({'message': f'File {file.filename} uploaded and queued for processing'}), 200
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|