FPDF MultiCell,以及如何使用PHP在FPDF中对齐特定单元格?

时间:2018-09-19 09:12:23

标签: php cell fpdf

我有两(2)个问题。

问题1

我有这些代码,其中的表包括:

  1. 从数据库中获取值。

  2. 具有多单元格的表。文本适合表格单元格。

我创建了三(3)个表,数据来自数据库,结果如下表所示。

enter image description here

代码似乎在第一个表之后换行了。我的目标是使桌子看起来像这样

enter image description here

这是我当前的代码

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');

问题2

如何将特定列与上述代码中的右对齐中心对齐对齐? 就我而言,我希望列 SHARE(%)内的值右对齐,列 DATE 内的值居中对齐

感谢有人可以提供帮助。

预先感谢

0 个答案:

没有答案