如何调整阵列 FPDF 中的多单元

发布于 2025-01-09 19:41:50 字数 2080 浏览 1 评论 0原文

我需要帮助,了解如何使数组内的数据在多单元格中处于同一高度且高度相同,

这就是结果 输入图片此处描述

这是代码:

  $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;
  }

这是我添加多单元之前的旧版本 输入图片此处描述

这是代码:

      $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;
  }

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;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2025-01-16 19:41:50

我在github中找到了一个解决方案 在此处输入链接描述

在此处输入图像描述

我用这些函数

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;
}

,这是我经过一些更改后的代码:

      //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 ; 
  }

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 ; 
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文