diff --git a/app/Http/Controllers/FileUploadController.php b/app/Http/Controllers/FileUploadController.php new file mode 100644 index 0000000..12b9b1d --- /dev/null +++ b/app/Http/Controllers/FileUploadController.php @@ -0,0 +1,23 @@ +validate([ + 'file' => 'required|mimes:pdf|max:2048', + ]); + + $fileName = time().'.'.$request->file->extension(); + $request->file->move(public_path('uploads'), $fileName); + + return redirect()->route("process", ['fileName' => $fileName]); + } +} diff --git a/app/Http/Controllers/LibrilladoraController.php b/app/Http/Controllers/LibrilladoraController.php index d5650b9..392e1af 100644 --- a/app/Http/Controllers/LibrilladoraController.php +++ b/app/Http/Controllers/LibrilladoraController.php @@ -8,9 +8,8 @@ use Illuminate\Http\Request; class LibrilladoraController extends Controller { // - public function process(Request $request) { - $nombre = $request->input("Archivo"); + public function process($fileName) { $lib = new Librilladora(); - $lib->prueba($nombre); + $lib->prueba($fileName); } } diff --git a/app/Models/Librilladora.php b/app/Models/Librilladora.php index 6baf4be..f1c6117 100644 --- a/app/Models/Librilladora.php +++ b/app/Models/Librilladora.php @@ -4,17 +4,28 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\File; class Librilladora extends Model { use HasFactory; - public function prueba(string $nombre) { + public function prueba(string $fileName) { $mpdf = new \Mpdf\Mpdf(); - // Write some HTML code: - $mpdf->WriteHTML($nombre); +// // Write some HTML code: +// $mpdf->WriteHTML($nombre); +// +// // Output a PDF file directly to the browser +// $mpdf->Output(); +// +// // Require composer autoload +// require_once __DIR__ . '/vendor/autoload.php'; + $file = 'uploads/'.$fileName; + $pagecount = $mpdf->SetSourceFile($file); + $tplId = $mpdf->importPage(1); + $mpdf->useTemplate($tplId); + File::delete($file); - // Output a PDF file directly to the browser $mpdf->Output(); } diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 06798e7..dcc80f5 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -45,9 +45,9 @@
-
+ @csrf - +
diff --git a/routes/web.php b/routes/web.php index 6a2a33b..d20ed3c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,7 +1,8 @@ name('process'); +Route::get('/processed/{fileName}', [LibrilladoraController::class, 'process'])->name('process'); +Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post');