Import latest version from label_printer repository as starting point

This commit is contained in:
NAME
2026-02-12 19:25:05 +02:00
parent fdb143e726
commit 0743c44051
8 changed files with 273 additions and 28 deletions

View File

@@ -28,6 +28,7 @@ SYSTEM = platform.system() # 'Linux', 'Windows', 'Darwin'
def get_available_printers():
"""
Get list of available printers (cross-platform).
Includes both local and network printers on Windows.
Returns:
list: List of available printer names, with "PDF" as fallback
@@ -40,14 +41,29 @@ def get_available_printers():
return list(printers.keys()) if printers else ["PDF"]
elif SYSTEM == "Windows":
# Windows: Try win32print first
# Windows: Get local + connected printers (includes print server connections)
try:
printers = []
for printer_name in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL):
printers.append(printer_name[2])
# PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS captures:
# - Locally installed printers
# - Printers connected from a print server (e.g. \\server\printer)
try:
flags = win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS
for printer_info in win32print.EnumPrinters(flags):
printer_name = printer_info[2]
if printer_name and printer_name not in printers:
printers.append(printer_name)
except Exception as e:
print(f"Error enumerating printers: {e}")
# Add PDF as fallback option
if "PDF" not in printers:
printers.append("PDF")
return printers if printers else ["PDF"]
except:
# Fallback for Windows if win32print fails
except Exception as e:
print(f"Error getting Windows printers: {e}")
return ["PDF"]
elif SYSTEM == "Darwin":
@@ -233,23 +249,58 @@ def print_to_printer(printer_name, file_path):
return True
elif SYSTEM == "Windows":
# Windows: Use win32print or open with default printer
# Windows: Print directly without opening PDF viewer
try:
if WIN32_AVAILABLE:
import win32print
import win32api
# Print using the Windows API
win32api.ShellExecute(0, "print", file_path, f'/d:"{printer_name}"', ".", 0)
print(f"Label sent to printer: {printer_name}")
return True
else:
# Fallback: Open with default printer
if file_path.endswith('.pdf'):
os.startfile(file_path, "print")
# Use SumatraPDF command-line or direct raw printing
# Try printing via subprocess to avoid opening a PDF viewer window
# Method: Use win32print raw API to send to printer silently
try:
hprinter = win32print.OpenPrinter(printer_name)
try:
# Start a print job
job_info = ("Label Print", None, "RAW")
hjob = win32print.StartDocPrinter(hprinter, 1, job_info)
win32print.StartPagePrinter(hprinter)
# Read PDF file and send to printer
with open(file_path, 'rb') as f:
pdf_data = f.read()
win32print.WritePrinter(hprinter, pdf_data)
win32print.EndPagePrinter(hprinter)
win32print.EndDocPrinter(hprinter)
print(f"Label sent to printer: {printer_name}")
finally:
win32print.ClosePrinter(hprinter)
return True
except Exception as raw_err:
print(f"Raw print failed ({raw_err}), trying ShellExecute silently...")
# Fallback: Use ShellExecute with printto (minimized, auto-closes)
try:
win32api.ShellExecute(
0, "printto", file_path,
f'"{printer_name}"', ".", 0
)
print(f"Label sent to printer: {printer_name}")
return True
except Exception as shell_err:
print(f"ShellExecute print failed: {shell_err}")
return True
else:
# For images, use default print application
subprocess.run([f'notepad', '/p', file_path], check=False)
print(f"Label sent to default printer")
# Non-PDF files: print silently with notepad
subprocess.run(['notepad', '/p', file_path],
check=False,
creationflags=subprocess.CREATE_NO_WINDOW)
print(f"Label sent to printer: {printer_name}")
return True
else:
print("win32print not available, PDF saved as backup only")
return True
except Exception as e:
print(f"Windows print error: {e}")