diff --git a/app/Http/Controllers/Repuve/InscriptionController.php b/app/Http/Controllers/Repuve/InscriptionController.php index 7ecf4d5..b165234 100644 --- a/app/Http/Controllers/Repuve/InscriptionController.php +++ b/app/Http/Controllers/Repuve/InscriptionController.php @@ -133,7 +133,7 @@ public function vehicleInscription(VehicleStoreRequest $request) foreach ($files as $index => $file) { $fileName = uniqid() . '_' . time() . '_' . $file->getClientOriginalName(); - $path = $file->storeAs('records', $fileName, 'public'); + $path = $file->storeAs('/', $fileName, 'records'); $md5 = md5_file($file->getRealPath()); $fileRecord = File::create([ @@ -350,7 +350,6 @@ public function searchRecord(Request $request) 'folio' => 'nullable|string', 'placa' => 'nullable|string', 'niv' => 'nullable|string', - 'per_page' => 'nullable|integer|min:1|max:20', ], [ 'required_without_all' => 'Debe proporcionar al menos uno de los siguientes: folio, placa o NIV.' ]); @@ -437,9 +436,9 @@ public function searchRecord(Request $request) private function getVehicle(): array { return [ - "ANIO_PLACA" => "2020", - "PLACA" => "WNU700B", - "NO_SERIE" => "LSGHD52H0ND032457", + "ANIO_PLACA" => "2021", + "PLACA" => "WNU700A", + "NO_SERIE" => "LSGHD52H0ND032458", "RFC" => "GME111116GJA", "FOLIO" => "EXP-2025-201030", "VIGENCIA" => "2025", @@ -459,13 +458,13 @@ private function getVehicle(): array "LINEA" => "AVEO", "SUBLINEA" => "PAQ. \"A\" LS", "MODELO" => 2022, - "NUMERO_SERIE" => "LSGHD52H0ND032457", + "NUMERO_SERIE" => "LSGHD52H0ND032458", "NUMERO_MOTOR" => "H. EN WUHANLL,SGM", - "DESCRIPCION_ORIGEN" => "IMPORTADO", + "DESCRIPCION_ORIGEN" => "NACIONAL", "COLOR" => "BLANCO", "CODIGO_POSTAL" => "86179", - "SERIE_FOLIO" => "D3962243", - "SFOLIO" => "3962243" + "SERIE_FOLIO" => "D3962242", + "SFOLIO" => "3962242" ]; } diff --git a/app/Http/Controllers/Repuve/RecordController.php b/app/Http/Controllers/Repuve/RecordController.php index 0039928..d234e15 100644 --- a/app/Http/Controllers/Repuve/RecordController.php +++ b/app/Http/Controllers/Repuve/RecordController.php @@ -5,6 +5,9 @@ use App\Http\Controllers\Controller; use Barryvdh\DomPDF\Facade\Pdf; use App\Models\Record; +use Illuminate\Support\Facades\Storage; +use Notsoweb\ApiResponse\Enums\ApiResponse; +use Codedge\Fpdf\Fpdf\Fpdf; class RecordController extends Controller { @@ -53,6 +56,126 @@ public function generatePdfConstancia($id) return $pdf->stream('constancia-inscripcion' . $id . '.pdf'); } + /** + * Generar PDF con las imágenes + */ + public function generatePdfImages($id) + { + try { + // Obtener el record con sus archivos + $record = Record::with(['vehicle.owner', 'files'])->findOrFail($id); + // Validar que tenga archivos + if ($record->files->isEmpty()) { + return ApiResponse::NOT_FOUND->response([ + 'message' => 'El expediente no tiene imágenes adjuntas.', + 'record_id' => $id, + ]); + } + + // Crear instancia de FPDF + $pdf = new Fpdf('P', 'mm', 'A4'); + $pdf->SetAutoPageBreak(false); + $pdf->SetMargins(10, 10, 10); + + $totalImages = $record->files->count(); + $currentImage = 0; + + foreach ($record->files as $file) { + $currentImage++; + + // Buscar archivo en disk 'records' + $diskRecords = Storage::disk('records'); + + $fileContent = null; + if ($diskRecords->exists($file->path)) { + $fileContent = $diskRecords->get($file->path); + } + + // Si no se encontró el archivo, continuar + if ($fileContent === null) { + continue; + } + + // Agregar nueva página + $pdf->AddPage(); + + // Header con folio + $pdf->SetFillColor(44, 62, 80); + $pdf->Rect(0, 0, 210, 20, 'F'); + $pdf->SetTextColor(255, 255, 255); + $pdf->SetFont('Arial', 'B', 14); + $pdf->SetXY(10, 7); + $pdf->Cell(0, 6, 'FOLIO: ' . $record->folio, 0, 1, 'L'); + + // Obtener ruta temporal del archivo + $tempPath = tempnam(sys_get_temp_dir(), 'pdf_img_'); + file_put_contents($tempPath, $fileContent); + + // Obtener dimensiones de la imagen + $imageInfo = getimagesize($tempPath); + + if ($imageInfo !== false) { + list($originalWidth, $originalHeight) = $imageInfo; + $imageType = $imageInfo[2]; + + $availableWidth = 190; // 210mm - 20mm márgenes + $availableHeight = 247; // 297mm - 20mm header - 20mm footer - 10mm márgenes + + // Calcular dimensiones manteniendo proporción + $ratio = min($availableWidth / $originalWidth, $availableHeight / $originalHeight); + $newWidth = $originalWidth * $ratio; + $newHeight = $originalHeight * $ratio; + + // Centrar imagen + $x = (210 - $newWidth) / 2; + $y = 25 + (($availableHeight - $newHeight) / 2); + + // Determinar tipo de imagen + $imageExtension = ''; + switch ($imageType) { + case IMAGETYPE_JPEG: + $imageExtension = 'JPEG'; + break; + case IMAGETYPE_PNG: + $imageExtension = 'PNG'; + break; + default: + // Si no es un formato soportado, continuar + unlink($tempPath); + continue 2; + } + + // Insertar imagen + $pdf->Image($tempPath, $x, $y, $newWidth, $newHeight, $imageExtension); + } + + // Limpiar archivo temporal + unlink($tempPath); + + } + + // Verificar que se agregaron páginas + if ($pdf->PageNo() == 0) { + return ApiResponse::NOT_FOUND->response([ + 'message' => 'No se pudieron procesar las imágenes del expediente.', + 'record_id' => $id, + ]); + } + + // Generar PDF + $pdfContent = $pdf->Output('S'); + + return response($pdfContent, 200) + ->header('Content-Type', 'application/pdf') + ->header('Content-Disposition', 'inline; filename="expediente-imagenes-' . $record->folio . '.pdf"'); + + } catch (\Exception $e) { + return ApiResponse::INTERNAL_ERROR->response([ + 'message' => 'Error al generar el PDF de imágenes', + 'error' => $e->getMessage(), + ]); + } + } } diff --git a/app/Models/File.php b/app/Models/File.php index e9c825d..df4a52d 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -31,7 +31,7 @@ public function record() public function url(): Attribute { return Attribute::make( - get: fn () => Storage::url($this->path), + get: fn () => Storage::disk('records')->url($this->path), ); } } diff --git a/composer.json b/composer.json index 521893a..9fb4620 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "require": { "php": "^8.3", "barryvdh/laravel-dompdf": "*", + "codedge/laravel-fpdf": "*", "laravel/framework": "^12.0", "laravel/passport": "^12.4", "laravel/pulse": "^1.4", @@ -15,6 +16,7 @@ "laravel/tinker": "^2.10", "milon/barcode": "^12.0", "notsoweb/laravel-core": "dev-main", + "setasign/fpdf": "^1.8", "spatie/laravel-permission": "^6.16", "tightenco/ziggy": "^2.5" }, diff --git a/composer.lock b/composer.lock index 4fc2bc5..f7cac14 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2e95bbdb182abae36557342b05654934", + "content-hash": "ddec2d00c4e2c1ad362c8e28c77eb079", "packages": [ { "name": "barryvdh/laravel-dompdf", @@ -342,6 +342,61 @@ ], "time": "2025-01-03T16:18:33+00:00" }, + { + "name": "codedge/laravel-fpdf", + "version": "1.12.3", + "source": { + "type": "git", + "url": "https://codeberg.org/codedge/laravel-fpdf", + "reference": "98b01bc4c61f2ec70a5ec120106507961481d593" + }, + "require": { + "illuminate/support": "^11.0 || ^12.0", + "php": "~8.3 || ~8.4" + }, + "require-dev": { + "ergebnis/composer-normalize": "^2.39", + "orchestra/testbench": "^9.0 || ^10.0", + "phpunit/phpunit": "^10.5 || ^11.5.3" + }, + "type": "library", + "extra": { + "laravel": { + "aliases": { + "Fpdf": "Codedge\\Fpdf\\Facades\\Fpdf" + }, + "providers": [ + "Codedge\\Fpdf\\FpdfServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Codedge\\Fpdf\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Holger Lösken", + "email": "holger.loesken@codedge.de" + } + ], + "description": "Laravel package to include Fpdf. It ships with Fpdf 1.86.", + "keywords": [ + "fpdf", + "laravel", + "pdf" + ], + "support": { + "issues": "https://github.com/codedge/laravel-fpdf/issues", + "source": "https://github.com/codedge/laravel-fpdf" + }, + "time": "2025-03-14T15:54:58+00:00" + }, { "name": "defuse/php-encryption", "version": "v2.4.0", @@ -5816,6 +5871,52 @@ }, "time": "2025-07-11T13:20:48+00:00" }, + { + "name": "setasign/fpdf", + "version": "1.8.6", + "source": { + "type": "git", + "url": "https://github.com/Setasign/FPDF.git", + "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Setasign/FPDF/zipball/0838e0ee4925716fcbbc50ad9e1799b5edfae0a0", + "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0", + "shasum": "" + }, + "require": { + "ext-gd": "*", + "ext-zlib": "*" + }, + "type": "library", + "autoload": { + "classmap": [ + "fpdf.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Olivier Plathey", + "email": "oliver@fpdf.org", + "homepage": "http://fpdf.org/" + } + ], + "description": "FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.", + "homepage": "http://www.fpdf.org", + "keywords": [ + "fpdf", + "pdf" + ], + "support": { + "source": "https://github.com/Setasign/FPDF/tree/1.8.6" + }, + "time": "2023-06-26T14:44:25+00:00" + }, { "name": "spatie/laravel-permission", "version": "6.21.0", diff --git a/config/filesystems.php b/config/filesystems.php index 471474c..6bba8bb 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -53,6 +53,14 @@ 'throw' => false, ], + 'records' => [ + 'driver' => 'local', + 'root' => storage_path('app/records'), + 'url' => env('APP_URL').'/records', + 'visibility' => 'public', + 'throw' => false, + ], + 'images' => [ 'driver' => 'local', 'root' => storage_path('app/images'), @@ -89,5 +97,6 @@ public_path('storage') => storage_path('app/public'), public_path('profile') => storage_path('app/profile'), public_path('images') => storage_path('app/images'), + public_path('records') => storage_path('app/records'), ], ]; diff --git a/public/records b/public/records new file mode 120000 index 0000000..95b0c5a --- /dev/null +++ b/public/records @@ -0,0 +1 @@ +/var/www/repuve-v1/storage/app/records \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 061bf33..c11bb28 100644 --- a/routes/api.php +++ b/routes/api.php @@ -34,6 +34,7 @@ Route::get('expediente/{id}/pdf', [RecordController::class, 'generatePdf']); Route::get('expediente/{id}/pdfVerificacion', [RecordController::class, 'generatePdfVerification']); Route::get('expediente/{id}/pdfConstancia', [RecordController::class, 'generatePdfConstancia']); + Route::get('expediente/{id}/pdfImagenes', [RecordController::class, 'generatePdfImages']); //Rutas de Actualización Route::put('actualizar-vehiculo', [UpdateController::class, 'vehicleUpdate']);