Funcionando
This commit is contained in:
parent
20c5ac4731
commit
9e10c185ce
14 changed files with 132 additions and 13 deletions
|
@ -1,5 +1,5 @@
|
||||||
[Dolphin]
|
[Dolphin]
|
||||||
HeaderColumnWidths=372,72,103
|
HeaderColumnWidths=372,72,103
|
||||||
Timestamp=2021,11,27,10,20,55
|
Timestamp=2021,11,28,21,9,10
|
||||||
Version=4
|
Version=4
|
||||||
ViewMode=1
|
ViewMode=1
|
||||||
|
|
|
@ -9,6 +9,6 @@ class LibrilladoraController extends Controller
|
||||||
//
|
//
|
||||||
public function __invoke($fileName, $size) {
|
public function __invoke($fileName, $size) {
|
||||||
$lib = new Librilladora();
|
$lib = new Librilladora();
|
||||||
$lib->prueba($fileName, $size);
|
$lib->procesar($fileName, $size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,25 +5,144 @@ namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Symfony\Component\Console\Output\Output;
|
||||||
|
|
||||||
|
|
||||||
class Librilladora extends Model {
|
class Librilladora extends Model {
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
public function procesar($fileName, $size) {
|
||||||
public function prueba($fileName, $size) {
|
|
||||||
$file = 'uploads/'.$fileName;
|
$file = 'uploads/'.$fileName;
|
||||||
|
|
||||||
$mpdf = new \Mpdf\Mpdf();
|
$mpdf = new \Mpdf\Mpdf([
|
||||||
$pagecount = $mpdf->SetSourceFile($file);
|
'format' => 'A4-L',
|
||||||
$fin = ($size < $pagecount) ? $size : $pagecount;
|
'margin_left' => 0,
|
||||||
for ($i = 1; $i <= $fin; $i++) {
|
'margin_right' => 0,
|
||||||
$tplId = $mpdf->importPage($i);
|
'margin_top' => 0,
|
||||||
$mpdf->useTemplate($tplId);
|
'margin_bottom' => 0,
|
||||||
if ($i != $fin) $mpdf->AddPage();
|
'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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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('<pagebreak />');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$fileNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$mpdf->Output();
|
$mpdf->Output();
|
||||||
File::delete($file);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
BIN
public/uploads/0-1638146076.pdf
Normal file
BIN
public/uploads/0-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/1-1638146076.pdf
Normal file
BIN
public/uploads/1-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/1638146076.pdf
Normal file
BIN
public/uploads/1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/2-1638146076.pdf
Normal file
BIN
public/uploads/2-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/3-1638146076.pdf
Normal file
BIN
public/uploads/3-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/4-1638146076.pdf
Normal file
BIN
public/uploads/4-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/librillo-0-1638146076.pdf
Normal file
BIN
public/uploads/librillo-0-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/librillo-1-1638146076.pdf
Normal file
BIN
public/uploads/librillo-1-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/librillo-2-1638146076.pdf
Normal file
BIN
public/uploads/librillo-2-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/librillo-3-1638146076.pdf
Normal file
BIN
public/uploads/librillo-3-1638146076.pdf
Normal file
Binary file not shown.
BIN
public/uploads/librillo-4-1638146076.pdf
Normal file
BIN
public/uploads/librillo-4-1638146076.pdf
Normal file
Binary file not shown.
Loading…
Add table
Reference in a new issue