55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
from typing import Optional, Union
|
|
import cups
|
|
from pathlib import Path
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class RegularPrinterService:
|
|
def __init__(self, printer_name: Optional[str] = None):
|
|
"""Initialize the regular printer service.
|
|
|
|
Args:
|
|
printer_name: Name of the printer to use. If None, will use default printer.
|
|
"""
|
|
self.printer_name = printer_name
|
|
self.conn = cups.Connection()
|
|
|
|
async def print_file(self, file_path: Union[str, Path]) -> bool:
|
|
"""Print a PDF or PNG file to the regular printer.
|
|
|
|
Args:
|
|
file_path: Path to the PDF or PNG file
|
|
|
|
Returns:
|
|
bool: True if print was successful, False otherwise
|
|
"""
|
|
try:
|
|
if file_path is None:
|
|
logger.error("No file path provided")
|
|
return False
|
|
|
|
file_path = Path(file_path)
|
|
if not file_path.exists():
|
|
logger.error(f"File not found: {file_path}")
|
|
return False
|
|
|
|
# Get the printer
|
|
printer = self.printer_name or self.conn.getDefault()
|
|
if not printer:
|
|
logger.error("No default printer found")
|
|
return False
|
|
|
|
# Submit the print job
|
|
job_id = self.conn.printFile(
|
|
printer,
|
|
str(file_path),
|
|
f"{file_path.suffix.upper()} Print",
|
|
{}
|
|
)
|
|
logger.info(f"Print job {job_id} submitted to printer {printer}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error printing file {file_path}: {str(e)}")
|
|
return False |