Files
webapp/src/Data/PDF.php
T
2026-10-06 14:05:52 +07:00

201 lines
7.5 KiB
PHP

<?php
/*******************************************************************
Module : /Data/Pdf.php
Created By : Siau Pheng ([email protected]).
Created Date : April 18th, 2015.
Last Modified : August 21st, 2026.
(c) 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace Webapp\Data;
use \flight\Engine;
use \Mpdf;
/**
* Class Pdf
*
* Membuat file Pdf.
*
* @license MIT, https://www.3fonia.com
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
*
* @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 = "<thead><tr>";
for ($i=0; $i<count($this->atabel); $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 .= "</tr></thead>";
$this->__tcolname = $tmp_out;
}
public function clear(): void {
$this->avalue = [];
$this->__theader = "";
$this->__tcolname = "";
}
public function set_header(string $lapor, string $period): void {
$thasil = '<div style="text-align:center;font-size:12pt;"><b>'.strtoupper($this->webapp['REG_NAME']);
$thasil .= (($lapor=="") ? "" : '<br>'.$lapor) . '</b>';
$thasil .= ($period=="") ? "" : '<br><span style="font-size:11pt;">'.$period.'</span>';
$thasil .= '</div><br>';
$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 = "<tr>";
for ($i=0; $i<count($this->atabel); $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 .= "</tr>";
$this->avalue[] = $tmp_out;
}
public function add_span(): void {
$tmp_arr = func_get_args();
$tmp_out = "<tr>";
for ($i=0; $i<count($tmp_arr); $i++) {
$tmp_val = $tmp_arr[$i];// CONTENT, COLSPAN=1, L, FUNC
if (function_exists($tmp_val[3]) && trim($tmp_val[0])<>"") {
$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 .= "</tr>";
$this->avalue[] = $tmp_out;
}
public function add_empty(): void {
$this->avalue[] = $this->set_align("&nbsp;", 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 = '
<style>
#pdf_area {
width: 100%;
color: #000;
}
#pdf_area p { margin: 0; padding: 0; }
#pdf_cont table {
border: 1px solid #ccc;
border-spacing: 0;
border-collapse: collapse;
overflow: auto;
}
#pdf_cont table th, #pdf_cont table td {
font-size: 12pt;
text-align: left;
}
#pdf_cont table th {
text-align: center;
font-weight: bold;
padding: 2mm;
border: 1px solid #ccc;
}
#pdf_cont table td {
padding: 1mm;
}
#pdf_cont table td.left { text-align: left; }
#pdf_cont table td.center { text-align: center; }
#pdf_cont table td.right { text-align: right; }
#pdf_cont table td.bold { font-weight: bold; }
#pdf_cont table td.italic { font-style: italic; }
</style>
<body class="pdf_area">
<htmlpagefooter name="RepFoot">
<table width="100%" style="border:none;"><tr><td width="50%" style="text-align:left;border:none;">'.$this->get_stamp(1, true).'</td><td width="50%" style="text-align:right;font-style:italic;border:none;">Halaman : {PAGENO} dari {nbpg}</td></tr></table>
</htmlpagefooter>
<sethtmlpagefooter name="RepFoot" show-this-page="1" value="on" />';
if ($this->__theader<>"") $html .= $this->__theader;
$html .= '<div id="pdf_cont"><table>';
if ($this->__tcolname<>"") $html .= $this->__tcolname;
$html .= '<tbody>';
// content out
for ($i=0; $i<count($this->avalue); $i++) {
$html .= $this->avalue[$i];
}
$html .= '</tbody></table></div></div></body>';
$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), "<tr>")===false) {
if ($line===true) {
return '<tr><td colspan="'.count($this->atabel).'" class="'.$tstyle.'">'.$text."</td></tr>";
} else {
return (($head===true)?"<th":"<td").(($col>1)?' colspan="'.(string)$col.'"':"").' class="'.$tstyle.'" '.(($head===true)?'width="'.(string)$width.'"':"").">".$text.(($head===true)?"</th>":"</td>");
}
} else {
return (($line===true) ? $text : (($head===true)?"<th":"<td").(($col>1)?" colspan=\"".(string)$col."\"":"")." class=\"".$tstyle." \" ".(($head===true)?"width=\"".(string)$width."\"":"").">".$text.(($head===true)?"</th>":"</td>"));
}
}
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);
}
}
?>