pdf backup

This commit is contained in:
NAME
2026-02-12 22:43:26 +02:00
parent 8954135f93
commit 3b23f89cf0
2 changed files with 30 additions and 15 deletions

View File

@@ -346,19 +346,21 @@ def print_to_printer(printer_name, file_path):
def print_label_standalone(value, printer, preview=0, use_pdf=True):
"""
Print a label with the specified text on the specified printer.
Always generates a PDF backup in pdf_backup and prints that PDF.
Args:
value (str): The text to print on the label
printer (str): The name of the printer to use
preview (int): 0 = no preview, 1-3 = 3s preview, >3 = 5s preview
use_pdf (bool): True to use PDF (recommended for quality), False for PNG
use_pdf (bool): False to also generate a PNG if PDF generation fails
Returns:
bool: True if printing was successful, False otherwise
"""
# For tracking if file was created
# Track generated files
file_created = False
temp_file = None
pdf_file = None
try:
# Debug output
@@ -366,19 +368,32 @@ def print_label_standalone(value, printer, preview=0, use_pdf=True):
print(f"Preview type: {type(preview)}")
print(f"Using format: {'PDF' if use_pdf else 'PNG'}")
# Create label in selected format
if use_pdf:
temp_file = create_label_pdf(value)
print(f"PDF label created: {temp_file}")
print(f"PDF backup saved to: {temp_file}")
# Always generate a PDF backup and print that PDF for verification
try:
pdf_file = create_label_pdf(value)
if pdf_file and os.path.exists(pdf_file):
print(f"PDF label created: {pdf_file}")
print(f"PDF backup saved to: {pdf_file}")
else:
print("PDF generation returned no file path")
except Exception as pdf_err:
print(f"PDF generation failed: {pdf_err}")
# Optionally also create the label image (PNG)
if not pdf_file or not os.path.exists(pdf_file):
if not use_pdf:
label_img = create_label_image(value)
temp_file = 'final_label.png'
label_img.save(temp_file)
print(f"PNG label created: {temp_file}")
else:
# Create the label image (PNG)
label_img = create_label_image(value)
temp_file = 'final_label.png'
label_img.save(temp_file)
print(f"PNG label created: {temp_file}")
temp_file = pdf_file
file_created = True
if not temp_file or not os.path.exists(temp_file):
print("No label file created for printing")
return False
# Convert preview to int if it's a string
if isinstance(preview, str):
@@ -417,8 +432,8 @@ def print_label_standalone(value, printer, preview=0, use_pdf=True):
finally:
# This block always executes, ensuring cleanup
if use_pdf:
print(f"Cleanup complete - PDF backup saved to pdf_backup folder")
if pdf_file and os.path.exists(pdf_file):
print("Cleanup complete - PDF backup saved to pdf_backup folder")
else:
print("Cleanup complete - label file retained for reference")

View File

@@ -1 +1 @@
COM-2024-002;ART-67890;SN-20260212-TEST
COM-2024-002;ART-67890;SN-20260212