Files
adaptronic_label-printer/old_code/print_pdf_ghostscript.py
NAME 6a11cf3d8d Clean repository and update .gitignore
- Remove build artifacts from git tracking (build/, dist/, __pycache__/, *.spec)
- Updated .gitignore to properly exclude generated files
- Added old_code/ documentation folder
- Updated sample_data.txt to show new 5-field format
- Exclude user-specific conf/app.conf from tracking
2026-02-13 23:41:34 +02:00

136 lines
4.6 KiB
Python

"""
Alternative PDF printing using GhostScript for superior quality.
GhostScript maintains vector quality better than SumatraPDF.
"""
import subprocess
import os
import sys
def find_ghostscript():
"""
Find GhostScript installation on Windows.
Returns:
str: Path to gswin64c.exe or gswin32c.exe, or None if not found
"""
possible_paths = [
r"C:\Program Files\gs\gs10.04.0\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.03.1\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.03.0\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.02.1\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.02.0\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.01.2\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.01.1\bin\gswin64c.exe",
r"C:\Program Files\gs\gs10.01.0\bin\gswin64c.exe",
r"C:\Program Files (x86)\gs\gs10.04.0\bin\gswin32c.exe",
r"C:\Program Files (x86)\gs\gs10.03.1\bin\gswin32c.exe",
r"C:\Program Files (x86)\gs\gs10.03.0\bin\gswin32c.exe",
]
# Check standard locations
for path in possible_paths:
if os.path.exists(path):
return path
# Search in gs directory
for program_files in [r"C:\Program Files", r"C:\Program Files (x86)"]:
gs_base = os.path.join(program_files, "gs")
if os.path.exists(gs_base):
for version_dir in os.listdir(gs_base):
for exe_name in ["gswin64c.exe", "gswin32c.exe"]:
gs_path = os.path.join(gs_base, version_dir, "bin", exe_name)
if os.path.exists(gs_path):
return gs_path
return None
def print_pdf_with_ghostscript(pdf_path, printer_name):
"""
Print PDF using GhostScript for maximum quality.
GhostScript renders PDF at high resolution and maintains vector quality.
Args:
pdf_path (str): Path to PDF file
printer_name (str): Name of the printer
Returns:
bool: True if successful
"""
gs_path = find_ghostscript()
if not gs_path:
print("GhostScript not found. Install from: https://ghostscript.com/releases/gsdnld.html")
return False
try:
# GhostScript command for high-quality printing
# -dNOPAUSE: Don't pause between pages
# -dBATCH: Exit after processing
# -sDEVICE=mswinpr2: Windows printer device (high quality)
# -dNOSAFER: Allow file operations
# -r1200: 1200 DPI resolution
# -sOutputFile: Printer name with %printer% syntax
cmd = [
gs_path,
'-dNOPAUSE',
'-dBATCH',
'-sDEVICE=mswinpr2', # Windows printer with high quality
'-dNOSAFER',
'-r1200', # 1200 DPI - matches our PDF quality
'-dTextAlphaBits=4', # Anti-aliasing for text
'-dGraphicsAlphaBits=4', # Anti-aliasing for graphics
f'-sOutputFile=%printer%{printer_name}',
pdf_path
]
print(f"Printing with GhostScript at 1200 DPI...")
result = subprocess.run(
cmd,
check=True,
capture_output=True,
text=True,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0
)
print(f"✅ Label sent to printer via GhostScript: {printer_name}")
return True
except subprocess.CalledProcessError as e:
print(f"GhostScript print failed: {e}")
if e.stderr:
print(f"Error details: {e.stderr}")
return False
except Exception as e:
print(f"Error printing with GhostScript: {e}")
return False
def check_ghostscript_available():
"""
Check if GhostScript is available.
Returns:
tuple: (available: bool, path: str or None)
"""
gs_path = find_ghostscript()
return (gs_path is not None, gs_path)
if __name__ == "__main__":
# Test GhostScript availability
available, gs_path = check_ghostscript_available()
if available:
print(f"✅ GhostScript found at: {gs_path}")
print("GhostScript is available for high-quality PDF printing")
else:
print("❌ GhostScript not found")
print("\nTo install GhostScript:")
print("1. Download from: https://ghostscript.com/releases/gsdnld.html")
print("2. Install the Windows 64-bit version")
print("3. Restart your application")
print("\nGhostScript provides superior print quality by:")
print(" • Maintaining vector graphics quality")
print(" • Rendering at full 1200 DPI")
print(" • Better text anti-aliasing")