diff --git a/app/Http/Controllers/FileUploadController.php b/app/Http/Controllers/FileUploadController.php index 12b9b1d..f5a8689 100644 --- a/app/Http/Controllers/FileUploadController.php +++ b/app/Http/Controllers/FileUploadController.php @@ -12,12 +12,13 @@ class FileUploadController extends Controller { public function fileUploadPost(Request $request) { $request->validate([ - 'file' => 'required|mimes:pdf|max:2048', + 'archivo' => 'required|mimes:pdf|max:2048', + 'bookletsize' => 'min:1' ]); - $fileName = time().'.'.$request->file->extension(); - $request->file->move(public_path('uploads'), $fileName); + $fileName = time().'.'.$request->archivo->extension(); + $request->archivo->move(public_path('uploads'), $fileName); - return redirect()->route("process", ['fileName' => $fileName]); + return redirect()->route("process", ['fileName' => $fileName, 'size' => $request->bookletsize]); } } diff --git a/app/Http/Controllers/LibrilladoraController.php b/app/Http/Controllers/LibrilladoraController.php index 392e1af..bb45a32 100644 --- a/app/Http/Controllers/LibrilladoraController.php +++ b/app/Http/Controllers/LibrilladoraController.php @@ -3,13 +3,12 @@ namespace App\Http\Controllers; use App\Models\Librilladora; -use Illuminate\Http\Request; class LibrilladoraController extends Controller { // - public function process($fileName) { + public function __invoke($fileName, $size) { $lib = new Librilladora(); - $lib->prueba($fileName); + $lib->prueba($fileName, $size); } } diff --git a/app/Models/Librilladora.php b/app/Models/Librilladora.php index 6ca5db5..a7f009a 100644 --- a/app/Models/Librilladora.php +++ b/app/Models/Librilladora.php @@ -10,24 +10,20 @@ class Librilladora extends Model { use HasFactory; - public function prueba(string $fileName) { - $mpdf = new \Mpdf\Mpdf(); - -// // 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'; + public function prueba($fileName, $size) { $file = 'uploads/'.$fileName; - $mpdf->SetSourceFile($file); - $tplId = $mpdf->importPage(1); - $mpdf->useTemplate($tplId); - File::delete($file); + + $mpdf = new \Mpdf\Mpdf(); + $pagecount = $mpdf->SetSourceFile($file); + $fin = ($size < $pagecount) ? $size : $pagecount; + for ($i = 1; $i <= $fin; $i++) { + $tplId = $mpdf->importPage($i); + $mpdf->useTemplate($tplId); + if ($i != $fin) $mpdf->AddPage(); + } $mpdf->Output(); - + File::delete($file); } } + diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index dc41c54..5873014 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -47,10 +47,10 @@
@csrf - +
- - + +
diff --git a/routes/web.php b/routes/web.php index d20ed3c..993388f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -19,6 +19,6 @@ Route::get('/', function () { return view('welcome'); }); -Route::get('/processed/{fileName}', [LibrilladoraController::class, 'process'])->name('process'); +Route::get('/processed/{fileName}/{size}', LibrilladoraController::class)->name('process'); Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post');