Fix printing: landscape orientation + high quality DPI via win32print before SumatraPDF job
This commit is contained in:
@@ -394,10 +394,12 @@ def print_to_printer(printer_name, file_path):
|
||||
bundled_sumatra = os.path.join(sys._MEIPASS, 'SumatraPDF.exe')
|
||||
sumatra_paths.append(bundled_sumatra)
|
||||
|
||||
# Also check app directory for external version
|
||||
# Check app directory and conf subfolder for external version
|
||||
app_dir = os.path.dirname(sys.executable)
|
||||
sumatra_paths.append(os.path.join(app_dir, "SumatraPDF", "SumatraPDF.exe"))
|
||||
sumatra_paths.append(os.path.join(app_dir, "SumatraPDF.exe"))
|
||||
sumatra_paths.append(os.path.join(app_dir, "conf", "SumatraPDF.exe")) # conf subfolder next to exe
|
||||
sumatra_paths.append(os.path.join(os.getcwd(), "conf", "SumatraPDF.exe")) # conf relative to cwd
|
||||
else:
|
||||
# Running as script - check local folders
|
||||
app_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
@@ -411,26 +413,49 @@ def print_to_printer(printer_name, file_path):
|
||||
r"C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe",
|
||||
])
|
||||
|
||||
# Set printer to highest quality / landscape before sending job
|
||||
if WIN32_AVAILABLE:
|
||||
try:
|
||||
import win32print
|
||||
hPrinter = win32print.OpenPrinter(printer_name)
|
||||
try:
|
||||
props = win32print.GetPrinter(hPrinter, 2)
|
||||
dm = props['pDevMode']
|
||||
if dm:
|
||||
dm.Orientation = 2 # DMORIENT_LANDSCAPE
|
||||
dm.PrintQuality = -4 # DMRES_HIGH (highest DPI)
|
||||
dm.YResolution = -4 # also set Y res
|
||||
win32print.SetPrinter(hPrinter, 2, props, 0)
|
||||
print("Printer set to landscape + high quality")
|
||||
finally:
|
||||
win32print.ClosePrinter(hPrinter)
|
||||
except Exception as qe:
|
||||
print(f"Could not configure printer quality/orientation: {qe}")
|
||||
|
||||
for sumatra_path in sumatra_paths:
|
||||
if os.path.exists(sumatra_path):
|
||||
try:
|
||||
# Use noscale with paper size specification for thermal printers
|
||||
# Format: "noscale,paper=<size>" where paper size matches PDF (35mm x 25mm)
|
||||
# SumatraPDF will use the PDF's page size when noscale is used
|
||||
subprocess.run([
|
||||
print(f"Using SumatraPDF: {sumatra_path}")
|
||||
print(f"Sending to printer: {printer_name}")
|
||||
result = subprocess.run([
|
||||
sumatra_path,
|
||||
"-print-to",
|
||||
printer_name,
|
||||
file_path,
|
||||
"-print-settings",
|
||||
"noscale", # Preserve exact PDF page dimensions
|
||||
"noscale,landscape", # preserve dimensions + force landscape
|
||||
"-silent",
|
||||
"-exit-when-done"
|
||||
], check=False, creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
print(f"Label sent to printer via SumatraPDF: {printer_name}")
|
||||
print(f"Note: Printer '{printer_name}' should be configured for 35mm x 25mm labels")
|
||||
printed = True
|
||||
break
|
||||
], check=False, creationflags=subprocess.CREATE_NO_WINDOW,
|
||||
capture_output=True, text=True, timeout=30)
|
||||
if result.returncode != 0:
|
||||
print(f"SumatraPDF returned code {result.returncode}")
|
||||
if result.stderr:
|
||||
print(f"SumatraPDF stderr: {result.stderr.strip()}")
|
||||
else:
|
||||
print(f"Label sent to printer via SumatraPDF: {printer_name}")
|
||||
printed = True
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"SumatraPDF error: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user