Compare commits
2 Commits
fa5c846ebb
...
1cf4482914
| Author | SHA1 | Date | |
|---|---|---|---|
| 1cf4482914 | |||
| b9025fcabe |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
|||||||
label/
|
label/
|
||||||
|
build/
|
||||||
|
logs/
|
||||||
|
pdf_backup/
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -39,7 +39,7 @@ Write-Host ""
|
|||||||
|
|
||||||
Write-Host "[3/5] Installing dependencies..." -ForegroundColor Cyan
|
Write-Host "[3/5] Installing dependencies..." -ForegroundColor Cyan
|
||||||
Write-Host "Installing: python-barcode, pillow, reportlab, kivy, pyinstaller, pywin32, wmi..."
|
Write-Host "Installing: python-barcode, pillow, reportlab, kivy, pyinstaller, pywin32, wmi..."
|
||||||
pip install python-barcode pillow reportlab kivy==2.2.1 pyinstaller==6.1.0 pywin32 wmi
|
pip install python-barcode pillow reportlab kivy pyinstaller pywin32 wmi
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
Write-Host "ERROR: Failed to install dependencies" -ForegroundColor Red
|
Write-Host "ERROR: Failed to install dependencies" -ForegroundColor Red
|
||||||
Read-Host "Press Enter to exit"
|
Read-Host "Press Enter to exit"
|
||||||
|
|||||||
BIN
dist/LabelPrinter.exe
vendored
BIN
dist/LabelPrinter.exe
vendored
Binary file not shown.
@@ -41,38 +41,21 @@ def get_available_printers():
|
|||||||
return list(printers.keys()) if printers else ["PDF"]
|
return list(printers.keys()) if printers else ["PDF"]
|
||||||
|
|
||||||
elif SYSTEM == "Windows":
|
elif SYSTEM == "Windows":
|
||||||
# Windows: Get both local and network printers
|
# Windows: Get local + connected printers (includes print server connections)
|
||||||
try:
|
try:
|
||||||
printers = []
|
printers = []
|
||||||
|
|
||||||
# Get local printers
|
# PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS captures:
|
||||||
|
# - Locally installed printers
|
||||||
|
# - Printers connected from a print server (e.g. \\server\printer)
|
||||||
try:
|
try:
|
||||||
for printer_info in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL):
|
flags = win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS
|
||||||
|
for printer_info in win32print.EnumPrinters(flags):
|
||||||
printer_name = printer_info[2]
|
printer_name = printer_info[2]
|
||||||
if printer_name and printer_name not in printers:
|
if printer_name and printer_name not in printers:
|
||||||
printers.append(printer_name)
|
printers.append(printer_name)
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
print(f"Error enumerating printers: {e}")
|
||||||
|
|
||||||
# Get network printers from print server
|
|
||||||
try:
|
|
||||||
for printer_info in win32print.EnumPrinters(win32print.PRINTER_ENUM_NETWORK):
|
|
||||||
printer_name = printer_info[2]
|
|
||||||
if printer_name and printer_name not in printers:
|
|
||||||
printers.append(printer_name)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Get connected printers (alternative method using WMI)
|
|
||||||
try:
|
|
||||||
import wmi
|
|
||||||
c = wmi.WMI()
|
|
||||||
for printer in c.Win32_Printer():
|
|
||||||
printer_name = printer.Name
|
|
||||||
if printer_name and printer_name not in printers:
|
|
||||||
printers.append(printer_name)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Add PDF as fallback option
|
# Add PDF as fallback option
|
||||||
if "PDF" not in printers:
|
if "PDF" not in printers:
|
||||||
@@ -266,22 +249,37 @@ def print_to_printer(printer_name, file_path):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
elif SYSTEM == "Windows":
|
elif SYSTEM == "Windows":
|
||||||
# Windows: Use win32print or open with default printer
|
# Windows: Use win32print API for reliable printing (supports UNC paths)
|
||||||
try:
|
try:
|
||||||
if WIN32_AVAILABLE:
|
if WIN32_AVAILABLE:
|
||||||
import win32print
|
import win32print
|
||||||
import win32api
|
import win32api
|
||||||
# Print using the Windows API
|
|
||||||
win32api.ShellExecute(0, "print", file_path, f'/d:"{printer_name}"', ".", 0)
|
# Set the target printer as default temporarily, then print
|
||||||
print(f"Label sent to printer: {printer_name}")
|
# This approach works reliably with both local and UNC printer paths
|
||||||
|
try:
|
||||||
|
old_default = win32print.GetDefaultPrinter()
|
||||||
|
except:
|
||||||
|
old_default = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
win32print.SetDefaultPrinter(printer_name)
|
||||||
|
win32api.ShellExecute(0, "print", file_path, None, ".", 0)
|
||||||
|
print(f"Label sent to printer: {printer_name}")
|
||||||
|
finally:
|
||||||
|
# Restore original default printer
|
||||||
|
if old_default:
|
||||||
|
try:
|
||||||
|
win32print.SetDefaultPrinter(old_default)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
# Fallback: Open with default printer
|
# Fallback: Open with default printer
|
||||||
if file_path.endswith('.pdf'):
|
if file_path.endswith('.pdf'):
|
||||||
os.startfile(file_path, "print")
|
os.startfile(file_path, "print")
|
||||||
else:
|
else:
|
||||||
# For images, use default print application
|
subprocess.run(['notepad', '/p', file_path], check=False)
|
||||||
subprocess.run([f'notepad', '/p', file_path], check=False)
|
|
||||||
print(f"Label sent to default printer")
|
print(f"Label sent to default printer")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user