diff --git a/.directory b/.directory index 14ff249..c1e0531 100644 --- a/.directory +++ b/.directory @@ -1,5 +1,5 @@ [Dolphin] HeaderColumnWidths=372,72,103 -Timestamp=2021,11,27,10,20,55 +Timestamp=2021,11,28,21,9,10 Version=4 ViewMode=1 diff --git a/app/Http/Controllers/LibrilladoraController.php b/app/Http/Controllers/LibrilladoraController.php index bb45a32..34531fa 100644 --- a/app/Http/Controllers/LibrilladoraController.php +++ b/app/Http/Controllers/LibrilladoraController.php @@ -9,6 +9,6 @@ class LibrilladoraController extends Controller // public function __invoke($fileName, $size) { $lib = new Librilladora(); - $lib->prueba($fileName, $size); + $lib->procesar($fileName, $size); } } diff --git a/app/Models/Librilladora.php b/app/Models/Librilladora.php index a7f009a..fbbb374 100644 --- a/app/Models/Librilladora.php +++ b/app/Models/Librilladora.php @@ -5,25 +5,144 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\File; +use Symfony\Component\Console\Output\Output; + class Librilladora extends Model { use HasFactory; - - public function prueba($fileName, $size) { + public function procesar($fileName, $size) { $file = 'uploads/'.$fileName; - $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 = new \Mpdf\Mpdf([ + 'format' => 'A4-L', + 'margin_left' => 0, + 'margin_right' => 0, + 'margin_top' => 0, + 'margin_bottom' => 0, + 'margin_header' => 0, + 'margin_footer' => 0, + ]); + + // DIVIDIR EL DOCUMENTO Y BOOKLETEAR LAS DIVISIONES + $pageCount = $mpdf->SetSourceFile($file); + $cantBooklets = ($pageCount / $size); + for ($i = 0; $i < $cantBooklets; $i++) { + $comienzo = ($size*$i)+1; + $fin = ($size*($i+1) < $pageCount) ? $size*($i+1) : $pageCount; + $docAux = new \Mpdf\Mpdf(); + $docAux->SetSourceFile($file); + for ($j = $comienzo; $j <= $fin; $j++) { + $tplId = $docAux->importPage($j); + $docAux->useTemplate($tplId); + if ($j != $fin) $docAux->AddPage(); + } + $docAux->Output('uploads/'.$i."-".$fileName, \Mpdf\Output\Destination::FILE); + $this->librillar('uploads/', $i."-".$fileName); } - $mpdf->Output(); - File::delete($file); + // MERGEAR LAS DIVISIONES + $fileNames = array(); + for ($i = 0; $i < $cantBooklets; $i++) + array_push($fileNames, 'uploads/librillo-'.$i."-".$fileName); + $this->mergePDFFiles($fileNames); + } + + + private function librillar($dir, $filename) { + $mpdf = new \Mpdf\Mpdf([ + 'format' => 'A4-L', + 'margin_left' => 0, + 'margin_right' => 0, + 'margin_top' => 0, + 'margin_bottom' => 0, + 'margin_header' => 0, + 'margin_footer' => 0, + ]); + + $pw = $mpdf->w / 2; + $ph = $mpdf->h; + + $mpdf->SetDisplayMode('fullpage'); + + $pagecount = $mpdf->SetSourceFile($dir.$filename); + $pp = $this->GetBookletPages($pagecount); + + foreach ($pp as $v) { + + $mpdf->AddPage(); + + if ($v[0] > 0 && $v[0] <= $pagecount) { + $tplIdx = $mpdf->ImportPage($v[0]); //, 0, 0, $ow, $oh + $mpdf->UseTemplate($tplIdx, 0, 0, $pw, $ph); + } + + if ($v[1] > 0 && $v[1] <= $pagecount) { + $tplIdx = $mpdf->ImportPage($v[1]); //, 0, 0, $ow, $oh + $mpdf->UseTemplate($tplIdx, $pw, 0, $pw, $ph); + } + } + + $mpdf->Output($dir.'librillo-'.$filename, \Mpdf\Output\Destination::FILE); + } + + + private function GetBookletPages($np, $backcover = true) { + $lastpage = $np; + $np = 4 * ceil($np / 4); + $pp = []; + + for ($i = 1; $i <= $np / 2; $i++) { + + $p1 = $np - $i + 1; + + if ($backcover) { + if ($i == 1) { + $p1 = $lastpage; + } elseif ($p1 >= $lastpage) { + $p1 = 0; + } + } + + $pp[] = ($i % 2 == 1) ? + [ $i, $p1 ] : [ $p1, $i ]; + } + + return $pp; + } + + function mergePDFFiles(Array $filenames) { + $mpdf = new \Mpdf\Mpdf([ + 'format' => 'A4-L', + 'margin_left' => 0, + 'margin_right' => 0, + 'margin_top' => 0, + 'margin_bottom' => 0, + 'margin_header' => 0, + 'margin_footer' => 0, + ]); + + if ($filenames) { + + $filesTotal = sizeof($filenames); + $fileNumber = 1; + + foreach ($filenames as $fileName) { + if (file_exists($fileName)) { + $pagesInFile = $mpdf->SetSourceFile($fileName); + for ($i = 1; $i <= $pagesInFile; $i++) { + $tplId = $mpdf->ImportPage($i); // in mPdf v8 should be 'importPage($i)' + $mpdf->UseTemplate($tplId); + if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) { + $mpdf->WriteHTML(''); + } + } + } + $fileNumber++; + } + + $mpdf->Output(); + } } } diff --git a/public/uploads/0-1638146076.pdf b/public/uploads/0-1638146076.pdf new file mode 100644 index 0000000..6b54e78 Binary files /dev/null and b/public/uploads/0-1638146076.pdf differ diff --git a/public/uploads/1-1638146076.pdf b/public/uploads/1-1638146076.pdf new file mode 100644 index 0000000..056e158 Binary files /dev/null and b/public/uploads/1-1638146076.pdf differ diff --git a/public/uploads/1638146076.pdf b/public/uploads/1638146076.pdf new file mode 100644 index 0000000..d23dc05 Binary files /dev/null and b/public/uploads/1638146076.pdf differ diff --git a/public/uploads/2-1638146076.pdf b/public/uploads/2-1638146076.pdf new file mode 100644 index 0000000..a5f9ece Binary files /dev/null and b/public/uploads/2-1638146076.pdf differ diff --git a/public/uploads/3-1638146076.pdf b/public/uploads/3-1638146076.pdf new file mode 100644 index 0000000..160f081 Binary files /dev/null and b/public/uploads/3-1638146076.pdf differ diff --git a/public/uploads/4-1638146076.pdf b/public/uploads/4-1638146076.pdf new file mode 100644 index 0000000..7e6c4fd Binary files /dev/null and b/public/uploads/4-1638146076.pdf differ diff --git a/public/uploads/librillo-0-1638146076.pdf b/public/uploads/librillo-0-1638146076.pdf new file mode 100644 index 0000000..b5f0128 Binary files /dev/null and b/public/uploads/librillo-0-1638146076.pdf differ diff --git a/public/uploads/librillo-1-1638146076.pdf b/public/uploads/librillo-1-1638146076.pdf new file mode 100644 index 0000000..94d803a Binary files /dev/null and b/public/uploads/librillo-1-1638146076.pdf differ diff --git a/public/uploads/librillo-2-1638146076.pdf b/public/uploads/librillo-2-1638146076.pdf new file mode 100644 index 0000000..1adc9f1 Binary files /dev/null and b/public/uploads/librillo-2-1638146076.pdf differ diff --git a/public/uploads/librillo-3-1638146076.pdf b/public/uploads/librillo-3-1638146076.pdf new file mode 100644 index 0000000..c65f788 Binary files /dev/null and b/public/uploads/librillo-3-1638146076.pdf differ diff --git a/public/uploads/librillo-4-1638146076.pdf b/public/uploads/librillo-4-1638146076.pdf new file mode 100644 index 0000000..2ba5508 Binary files /dev/null and b/public/uploads/librillo-4-1638146076.pdf differ