如何调整数组 FPDF 中的多单元格
How to adjust Multicell in Array FPDF
我需要有关如何使数组内的数据在多单元格中位于同一行和相同高度的帮助
这是结果
这是代码:
$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;
}
我在 github enter link description here
中找到了解决方案
我使用这些功能
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 ;
}
我需要有关如何使数组内的数据在多单元格中位于同一行和相同高度的帮助
这是结果
这是代码:
$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;
}
我在 github enter link description here
中找到了解决方案我使用这些功能
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 ;
}