Devuelve las primeras n paginas del documento pasado

This commit is contained in:
Alejandro Tasistro 2021-11-28 20:13:58 -03:00
parent bbea85bbd4
commit 20c5ac4731
5 changed files with 23 additions and 27 deletions

View File

@ -12,12 +12,13 @@ class FileUploadController extends Controller {
public function fileUploadPost(Request $request) public function fileUploadPost(Request $request)
{ {
$request->validate([ $request->validate([
'file' => 'required|mimes:pdf|max:2048', 'archivo' => 'required|mimes:pdf|max:2048',
'bookletsize' => 'min:1'
]); ]);
$fileName = time().'.'.$request->file->extension(); $fileName = time().'.'.$request->archivo->extension();
$request->file->move(public_path('uploads'), $fileName); $request->archivo->move(public_path('uploads'), $fileName);
return redirect()->route("process", ['fileName' => $fileName]); return redirect()->route("process", ['fileName' => $fileName, 'size' => $request->bookletsize]);
} }
} }

View File

@ -3,13 +3,12 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Librilladora; use App\Models\Librilladora;
use Illuminate\Http\Request;
class LibrilladoraController extends Controller class LibrilladoraController extends Controller
{ {
// //
public function process($fileName) { public function __invoke($fileName, $size) {
$lib = new Librilladora(); $lib = new Librilladora();
$lib->prueba($fileName); $lib->prueba($fileName, $size);
} }
} }

View File

@ -10,24 +10,20 @@ class Librilladora extends Model {
use HasFactory; use HasFactory;
public function prueba(string $fileName) { public function prueba($fileName, $size) {
$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';
$file = 'uploads/'.$fileName; $file = 'uploads/'.$fileName;
$mpdf->SetSourceFile($file);
$tplId = $mpdf->importPage(1); $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); $mpdf->useTemplate($tplId);
File::delete($file); if ($i != $fin) $mpdf->AddPage();
}
$mpdf->Output(); $mpdf->Output();
File::delete($file);
}
}
}
}

View File

@ -47,10 +47,10 @@
<div class="p-6"> <div class="p-6">
<form id="archivo" action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data"> <form id="archivo" action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf @csrf
<input required name="file" type="file" class="formulario"> <input required name="archivo" type="file" class="formulario">
<div class="labeled-input"> <div class="labeled-input">
<label for="booklet-size">Tamaño de los librillos</label> <label for="bookletsize">Tamaño de los librillos</label>
<input name="booklet-size" type="number" min=0 value=20 class="formulario numero-input"> <input name="bookletsize" type="number" min=1 value=20 class="formulario numero-input">
</div> </div>
<input type="submit" value="Cargar archivo"> <input type="submit" value="Cargar archivo">
</form> </form>

View File

@ -19,6 +19,6 @@ Route::get('/', function () {
return view('welcome'); 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'); Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post');