Dan Dan - 18 days ago 7
PHP Question

Change width of one Column in Table using FPDF

I have a dynamic PHP table which is generated on to a PDF via FPDF.

How can I make the column 'Name' wider than the others?

class PDF extends FPDF {
function Header() {
$this->Image('quote-header.png');
$this->Ln(2);
}

function Footer() {
$this->Image('quote-footer.png');
}

function LoadData($file) {
$lines = file($file);
$data = array();
foreach($lines as $line)
$data[] = explode(';', trim($line));
return $data;
}

function BasicTable($header, $data) {
foreach($header as $col)
$this->Cell(40, 7, $col, 1);
$this->Ln();
foreach($data as $row) {
foreach($row as $col)
$this->Cell(40, 6, $col, 1);
$this->Ln();
}
}
}

$header = array('Product Reference', 'Name', '('. $pound_sign .') Price (excl. VAT)', 'Unit');


I'm confident that is the only code that generates the table?

Any help would be brilliant. This is for a Product Quotation system for a company I work for and I cannot progress further without fixing this column width issue.

The table is made up of 4 columns: Product Reference, Name, Price and Unit. I need the Name column to be wider than the others, or if possible (automatically adjust) to the Product Name.

Answer

The first parameter in the Cell method is width. http://www.fpdf.org/en/doc/cell.htm

Try this to double the size.

function BasicTable($header, $data) {
    $nameIndex =  array_search ( 'Name' , $header );       

    foreach($header as $key => $col) {
        $width = ($key == $nameIndex) ? 80 : 40;
        $this->Cell($width, 7, $col, 1);            
    }

    $this->Ln();

    // This assumes that $row is an int indexed array
    // E.G looks like array(0 => 'some Product Reference ', 1 => 'Some Name' , 2 =>'Some Price', 3 => 'Some Unit')         
    foreach($data as $row) {
        foreach($row as $key => $col) {
            $width = ($key == $nameIndex) ? 80 : 40;
            $this->Cell($width, 6, $col, 1);
        }
        $this->Ln();
    }

}