get("RapportoFile({$id})", []); file_put_contents( $debugDir . "rapporto_file_{$id}.json", json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) ); /** * Provo vari nomi campo possibili per il contenuto PDF. * Dopo il primo test potremo adattarlo al nome esatto. */ $possibleContentFields = [ 'File', 'file', 'Content', 'content', 'FileContent', 'fileContent', 'Contenuto', 'contenuto', 'Bytes', 'bytes' ]; $base64 = null; foreach ($possibleContentFields as $field) { if (!empty($data[$field])) { $base64 = $data[$field]; break; } } if (!$base64) { header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'success' => false, 'message' => 'Record RapportoFile recuperato, ma non ho trovato un campo base64 del PDF.', 'hint' => 'Apri logs/lims_get_commessa/rapporto_file_' . $id . '.json e controlla il nome reale del campo file.', 'data' => $data ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); exit; } /** * Se il contenuto ha prefisso data URI, lo pulisco. */ if (strpos($base64, 'base64,') !== false) { $parts = explode('base64,', $base64); $base64 = end($parts); } $pdfBinary = base64_decode($base64, true); if ($pdfBinary === false) { throw new Exception("Il contenuto trovato non รจ un base64 valido"); } $filename = $data['NomeFile'] ?? $data['nomeFile'] ?? $data['FileName'] ?? $data['fileName'] ?? "rapporto_{$id}.pdf"; if (!str_ends_with(strtolower($filename), '.pdf')) { $filename .= '.pdf'; } header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="' . basename($filename) . '"'); header('Content-Length: ' . strlen($pdfBinary)); echo $pdfBinary; } catch (Exception $e) { http_response_code(500); header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'success' => false, 'error' => $e->getMessage() ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); }