Forum How do I...?

Splitting text into 3 columns

elvanto
I'm slowly moving my TCPDF code over to Prince. One thing I am wanting to replicate is the ability to split a list of names over 2 or 3 columns.

In TCPDF you give simply create a list of names separated by line breaks as per my example below.

It will display the first list of names in the first column alphabetically down one column. If there's more text that can fit in that single column, it creates a second column, then a third column and then breaks onto the next page and starts all over again.

It's nice and easy and looks good.

My code:

It uses setEqualColumns. Here was my code:

<?php
$people_html = '';
if ($people) {
	foreach($people AS $person) {
		$people_html .= $person . '<br>';
	}
}

// PDF library
require_once(LIBPATH . '/tcpdf/tcpdf.php');

// Extend TCPDF to work with multiple columns
class MC_TCPDF extends TCPDF {

	public function PrintColumns($columns, $people_html) {
		
		// add a new page
		$this->AddPage();
		
		// set font
		$this->SetFont(pdf_font(), 'B', '14');
		
		// heading
		$this->Cell(180, 6, __('Group Report') . ' - ' . esc_text($group->group_name), 0, 1);
		$this->Ln(4);
		
		// disable existing columns
		$this->resetColumns();

		// determine column widths
		$column_width = round(171 / $columns);
		
		// set columns
		$this->setEqualColumns($columns, $column_width);
		
		// print html
		$this->writeHTML($people_html, true, false, true, false, 'J');
	}

}

// create new PDF document
$pdf = new MC_TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, '12pt', PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin('12pt');
$pdf->SetFooterMargin('12pt');

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, '12pt');

// print columns of names
$pdf->PrintColumns(3, $people_html);

// close and output PDF document
$pdf->Output('People-list.pdf', 'I');
?>


Is it possible to do a similar thing in Prince? I understand Prince may not have a similar function but is there some way to determine if the text is going to go over the height of the page so I can start putting it in table form?
mikeday
You can just put it in a div and apply "columns: 3" for a multi-column layout. :)
elvanto
Perfect! I didn't even know that existed. I see .columns only has partial browser support. Glad Prince is all over it :)