Ben
Ben

Reputation: 129

Table MultiCell in FPDF

I am currently using FPDF to output data into a table, however i am running into issues with table cell wrapping.

My current table function is below:

function ImprovedTableCol2($header, $data, $cols ,$colWidth1, $colWidth2, $tableHeader, $closeTable, $fontSize, $icon)
{
    // Table Header
    if ($tableHeader==true) {
        $this->SetFontSize(12);
        $this->Cell(90,7,$header[0],1,0,'L');
        $this->Cell(90,7,$header[1],1,0,'L');
        $this->Ln();
    }
    // Table body
    $this->SetFontSize($fontSize);
    $this-> MultiCell(90,6,$data[0],'LRB');
    $this->MultiCell(90,6,$data[1],'LRB');
    $this->Ln();
}

My table page is as follows:

$pdf->AddPage();
$pdf->SetFont('Arial','B');
$pdf->SetFontSize(18);
$pdf->Cell(0,10,'Method Statement','B',1,'L');
$pdf->SetFont('');
$pdf->SetFontSize(10);
$pdf->Ln();
$header = array('', '');
$intTotalSCRows = "{method_statement:total_rows}";
$arrSafetyCheck = array();
array_push($arrSafetyCheck, array(
    "day" => "{method_statement:day}",
    "statement" => "{method_statement:statement}",
    "engineer" => "{method_statement:engineer}",
    "count" => "{method_statement:count}")
);
foreach ($arrSafetyCheck as $key => $list) {
    if ($list['count']=="1") {
        $data = array(utf8_decode($list['day']), $list['statement']);
        $pdf->ImprovedTableCol2($header,$data,2,130,50,true,false,9,false);
    } else if ($list['count']==$intTotalSCRows) {
        $data = array(utf8_decode($list['day']), $list['statement']);
        $pdf->ImprovedTableCol2($header,$data,2,130,50,false,true,9,false);
    } else {
        $data = array(utf8_decode($list['day']), $list['statement']);
        $pdf->ImprovedTableCol2($header,$data,2,130,50,false,false,9,false);
    }
}

The MultiCell function does wrap the text, but i cannot get the 2 table columns to sit side by side.

Upvotes: 2

Views: 8419

Answers (1)

Danny
Danny

Reputation: 729

You will need to manually set the position of the cell (see the updated method below)

function ImprovedTableCol2($header, $data, $cols ,$colWidth1, $colWidth2, $tableHeader, $closeTable, $fontSize, $icon)
{
    // Table Header
    if ($tableHeader==true) {
        $this->SetFontSize(12);
        $this->Cell(90,7,$header[0],1,0,'L');
        $this->Cell(90,7,$header[1],1,0,'L');
        $this->Ln();
    }
    // Table body
    $this->SetFontSize($fontSize);

    // Get X,Y coordinates
    $x = $this->GetX();
    $y = $this->GetY();

    $this->MultiCell(90,6,$data[0],'LRB');

    // update the X coordinate to account for the previous cell width
    $x += 90;

    // set the XY Coordinates
    $this->SetXY($x, $y);

    $this->MultiCell(90,6,$data[1],'LRB');
    $this->Ln();
}

Upvotes: 3

Related Questions