FPDF MultiCell & how to align specific cell in FPDF using PHP ?

Viewed 1551

I have two (2) questions.

QUESTION 1

I have these code where the table that includes:

  1. Fetch value from database.

  2. Table with MultiCells. Text fits inside table cell.

I create three (3) table, data from database and the result shows like table below.

enter image description here

Code seem to break into new line after the first table. My goal is to make the table look like this

enter image description here

Here's my current code

Code.php

class myPDF extends FPDF 
{
    // CALCULATE
        var $widths;
        var $aligns;

        function SetWidths($w)
        {
            //Set the array of column widths
            $this->widths=$w;
        }

        function SetAligns($a)
        {
            //Set the array of column alignments
            $this->aligns=$a;
        }

        function Row($data)
        {
            //Calculate the height of the row
            $nb=0;
            for($i=0;$i<count($data);$i++)
                $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
            $h=5*$nb;
            //Issue a page break first if needed
            $this->CheckPageBreak($h);
            //Draw the cells of the row
            for($i=0;$i<count($data);$i++)
            {
                $w=$this->widths[$i];
                $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
                //Save the current position
                $x=$this->GetX();
                $y=$this->GetY();
                //Draw the border
                $this->Rect($x,$y,$w,$h);
                //Print the text
                $this->MultiCell($w,5,$data[$i],0,$a);
                //Put the position to the right of the cell
                $this->SetXY($x+$w,$y);
            }
            //Go to the next line
            $this->Ln($h);
        }

        function CheckPageBreak($h)
        {
            //If the height h would cause an overflow, add a new page immediately
            if($this->GetY()+$h>$this->PageBreakTrigger)
                $this->AddPage($this->CurOrientation);
        }

        function NbLines($w,$txt)
        {
            //Computes the number of lines a MultiCell of width w will take
            $cw=&$this->CurrentFont['cw'];
            if($w==0)
                $w=$this->w-$this->rMargin-$this->x;
            $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
            $s=str_replace("\r",'',$txt);
            $nb=strlen($s);
            if($nb>0 and $s[$nb-1]=="\n")
                $nb--;
            $sep=-1;
            $i=0;
            $j=0;
            $l=0;
            $nl=1;
            while($i<$nb)
            {
                $c=$s[$i];
                if($c=="\n")
                {
                    $i++;
                    $sep=-1;
                    $j=$i;
                    $l=0;
                    $nl++;
                    continue;
                }
                if($c==' ')
                    $sep=$i;
                $l+=$cw[$c];
                if($l>$wmax)
                {
                    if($sep==-1)
                    {
                        if($i==$j)
                            $i++;
                    }
                    else
                        $i=$sep+1;
                    $sep=-1;
                    $j=$i;
                    $l=0;
                    $nl++;
                }
                else
                    $i++;
            }
            return $nl;
        }
    // CALCULATE

    // TABLE
        function viewHeaderTable(){
            $this->SetFont('Arial','B',10);
            $this->Cell(88.33, 5, 'CUSTOMER PROFILE',0,0);
            $this->Cell(5, 1, '',0,0);
            $this->Cell(88.33, 5, 'RATING',0,0);
            $this->Cell(5, 1, '',0,0);
            $this->Cell(88.33, 5, 'LEGAL ACTION STATUS',0,0);
            $this->Ln();


            $this->setFillColor(220,220,220); 
            $this->SetFont('Arial','B',6);
            $this->Cell(50,5,'SHAREHOLDERS NAME',1,0,'',true);
            $this->Cell(19.16,5,'SHARE (%)',1,0,'R',true);
            $this->Cell(19.16,5,'EQUITY',1,0,'',true);

            $this->Cell(5,5,'',0,0);


            $this->setFillColor(220,220,220); 
            $this->SetFont('Arial','B',6);
            $this->Cell(44.16,5,'INITIAL',1,0,'',true);
            $this->Cell(44.16,5,'LATEST',1,0,'',true);

            $this->Cell(5,5,'',0,0);


            $this->setFillColor(220,220,220); 
            $this->SetFont('Arial','B',6);
            $this->Cell(10,5,'NO',1,0,'',true);
            $this->Cell(20,5,'TYPE',1,0,'',true);
            $this->Cell(58.33,5,'DATE',1,0,'',true);

            $this->Ln();
        }
        function viewDatabaseTable(){

            // Customer Profile
                $this->SetFont('Arial','',6);
                $sqlTxt = "SELECT * FROM CUSTOMER";
                $sql = ORACLE::getInstance()->FetchArray($sqlTxt);
                if(count($sql) > 0)
                {
                    foreach($sql as $row)
                    {
                        $this->SetWidths(array(50,19.16,19.16));
                        srand(microtime()*1000000);
                        $altItem = array();

                        $altItem[]=$row["SHAREHOLDERS_NAME"];
                        $altItem[]=$row["SHARE"];
                        $altItem[]=$row["EQUITY"];
                        $this->Row($altItem);
                    }
                }
            // Customer Profile

            // Rating
                $this->SetFont('Arial','',6);
                $sqlTxt = "SELECT * FROM RATING";
                $sql = ORACLE::getInstance()->FetchArray($sqlTxt);
                if(count($sql) > 0)
                {
                    foreach($sql as $row){

                        $this->SetWidths(array(44.16,44.16));
                        srand(microtime()*1000000);
                        $altItem = array();

                        $altItem[]=$row["INITIAL"];
                        $altItem[]=$row["LATEST"];
                        $this->Row($altItem);
                    }
                }
            // Rating 

            // Legal
                $this->Cell(10,5,'1.',1,0);
                $this->Cell(20,5,'1st Reminder',1,0);
                $sql = ORACLE::getInstance()->FetchArray("SELECT * FROM LEGAL");
                if(count($sql) > 0){
                    foreach($sql as $row){
                        $output = $row['FIRST_REMINDER'];
                        $newdate = date_create($output);
                        $legal_date = date_format($newdate, 'j F Y');
                        $this->Cell(58.33,5,$legal_date,1,0);
                    }
                }else{
                    $this->Cell(58.33,5,'',1,0);
                }
                $this->Ln();

                $this->Cell(10,5,'2.',1,0);
                $this->Cell(20,5,'Final Reminder',1,0);
                $sql = ORACLE2::getInstance()->FetchArray("SELECT * FROM LEGAL");
                if(count($sql) > 0){
                    foreach($sql as $row){
                        $output = $row['FINAL_REMINDER'];
                        $newdate = date_create($output);
                        $legal_final_date = date_format($newdate, 'j F Y');
                        $this->Cell(58.33,5,$legal_final_date,1,0);
                    }
                }else{
                    $this->Cell(58.33,5,'',1,0);
                }
                $this->Ln();

                $this->Cell(10,5,'3.',1,0);
                $this->Cell(20,5,'NOD',1,0);
                $sql = ORACLE2::getInstance()->FetchArray("SELECT * FROM LEGAL");
                if(count($sql) > 0){
                    foreach($sql as $row){
                        $output = $row['NOD'];
                        $newdate = date_create($output);
                        $legal_nod_date = date_format($newdate, 'j F Y');

                        $this->Cell(58.33,5,$legal_nod_date,1,0);
                    }
                }else{
                    $this->Cell(58.33,5,'',1,0);
                }
                $this->Ln();

                $this->Cell(10,5,'4.',1,0);
                $this->Cell(20,5,'NOT',1,0);
                $sql = ORACLE2::getInstance()->FetchArray("SELECT * FROM LEGAL");
                if(count($sql) > 0){
                    foreach($sql as $row){
                        $output = $row['NOT'];
                        $newdate = date_create($output);
                        $legal_not_date = date_format($newdate, 'j F Y');
                        $this->Cell(58.33,5,$legal_not_date,1,0);
                    }
                }else{
                    $this->Cell(58.33,5,'',1,0);
                }
                $this->Ln();

                $this->Cell(10,5,'5.',1,0);
                $this->Cell(20,5,'STATUS',1,0);
                $sql = ORACLE2::getInstance()->FetchArray("SELECT * FROM LEGAL");
                if(count($sql) > 0){
                    foreach($sql as $row){
                        $this->Cell(58.33,5,$row['STATUS'],1,0);
                    }
                }else{
                    $this->Cell(58.33,5,'',1,0);
                }
                $this->Ln();
            // Legal
        }
    // TABLE

}
$pdf = new myPDF('L','mm','A4');
$pdf->AliasNbPages('{pages}');
$pdf->SetTitle('Review');
$pdf->SetAutoPageBreak(true,15);
$pdf->SetMargins(10, 10); 
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();  

$pdf->viewHeaderTable();
$pdf->viewDatabaseTable();
$pdf->Ln();

$fileName = 'Filename.pdf';
$pdf->Output($fileName, 'I');

QUESTION 2

How to align specific column to right alignment or center alignment from above code ? In my case, I want value inside column SHARE (%) be right align and column DATE be center align

Appreciate if someone can help.

Thanks in advance

0 Answers
Related