Se sube un archivo y se devuleve la primera pagina

This commit is contained in:
Alejandro Tasistro 2021-11-27 12:51:28 -03:00
parent e0c33f23e2
commit 7161404b4c
5 changed files with 46 additions and 11 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class FileUploadController extends Controller {
public function fileUploadPost(Request $request)
{
$request->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]);
}
}

View File

@ -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);
}
}

View File

@ -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();
}

View File

@ -45,9 +45,9 @@
<div class="mt-8 bg-white dark:bg-gray-800 overflow-hidden shadow sm:rounded-lg">
<div class="p-6">
<form id="archivo" method="post" action="{{route('process')}}">
<form id="archivo" action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<input required name="Archivo" type="text" class="formulario">
<input required name="file" type="file" class="formulario">
<input type="submit" value="Cargar archivo">
</form>
</div>

View File

@ -1,7 +1,8 @@
<?php
use App\Http\Controllers\LibrilladoraController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EventoController;
use App\Http\Controllers\FileUploadController;
/*
|--------------------------------------------------------------------------
@ -18,5 +19,6 @@ Route::get('/', function () {
return view('welcome');
});
Route::post('/processed', [\App\Http\Controllers\LibrilladoraController::class, 'process'])->name('process');
Route::get('/processed/{fileName}', [LibrilladoraController::class, 'process'])->name('process');
Route::post('file-upload', [FileUploadController::class, 'fileUploadPost'])->name('file.upload.post');