Files
crm-ms1/v4_ci4/app/Libraries/LabelGenerator.php
2026-05-27 11:44:10 -04:00

63 lines
1.5 KiB
PHP

<?php
namespace App\Libraries;
class LabelGenerator
{
public function render($formatName, $view, $data)
{
require_once APPPATH . 'ThirdParty/FPDF/fpdf.php';
$start = $_POST['start_label'] ?? 1;
$pos = 1;
$formats = config('LabelFormats');
$f = $formats->formats[$formatName];
$pdf = new \App\Libraries\Pdf\MyFPDF('P', 'mm', $f['page']);
$pdf->SetAutoPageBreak(false);
$i = 0;
$total = count($data);
while ($i < $total) {
$pdf->AddPage();
for ($row = 0; $row < $f['rows']; $row++) {
for ($col = 0; $col < $f['cols']; $col++) {
$x = $f['marginLeft'] + ($col * ($f['width'] + $f['gapX']));
$y = $f['marginTop'] + ($row * ($f['height'] + $f['gapY']));
if ($pos < $start) {
$pos++;
continue;
}
if ($i >= $total) {
break 3;
}
$pdf->SetXY($x, $y);
extract(array_merge($data[$i], [
'pdf' => $pdf,
'x' => $x,
'y' => $y,
'f' => $f,
]));
require APPPATH . 'Views/' . $view . '.php';
$i++;
$pos++;
}
}
}
$pdf->Output('I', 'labels.pdf');
exit;
}
}