Skip to content

Instantly share code, notes, and snippets.

@arysom
Last active September 20, 2021 21:34
Show Gist options
  • Select an option

  • Save arysom/d0662177bc3b53b20dce49c1d6d02e53 to your computer and use it in GitHub Desktop.

Select an option

Save arysom/d0662177bc3b53b20dce49c1d6d02e53 to your computer and use it in GitHub Desktop.
TCPDF multi column layout using setEqualColumns
<?php
//data taken f.e. from https://mockaroo.com/
$data = file_get_contents('./data/MOCK_DATA.json');
$arr = json_decode($data, true);
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->addPage();
$pdf->SetAutoPageBreak(true);
$pdf->SetY(150);
$pdf->SetMargins(15,20,20,TRUE);
$last_y = $pdf->GetY();
$pdf->setEqualColumns(4, 45);
$cols = 3;
$col = 0;
$count = 1;
$pdf->selectColumn($col);
foreach ($arr as $val) {
if ($last_y >= $pdf->getPageHeight() - 20) {
$col++;
$pdf->selectColumn($col);
if ($col > $cols) {
$col = 0;
$pdf->selectColumn($col);
$pdf->AddPage();
}
}
$str = $count . '. ' . $val['first_name'] . ' ' . $val['last_name'];
$h = $pdf->getStringHeight(45, $str);
$pdf->MultiCell(45, $h, $str, 1, 'L');
$count++;
$last_y = $pdf->GetY();
}
$pdf->Output('output.pdf', 'I');
//https://www.dropbox.com/s/mrtm1dwc789fkxd/test.pdf?dl=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment