validate([ 'image_path' => 'required|string', 'quantity' => 'required|integer|min:1', ]); $imagePath = public_path(str_replace(url('/'), '', $request->input('image_path'))); $quantity = $request->input('quantity'); if (!file_exists($imagePath)) { Log::error("PrintController: Image file not found at {$imagePath}"); return response()->json(['error' => 'Image file not found.'], 404); } // IMPORTANT: Replace this command with one that works in your environment. // Examples: // Linux/macOS: $command = ['lpr', '-#', $quantity, $imagePath]; // Windows (assuming a shared printer named 'MyNetworkPrinter'): // $command = ['print', '/d:\\MyNetworkPrinter', $imagePath]; // You might need to install additional software or configure your system // to enable command-line printing. // For a more robust solution, consider a dedicated print server application // or a commercial print API. $command = ['echo', "Simulating print of {$quantity} copies of {$imagePath}"]; // Placeholder try { $process = new Process($command); $process->run(); if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } Log::info("PrintController: Successfully sent print command for {$imagePath} (x{$quantity})"); return response()->json(['message' => 'Print command sent successfully.']); } catch (ProcessFailedException $exception) { Log::error("PrintController: Print command failed. Error: " . $exception->getMessage()); return response()->json(['error' => 'Failed to send print command.', 'details' => $exception->getMessage()], 500); } } }