*
* @package Webapp\Data
*/
final class Pdf {
private array $avalue = [];
private array $atabel = [];
private string $__theader = "";
private string $__tcolname = "";
protected array $webapp;
protected array $webuser;
/**
* @param Engine $app
*/
public function __construct(Engine $app) {
$this->webapp = $app->session()->get('WEB_APP', ['REG_NAME'=>'', 'APP_NAME'=>'', 'APP_VER'=>'']);
$this->webuser = $app->session()->get('WEB_USER', ['UserID'=>0, 'Username'=>'']);
}
public function add_field(string $nama, string|int $lebar=0, string $align="L", string $func="", int $kol=1): void {
$this->atabel[] = [ $nama, (string)$lebar.'%', $func, $align, $kol ];
}
public function show_fields(): void {
$tmp_out = "";
for ($i=0; $iatabel); $i++) {
if ($this->atabel[$i][4]==0) continue;
$tmp_out .= $this->set_align($this->atabel[$i][0], $this->atabel[$i][1], "C", false, true, $this->atabel[$i][4]);
}
$tmp_out .= "
";
$this->__tcolname = $tmp_out;
}
public function clear(): void {
$this->avalue = [];
$this->__theader = "";
$this->__tcolname = "";
}
public function set_header(string $lapor, string $period): void {
$thasil = '
'.strtoupper($this->webapp['REG_NAME']);
$thasil .= (($lapor=="") ? "" : '
'.$lapor) . '';
$thasil .= ($period=="") ? "" : '
'.$period.'';
$thasil .= '
';
$this->__theader = $thasil;
}
public function add_value(): void {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$tmp_out = "";
for ($i=0; $iatabel); $i++) {
$tmp_val = $tmp_arr[$i];
if (is_array($this->atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = $this->atabel[$i][2][$tmp_val];
} else if (function_exists($this->atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = call_user_func($this->atabel[$i][2], $tmp_val);
}
$tmp_val = $this->set_align($tmp_val, $this->atabel[$i][1], $this->atabel[$i][3]);
$tmp_out .= $tmp_val;
}
$tmp_out .= "
";
$this->avalue[] = $tmp_out;
}
public function add_span(): void {
$tmp_arr = func_get_args();
$tmp_out = "";
for ($i=0; $i"") {
$tmp_val[0] = call_user_func($tmp_val[3], $tmp_val[0]);
}
$tmp_val = $this->set_align($tmp_val[0], 0, $tmp_val[2], false, false, $tmp_val[1]);
$tmp_out .= $tmp_val;
}
$tmp_out .= "
";
$this->avalue[] = $tmp_out;
}
public function add_empty(): void {
$this->avalue[] = $this->set_align(" ", 0, "L,NB", true);
}
public function add_stamp(int $ctk_ke=1, bool $tampil_nama=false): void {
$this->avalue[] = $this->set_align($this->get_stamp($ctk_ke, $tampil_nama), 0, "L,NB", true);
}
public function get_stamp(int $ctk_ke=1, bool $tampil_nama=false): string {
return $this->webapp['APP_NAME']."v".$this->webapp['APP_VER'].date("\@ymd.His")."-".isi_nol($this->webuser['UserID'], 3).(($tampil_nama)?"/".strtoupper($this->webuser['Username']):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):"");
}
public function createPDF(string $nama_file = "Nama_File.pdf", int $fontz = 8, string $orien = "P"): void {
$html = '
| '.$this->get_stamp(1, true).' | Halaman : {PAGENO} dari {nbpg} |
';
if ($this->__theader<>"") $html .= $this->__theader;
$html .= '';
if ($this->__tcolname<>"") $html .= $this->__tcolname;
$html .= '';
// content out
for ($i=0; $iavalue); $i++) {
$html .= $this->avalue[$i];
}
$html .= '
';
$mpdf = new mPDF('c', 'A4'.($orien==="L"?"-L":""), $fontz, '', 5, 5, 5, 10, 5, 5);
$mpdf->useSubstitutions = false;
$mpdf->WriteHTML($html);
$mpdf->Output($nama_file.".pdf", "I");
}
public function set_align(string $text, string|int $width, string $dir="L", bool $line=false, bool $head=false, int $col=1): string {
$tstyle = $this->create_style($dir);
if (strpos(strtolower($text), "")===false) {
if ($line===true) {
return '
| '.$text." |
";
} else {
return (($head===true)?"1)?' colspan="'.(string)$col.'"':"").' class="'.$tstyle.'" '.(($head===true)?'width="'.(string)$width.'"':"").">".$text.(($head===true)?" | ":"");
}
} else {
return (($line===true) ? $text : (($head===true)?"1)?" colspan=\"".(string)$col."\"":"")." class=\"".$tstyle." \" ".(($head===true)?"width=\"".(string)$width."\"":"").">".$text.(($head===true)?" | ":""));
}
}
private function create_style(string $style="L"): string {
$apad = ["L"=>"left", "R"=>"right", "C"=>"center", "NB"=>"nb", "NL"=>"noleft", "NR"=>"noright", "B"=>"bold", "I"=>"italic"];
$_astyle = explode(",", $style);
$ahasil = [];
foreach ($_astyle as $_key => $_val) {
$ahasil[] = $apad[$_val];
}
return implode(' ', $ahasil);
}
}
?>