Code:
// this uses tcpdf
//sample output is at http://ecoland.com/cidemo/pdfdemo.bmp
// first is a general purpose routine
// then comes an example on how to use
//1. this is a general purpose table print routine in a helper file
public function print_table($title,$cols,$data,$autoadjustcols = true,
$started = false,$sumcols=false){
if (!$started) $this->reportinit();
$margins = $this->getMargins();
$dims = $this->getPageDimensions('0');
$mrg = $this->getMargins();
$btmline = $dims['hk']-$mrg['top']; // pagelength
if ($autoadjustcols){
$w = 0;
for ($i=0;$i<count($cols);$i++) $w += $cols[$i][1];
$delta = ($dims['wk']-$mrg['left']-$mrg['right']-$w); // excess/deficit
for ($i=0;$i<count($cols);$i++) $cols[$i][1] = ($cols[$i][1]*($w+$delta)/$w);
}
if ($title != ''){
// $this->SetY(10);
// $this->SetX(10);
$this->SetFont('', 'B');
$w = 0;
for ($i=0;$i<count($cols);$i++) $w += $cols[$i][1];
$this->Cell($w,0,$title,0,0,'C'); // width,height,text,border,ln,align
$this->SetFont('', '');
$this->Ln();
}
$lineheight = 0;
// $this->newpage($cols);
$this->print_colheaders($cols);
$fill = 0;
$brd = 'LR';
if ($sumcols) {
$runtots = array();
$dotot = array();
for ($icol=0;$icol<count($cols);$icol++){
$runtots[] = 0;
$dotot[] = (int)(in_array($icol,$sumcols));
}
}
foreach ($data as $row){
$linetop = $this->getY();
for ($icol=0;$icol<count($cols);$icol++){
$col = $cols[$icol];
if ($col[4] == 1) // float
$this->Cell($col[1],6,number_format($row[$col[2]],2),$brd,0,$col[3],$fill);
else { //$this->Cell($col[1],6,$row[$col[2]],$brd,0,$col[3],$fill);
// if ($col[4] == 2) // date
$this->ClippedCell($col[1],6,$row[$col[2]],$brd,0,$col[3],$fill);
}
if ($sumcols && $dotot[$icol])$runtots[$icol] += $row[$col[2]];
}
$this->Ln();
$lineheight = $this->getY()-$linetop;
$fill=!$fill;
if (($this->getY()+($lineheight*2)) >= $btmline){
$brd = 'LRB';
if (($this->getY()+$lineheight) >= $btmline) {
$this->AddPage();
$fill = 0;
$this->SetMargins(10,10,10);
$this->print_colheaders($cols);
// $this->newpage($cols,true);
}
} else $brd = 'LR';
}
$this->SetFont('', 'B');
if ($sumcols)
for ($icol=0;$icol<count($cols);$icol++){
$col = $cols[$icol];
if ($dotot[$icol]) $this->Cell($col[1], 0, number_format($runtots[$icol],2), 1, 0, $col[3], $fill);
else $this->Cell($col[1], 0, '', 1, 0, $col[3], $fill);
}
$this->SetFont('', '');
}
// 2. this is an example of usage
$title = 'Sales ~ VAT Details (sorted by Date,Ref,VAT Code)';
// coltitle,colwidth,fieldname,alignment,formatid 0: none, 1: float, 2: date
$cols = array(
array('Date',22,'transdate','L',2),
array('Ref',25,'refnum','L',0),
array('VAT',15,'vatcode','L',0),
array('Type',15,'transtype','L',0),
array('Memo',68,'memo','L',0),
array('Nett',25,'amount','R',1),
array('VAT Amt',25,'vatamount','R',1),
);
$this->pdf->print_table($title,$cols,$qry->result_array(),false,true,array(5,6));