Skip to content

Instantly share code, notes, and snippets.

@filpgame
Last active July 17, 2018 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filpgame/5b1a8caad108c4c04c78 to your computer and use it in GitHub Desktop.
Save filpgame/5b1a8caad108c4c04c78 to your computer and use it in GitHub Desktop.
function ColoredTable($header, $data) {
// Colors, line width and bold font
$this->SetFillColor(200, 200, 200);
$this->SetTextColor(0);
$this->SetDrawColor(0, 0, 0);
$this->SetLineWidth(0.3);
$this->SetFont('', 'B', 12);
// Header
$w = array(50, 30, 20, 90, 30, 40);
foreach ($header as $key => $head) {
$this->Cell($w[$key], 7, $head, 1, 0, 'C', 1);
}
$this->Ln();
// Color and font restoration
$this->SetFillColor(224, 235, 255);
$this->SetTextColor(0);
$this->SetFont('', '', 10);
// Data
$fill = 0;
$i = 0;
foreach ($data as $row) {
$cellcount = array();
//write text first
$startX = $this->GetX();
$startY = $this->GetY();
//draw cells and record maximum cellcount
//cell height is 6 and width is 80
foreach ($row as $key => $column):
$cellcount[] = $this->MultiCell($w[$key], 6, ($column), 0, 'L', $fill, 0, '', '', true, 0, false, true, 0, "M");
endforeach;
$this->SetXY($startX, $startY);
//now do borders and fill
//cell height is 6 times the max number of cells
$maxnocells = max($cellcount);
foreach ($row as $key => $column):
$this->MultiCell($w[$key], $maxnocells * 6, '', 'LR', 'L', $fill, 0, '', '', true, 0, false, true, 0, "M");
endforeach;
$this->Ln();
// fill equals not fill (flip/flop)
$fill = !$fill;
$i += $maxnocells;
if ($i > 23) { //edit this condition according to your paper height
$this->AddPage('L', 'A4');
$i = 0;
}
}
$this->Cell(array_sum($w), 0, '', 'T');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment