fixed migrations, changed settings to global settings, changed image list to have a "delete all" button instead of "create", fixed printing, added imagick for printing.

This commit is contained in:
2025-08-26 10:39:18 +02:00
parent 44dd0f2867
commit 9b1f6a479f
69 changed files with 17232 additions and 1263 deletions

View File

@@ -4,12 +4,11 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use App\Services\PrinterService;
class PrintController extends Controller
{
public function printImage(Request $request)
public function printImage(Request $request, PrinterService $printerService)
{
$request->validate([
'image_path' => 'required|string',
@@ -18,36 +17,32 @@ class PrintController extends Controller
$imagePath = public_path(str_replace(url('/'), '', $request->input('image_path')));
$quantity = $request->input('quantity');
// Retrieve printer name from global settings using standard Eloquent
$printerName = \App\Models\Setting::where('key', 'selected_printer')->value('value');
if (!$printerName) {
Log::error("PrintController: Default printer name not found in settings.");
return response()->json(['error' => 'Default printer not configured.'], 500);
}
if (!$printerName) {
Log::error("PrintController: Default printer name not found in settings.");
return response()->json(['error' => 'Default printer not configured.'], 500);
}
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
$printSuccess = $printerService->printImage($imagePath, $printerName, $quantity);
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})");
if ($printSuccess) {
Log::info("PrintController: Successfully sent print command for {$imagePath} (x{$quantity}) to {$printerName}");
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);
} else {
Log::error("PrintController: Failed to send print command for {$imagePath} (x{$quantity}) to {$printerName}");
return response()->json(['error' => 'Failed to send print command.'], 500);
}
}
}