Search code examples
phppdffpdfbillingerp

How to adjust Multicell in Array FPDF


i need a help on how to make the data inside the array to be in one ligne and same height in multicell

this is the result enter image description here

and this is the code :

  $colored = false;
  foreach($products_info as $row){

      if($colored){
          $this->SetFillColor(218,218,218);              
      }else{
          $this->SetFillColor(255,255,255);
         
      }
    
    $this->Cell(40,15,ucwords($row["libelle"]),1,0,'L',$colored);
    
    $this->MultiCell(40,15,ucwords($row["description"]),1,1,'L',$colored);

    $this->Cell(40,15,number_format($row["prix_unitaire"],3,","," "),1,0,"C",$colored);
    $this->Cell(20,15,number_format($row["quantity"],3,","," "),1,0,"C",$colored);
    $this->Cell(10,15,number_format($row["largeur"],3,","," "),1,0,"C",$colored);
    $this->Cell(10,15,number_format($row["hauteur"],3,","," "),1,0,"C",$colored);
    $this->Cell(30,15,number_format($row["total_ht"],3,","," "),1,1,"R",$colored);
    $colored = !$colored;
  }

this is the old version before i add multicell enter image description here

and this is the code :

      $colored = false;
  foreach($products_info as $row){

      if($colored){
          $this->SetFillColor(218,218,218);              
      }else{
          $this->SetFillColor(255,255,255);
         
      }
    
    $this->Cell(40,15,ucwords($row["libelle"]),1,0,'L',$colored);
    $this->Cell(40,15,ucwords($row["description"]),1,0,'L',$colored);
    $this->Cell(40,15,number_format($row["prix_unitaire"],3,","," "),1,0,"C",$colored);
    $this->Cell(20,15,number_format($row["quantity"],3,","," "),1,0,"C",$colored);
    $this->Cell(10,15,number_format($row["largeur"],3,","," "),1,0,"C",$colored);
    $this->Cell(10,15,number_format($row["hauteur"],3,","," "),1,0,"C",$colored);
    $this->Cell(30,15,number_format($row["total_ht"],3,","," "),1,1,"R",$colored);
    $colored = !$colored;
  }

Solution

  • i found a solution in github enter link description here

    enter image description here

    i use these functions

    function SetWidths($w)
    {
        //Tableau des largeurs de colonnes
        $this->widths=$w;
    }
    
    function SetAligns($a)
    {
        //Tableau des alignements de colonnes
        $this->aligns=$a;
    }
    
    function Row($data,$bool)
    {
        //Calcule la hauteur de la ligne
        $nb=0;
        for($i=0;$i<count($data);$i++)
            $nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
        $h=10*$nb;
        //Effectue un saut de page si nécessaire
        $this->CheckPageBreak($h);
        //Dessine les cellules
    
        for($i=0;$i<count($data);$i++)
        {
            $w=$this->widths[$i];
            $a=isset($this->aligns[$i]) ?  'C' : 'C';
    
            //Sauve la position courante
            $x=$this->GetX();
            $y=$this->GetY();
            //Dessine le cadre
    
            if($bool){
              $this->Rect($x,$y,$w,$h,'DF');             
            }
            else  
            {
              $this->Rect($x,$y,$w,$h);
            }
    
              
            //Imprime le texte
            $this->MultiCell($w,10,$data[$i],0,$a);
            //Repositionne à droite
            $this->SetXY($x+$w,$y);
    
          
            
        }
        //Va à la ligne
        $this->Ln($h);
    }
    
    function CheckPageBreak($h)
    {
        //Si la hauteur h provoque un débordement, saut de page manuel
        if($this->GetY()+$h>$this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }
    
    function NbLines($w,$txt)
    {
        //Calcule le nombre de lignes qu'occupe un MultiCell de largeur w
        $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 && $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;
    }
    

    and here is my code after some changes :

          //Table de 7 colonnes
      $this->SetWidths(array(40,40,40,20,10,10,30));
    
    
      $colored = false;
      
      foreach($products_info as $row){
        $this->Row(
          array(
            ucwords($row["libelle"]),
            ucwords($row["description"]),
            number_format($row["prix_unitaire"],3,","," "),
            number_format($row["quantity"],3,","," "),
            number_format($row["largeur"],3,","," "),
            number_format($row["hauteur"],3,","," "),
            number_format($row["total_ht"],3,","," ")
          ), $colored);
    
          $colored = !$colored ; 
      }