Fix Windows browse, status label mapping, and repo cleanup

This commit is contained in:
2026-02-19 15:13:48 +02:00
parent 9b7697321b
commit 351fd3dfea
5 changed files with 148 additions and 105 deletions

View File

@@ -208,7 +208,8 @@ def create_label_pdf(text, svg_template=None):
PDFs are saved to the pdf_backup folder.
Args:
text (str): Combined text in format "article;nr_art;serial" or single value
text (str): Combined text in format "article;nr_art;serial;status" or single value
status: 1 = OK label, 0 = NOK label
svg_template (str): Path to specific SVG template to use (optional)
Returns:
@@ -219,6 +220,7 @@ def create_label_pdf(text, svg_template=None):
article = parts[0].strip() if len(parts) > 0 else ''
nr_art = parts[1].strip() if len(parts) > 1 else ''
serial = parts[2].strip() if len(parts) > 2 else ''
status_flag = parts[3].strip() if len(parts) > 3 else '1'
# Create PDF using high-quality generator
generator = PDFLabelGenerator()
@@ -230,22 +232,31 @@ def create_label_pdf(text, svg_template=None):
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
pdf_filename = os.path.join(pdf_backup_dir, f"final_label_{timestamp}.pdf")
# Use SVG template for customizable layout
if svg_template is None or not os.path.exists(svg_template):
# Try default templates
default_svg = os.path.join('conf', 'label_template.svg')
if os.path.exists(default_svg):
svg_template = default_svg
print(f"Using SVG template: {default_svg}")
else:
print("SVG template not found, using fallback PDF generation")
# Select template/image based on status flag
# 1 = OK label, 0 = NOK label
selected_template = svg_template if svg_template and os.path.exists(svg_template) else None
default_svg = os.path.join('conf', 'label_template.svg')
ok_svg = os.path.join('conf', 'label_template_ok.svg')
nok_svg = os.path.join('conf', 'label_template_nok.svg')
if selected_template:
image_path = os.path.join('conf', 'accepted.png') if status_flag != '0' else os.path.join('conf', 'refused.png')
elif status_flag == '0':
# NOK label: prefer dedicated NOK SVG template, otherwise use refused image in standard layout
if os.path.exists(nok_svg):
selected_template = nok_svg
elif os.path.exists(default_svg):
selected_template = default_svg
image_path = os.path.join('conf', 'refused.png')
else:
print(f"Using SVG template: {svg_template}")
# OK label (default): prefer dedicated OK SVG template, fallback to default SVG template
if os.path.exists(ok_svg):
selected_template = ok_svg
elif os.path.exists(default_svg):
selected_template = default_svg
image_path = os.path.join('conf', 'accepted.png')
# Check for default image path
image_path = os.path.join('conf', 'accepted.png')
return generator.create_label_pdf(article, nr_art, serial, pdf_filename, image_path, svg_template)
return generator.create_label_pdf(article, nr_art, serial, pdf_filename, image_path, selected_template)
def configure_printer_quality(printer_name, width_mm=35, height_mm=25):
@@ -423,75 +434,11 @@ def print_to_printer(printer_name, file_path):
except Exception as e:
print(f"SumatraPDF error: {e}")
# Method 2: Adobe Reader silent printing
# Do not launch default PDF viewers (Adobe/Edge/etc.) as fallback.
if not printed:
adobe_path = None
for key_path in [
r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe",
r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe"
]:
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path)
adobe_path, _ = winreg.QueryValueEx(key, "")
winreg.CloseKey(key)
break
except:
pass
if adobe_path and os.path.exists(adobe_path):
try:
subprocess.run([
adobe_path,
"/t", # Print and close
file_path,
printer_name
], check=False, creationflags=subprocess.CREATE_NO_WINDOW)
print(f"Label sent to printer via Adobe Reader: {printer_name}")
printed = True
except:
pass
# Method 3: GhostScript (if installed)
if not printed:
gs_paths = [
r"C:\Program Files\gs\gs10.02.1\bin\gswin64c.exe",
r"C:\Program Files (x86)\gs\gs10.02.1\bin\gswin32c.exe",
]
# Try to find gswin in PATH
try:
gs_result = subprocess.run(['where', 'gswin64c'],
capture_output=True, text=True, check=False)
if gs_result.returncode == 0:
gs_paths.insert(0, gs_result.stdout.strip().split('\n')[0])
except:
pass
for gs_path in gs_paths:
if os.path.exists(gs_path):
try:
subprocess.run([
gs_path,
"-dNOPAUSE", "-dBATCH", "-dQUIET",
f"-sDEVICE=mswinpr2",
f"-sOutputFile=%printer%{printer_name}",
file_path
], check=False, creationflags=subprocess.CREATE_NO_WINDOW)
print(f"Label sent to printer via GhostScript: {printer_name}")
printed = True
break
except:
pass
if not printed:
# Fallback: Let user know and save PDF
print("=" * 60)
print("NOTICE: Silent PDF printing requires SumatraPDF")
print("SumatraPDF not found (should be bundled inside the app)")
print("If you built the app yourself, ensure SumatraPDF.exe is downloaded first.")
print("=" * 60)
print(f"PDF saved to: {file_path}")
print("The PDF can be printed manually.")
print("SumatraPDF not found or failed. PDF saved as backup only (no viewer launched).")
return False
return True
else:
# Non-PDF files