Browse Source

AGREGO CAMBIO

FREDY 3 months ago
parent
commit
e0266f0b75
1 changed files with 28 additions and 21 deletions
  1. 28 21
      Back/backendP-Educativa/app/Http/Controllers/NivelExportController.php

+ 28 - 21
Back/backendP-Educativa/app/Http/Controllers/NivelExportController.php

@@ -50,6 +50,7 @@ class NivelExportController extends Controller
         }
     }
 
+
     private function aclararColor(string $hexColor, float $factor = 0.7): string
     {
         $hexColor = ltrim($hexColor, '#');
@@ -85,21 +86,21 @@ class NivelExportController extends Controller
         $sheet->getRowDimension(1)->setRowHeight(70);
 
         // Logo
-        $logoPathS3 = DB::table('personalizar')->value('PERLOGO');
-        if ($logoPathS3 && Storage::disk('s3')->exists($logoPathS3)) {
-            $tempLogoPath = tempnam(sys_get_temp_dir(), 'logo');
-            file_put_contents($tempLogoPath, Storage::disk('s3')->get($logoPathS3));
-
-            $logo = new Drawing();
-            $logo->setName('Logo');
-            $logo->setDescription('Logo de la empresa');
-            $logo->setPath($tempLogoPath);
-            $logo->setHeight(90);
-            $logo->setCoordinates('A1');
-            $logo->setOffsetX(10);
-            $logo->setOffsetY(5);
-            $logo->setWorksheet($sheet);
-        }
+       $logoPathS3 = DB::table('personalizar')->value('PERLOGO');
+if ($logoPathS3 && Storage::disk('s3')->exists($logoPathS3)) {
+    // Genera un nombre único para evitar que se quede el anterior
+    $tempLogoPath = sys_get_temp_dir() . '/logo_' . uniqid() . '.png';
+    file_put_contents($tempLogoPath, Storage::disk('s3')->get($logoPathS3));
+
+    $logo = new Drawing();
+    $logo->setName('Logo');
+    $logo->setDescription('Logo de la empresa');
+    $logo->setPath($tempLogoPath);
+    $logo->setHeight(90);
+    $logo->setCoordinates('A1');
+    $logo->setOffsetX(10);
+    $logo->setOffsetY(5);
+    $logo->setWorksheet($sheet);
 
         // Descripción
         $sheet->mergeCells('H3:J3');
@@ -156,12 +157,18 @@ class NivelExportController extends Controller
         $sheet->setAutoFilter("H4:$lastCol" . '4');
 
         $writer = new Xlsx($spreadsheet);
-        $filename = "$titulo.xlsx";
-        $tempPath = tempnam(sys_get_temp_dir(), $filename);
-        $writer->save($tempPath);
+$filename = "$titulo.xlsx";
+$tempPath = tempnam(sys_get_temp_dir(), $filename);
+$writer->save($tempPath);
 
-        return response()->download($tempPath, $filename, [
-            'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
-        ]);
+// Limpia el logo temporal después de usarlo
+if (isset($tempLogoPath) && file_exists($tempLogoPath)) {
+    unlink($tempLogoPath);
+}
+
+return response()->download($tempPath, $filename, [
+    'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
+]);
+}
     }
 }