Compare commits
1 Commits
a37918fe11
...
idk
Author | SHA1 | Date | |
---|---|---|---|
12d941fe2e |
50
app.py
50
app.py
@ -4,7 +4,7 @@ import threading
|
|||||||
import queue
|
import queue
|
||||||
import pdf2image
|
import pdf2image
|
||||||
from brother_ql.conversion import convert
|
from brother_ql.conversion import convert
|
||||||
from brother_ql.backends.helpers import send, status
|
from brother_ql.backends.helpers import send
|
||||||
from brother_ql.raster import BrotherQLRaster
|
from brother_ql.raster import BrotherQLRaster
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import mimetypes
|
import mimetypes
|
||||||
@ -16,19 +16,12 @@ app = Flask(__name__)
|
|||||||
|
|
||||||
# Set up logging
|
# Set up logging
|
||||||
log_file = 'log.txt'
|
log_file = 'log.txt'
|
||||||
|
|
||||||
# Create a rotating file handler for logging
|
|
||||||
log_handler = RotatingFileHandler(log_file, maxBytes=10*1024*1024, backupCount=5) # 10MB per log file, keep 5 backups
|
log_handler = RotatingFileHandler(log_file, maxBytes=10*1024*1024, backupCount=5) # 10MB per log file, keep 5 backups
|
||||||
log_handler.setLevel(logging.DEBUG)
|
log_handler.setLevel(logging.INFO)
|
||||||
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
||||||
log_handler.setFormatter(log_formatter)
|
log_handler.setFormatter(log_formatter)
|
||||||
|
|
||||||
# Add the log handler to the Flask app's logger
|
|
||||||
app.logger.addHandler(log_handler)
|
app.logger.addHandler(log_handler)
|
||||||
|
|
||||||
# Ensure Flask app uses our logger, not its default one
|
|
||||||
app.logger.setLevel(logging.DEBUG)
|
|
||||||
|
|
||||||
# Create a queue to hold the files for processing
|
# Create a queue to hold the files for processing
|
||||||
file_queue = queue.Queue()
|
file_queue = queue.Queue()
|
||||||
|
|
||||||
@ -42,33 +35,30 @@ backend = 'pyusb'
|
|||||||
printer = 'usb://0x04f9:0x20a7'
|
printer = 'usb://0x04f9:0x20a7'
|
||||||
|
|
||||||
# Convert PDF to image
|
# Convert PDF to image
|
||||||
def convert_pdf_to_images(pdf_path):
|
def convert_pdf_to_image(pdf_path):
|
||||||
"""Converts a PDF to a PIL Image"""
|
"""Converts a PDF to a PIL Image"""
|
||||||
try:
|
try:
|
||||||
# Convert PDF to image (get the first page)
|
# Convert PDF to image (get the first page)
|
||||||
images = pdf2image.convert_from_path(pdf_path)
|
images = pdf2image.convert_from_path(pdf_path)
|
||||||
if images:
|
if images:
|
||||||
return images
|
return images[0] # We will only print the first page for now
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
app.logger.error(f"Error converting PDF: {str(e)}")
|
app.logger.error(f"Error converting PDF: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Function to print address labels
|
# Function to print address labels
|
||||||
def print_address_label(images, pdf_path):
|
def print_address_label(pdf_path):
|
||||||
try:
|
try:
|
||||||
|
# Convert PDF to image
|
||||||
if 'address' in pdf_path.lower():
|
image = convert_pdf_to_image(pdf_path)
|
||||||
target_width = 1660
|
if not image:
|
||||||
target_height = 1164
|
raise Exception("Failed to create label images")
|
||||||
image = image.convert("RGB")
|
|
||||||
image = image.resize((target_width, target_height), Image.LANCZOS)
|
target_width = 1164
|
||||||
image = image.rotate(90, expand=True)
|
target_height = 1660
|
||||||
else:
|
image = image.convert("RGB")
|
||||||
target_width = 1164
|
image = image.resize((target_width, target_height), Image.LANCZOS)
|
||||||
target_height = 1660
|
|
||||||
image = image.convert("RGB")
|
|
||||||
image = image.resize((target_width, target_height), Image.LANCZOS)
|
|
||||||
|
|
||||||
qlr = BrotherQLRaster(printer_model)
|
qlr = BrotherQLRaster(printer_model)
|
||||||
qlr.exception_on_warning = True
|
qlr.exception_on_warning = True
|
||||||
@ -76,7 +66,7 @@ def print_address_label(images, pdf_path):
|
|||||||
app.logger.info("Converting image to printer instructions...")
|
app.logger.info("Converting image to printer instructions...")
|
||||||
instructions = convert(
|
instructions = convert(
|
||||||
qlr=qlr,
|
qlr=qlr,
|
||||||
images=images,
|
images=[image], # Pass as a list with the single image
|
||||||
label='102x152',
|
label='102x152',
|
||||||
threshold=70.0,
|
threshold=70.0,
|
||||||
dither=False,
|
dither=False,
|
||||||
@ -88,13 +78,12 @@ def print_address_label(images, pdf_path):
|
|||||||
)
|
)
|
||||||
|
|
||||||
app.logger.info("Sending to printer...")
|
app.logger.info("Sending to printer...")
|
||||||
status = send(
|
send(
|
||||||
instructions=instructions,
|
instructions=instructions,
|
||||||
printer_identifier=printer,
|
printer_identifier=printer,
|
||||||
backend_identifier=backend,
|
backend_identifier=backend,
|
||||||
blocking=True
|
blocking=True
|
||||||
)
|
)
|
||||||
app.logger.info(f"Printer status: {status}")
|
|
||||||
app.logger.info("Print job sent successfully")
|
app.logger.info("Print job sent successfully")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -109,11 +98,8 @@ def process_queue():
|
|||||||
break
|
break
|
||||||
# Process the file (convert and print)
|
# Process the file (convert and print)
|
||||||
app.logger.info(f"Processing file: {file_path}")
|
app.logger.info(f"Processing file: {file_path}")
|
||||||
images = convert_pdf_to_images(file_path)
|
print_address_label(file_path)
|
||||||
if images:
|
time.sleep(10)
|
||||||
print_address_label(images, file_path)
|
|
||||||
else:
|
|
||||||
app.logger.error(f"Failed to convert PDF to images: {file_path}")
|
|
||||||
# Signal that the task is done
|
# Signal that the task is done
|
||||||
file_queue.task_done()
|
file_queue.task_done()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user