|
|
|
|
|
by corytheboyd
516 days ago
|
|
Is there something out there like this that is self-hosted? It’s ridiculous that this isn’t, and it’s going to become a pay-per-print model?! I don’t care about the AI nonsense, it’s a printer. I could see myself using something like this to print recipes. I’d like to avoid getting my fancy cook book dirty, but have still have recipe in the kitchen to reference. Phone works okay until you have to scroll with dirty/wet hands. |
|
// Hardcoded token for authentication $authToken = 'your-secure-token-here';
// Set the printer name (adjust to your printer's name) $printerName = 'YourPrinterName';
// Allow only POST requests if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['error' => 'Method not allowed. Use POST.']); exit; }
// Check for token in the request if (empty($_POST['token']) || $_POST['token'] !== $authToken) { http_response_code(401); echo json_encode(['error' => 'Unauthorized: Invalid token.']); exit; }
// Check if a file was uploaded if (empty($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) { http_response_code(400); echo json_encode(['error' => 'No file uploaded or file upload error.']); exit; }
// Get the uploaded file path $uploadedFile = $_FILES['file']['tmp_name']; $fileName = basename($_FILES['file']['name']);
// Use lp command to print the file $command = escapeshellcmd("lp -d " . escapeshellarg($printerName) . " " . escapeshellarg($uploadedFile)); exec($command, $output, $status);
// Check the status of the command if ($status === 0) { http_response_code(200); echo json_encode(['success' => "File '$fileName' sent to printer '$printerName'."]); } else { http_response_code(500); echo json_encode(['error' => 'Failed to print the file.']); }