使用fpdf从一个数组中输出一个子弹列表[英] using fpdf to output a bullet list from an array

本文是小编为大家收集整理的关于使用fpdf从一个数组中输出一个子弹列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在寻找一种添加子弹或列表编号的方法.

假设我有来自数据库的三行文本:

Line 1
Line 2
Line 3

我想要的是fpdf读取每一行并将子弹放在每条线的前面.因此,输出将看起来像这样:

• Line 1
• Line 2
• Line 3

我发现的最好的解决方案是下面的代码.它功能齐全(如果您已将FPDF上传到服务器).问题是我无法弄清楚如何从文本行生成列表:

<? 
      define('FPDF_FONTPATH', 'font/');

 include('fpdf/fpdf.php');

 class PDF extends FPDF

 {




     var $wLine;
     var $hLine;
     var $Text; 
     var $border;
     var $align;
     var $fill;
     var $Padding;
     var $lPadding;
     var $tPadding;
     var $bPadding;
     var $rPadding;
     var $TagStyle;
     var $Indent;
     var $Space;
     var $PileStyle; 
          var $Line2Print;
     var $NextLineBegin; 
     var $TagName;
     var $Delta;
     var $StringLength; 
          var $LineLength;
     var $wTextLine;
     var $nbSpace;
     var $Xini;
     var $href;
     var $TagHref; 

     function WriteTag($w, $h, $txt, $border=0, $align="J", $fill=false, $padding=0)
     {
         $this->wLine=$w;
         $this->hLine=$h;
         $this->Text=trim($txt);
         $this->Text=preg_replace("/\n|\r|\t/","",$this->Text);
         $this->border=$border;
         $this->align=$align;
         $this->fill=$fill;
         $this->Padding=$padding;

         $this->Xini=$this->GetX();
         $this->href="";
         $this->PileStyle=array();        
              $this->TagHref=array();
         $this->LastLine=false;

         $this->SetSpace();
         $this->Padding();
         $this->LineLength();
         $this->BorderTop();

         while($this->Text!="")
         {
             $this->MakeLine();
             $this->PrintLine();
         }

         $this->BorderBottom();
     }


     function SetStyle($tag, $family, $style, $size, $color, $indent=-1)
     {
          $tag=trim($tag);
          $this->TagStyle[$tag]['family']=trim($family);
          $this->TagStyle[$tag]['style']=trim($style);
          $this->TagStyle[$tag]['size']=trim($size);
          $this->TagStyle[$tag]['color']=trim($color);
          $this->TagStyle[$tag]['indent']=$indent;
     }



     function SetSpace() 
     {
         $tag=$this->Parser($this->Text);
         $this->FindStyle($tag[2],0);
         $this->DoStyle(0);
         $this->Space=$this->GetStringWidth(" ");
     }


     function Padding()
     {
         if(preg_match("/^.+,/",$this->Padding)) {
             $tab=explode(",",$this->Padding);
             $this->lPadding=$tab[0];
             $this->tPadding=$tab[1];
             if(isset($tab[2]))
                 $this->bPadding=$tab[2];
             else
                 $this->bPadding=$this->tPadding;
             if(isset($tab[3]))
                 $this->rPadding=$tab[3];
             else
                 $this->rPadding=$this->lPadding;
         }
         else
         {
             $this->lPadding=$this->Padding;
             $this->tPadding=$this->Padding;
             $this->bPadding=$this->Padding;
             $this->rPadding=$this->Padding;
         }
         if($this->tPadding<$this->LineWidth)
             $this->tPadding=$this->LineWidth;
     }


     function LineLength()
     {
         if($this->wLine==0)
             $this->wLine=$this->w - $this->Xini - $this->rMargin;

         $this->wTextLine = $this->wLine - $this->lPadding - $this->rPadding;
     }


     function BorderTop()
     {
         $border=0;
         if($this->border==1)
             $border="TLR";
         $this->Cell($this->wLine,$this->tPadding,"",$border,0,'C',$this->fill);
         $y=$this->GetY()+$this->tPadding;
         $this->SetXY($this->Xini,$y);
     }


     function BorderBottom()
     {
         $border=0;
         if($this->border==1)
             $border="BLR";
         $this->Cell($this->wLine,$this->bPadding,"",$border,0,'C',$this->fill);
     }


     function DoStyle($tag)
     {
         $tag=trim($tag);
         $this->SetFont($this->TagStyle[$tag]['family'],
             $this->TagStyle[$tag]['style'],
             $this->TagStyle[$tag]['size']);

         $tab=explode(",",$this->TagStyle[$tag]['color']);
         if(count($tab)==1)
             $this->SetTextColor($tab[0]);
         else
             $this->SetTextColor($tab[0],$tab[1],$tab[2]);
     }


     function FindStyle($tag, $ind) 
     {
         $tag=trim($tag);

         if($this->TagStyle[$tag]['family']!="")
             $family=$this->TagStyle[$tag]['family'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['family']!="") {
                     $family=$this->TagStyle[$val]['family'];
                     break;
                 }
             }
         }

         $style="";
         $style1=strtoupper($this->TagStyle[$tag]['style']);
         if($style1!="N")
         {
             $bold=false;
             $italic=false;
             $underline=false;
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 $style1=strtoupper($this->TagStyle[$val]['style']);
                 if($style1=="N")
                     break;
                 else
                 {
                     if(strpos($style1,"B")!==false)
                         $bold=true;
                     if(strpos($style1,"I")!==false)
                         $italic=true;
                     if(strpos($style1,"U")!==false)
                         $underline=true;
                 } 
                  }
             if($bold)
                 $style.="B";
             if($italic)
                 $style.="I";
             if($underline)
                 $style.="U";
         }

         if($this->TagStyle[$tag]['size']!=0)
             $size=$this->TagStyle[$tag]['size'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['size']!=0) {
                     $size=$this->TagStyle[$val]['size'];
                     break;
                 }
             }
         }

         if($this->TagStyle[$tag]['color']!="")
             $color=$this->TagStyle[$tag]['color'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['color']!="") {
                     $color=$this->TagStyle[$val]['color'];
                     break;
                 }
             }
         }

          $this->TagStyle[$ind]['family']=$family;
         $this->TagStyle[$ind]['style']=$style;
         $this->TagStyle[$ind]['size']=$size;
         $this->TagStyle[$ind]['color']=$color;
         $this->TagStyle[$ind]['indent']=$this->TagStyle[$tag]['indent'];
     }


     function Parser($text)
     {
         $tab=array();
         if(preg_match("|^(</([^>]+)>)|",$text,$regs)) {
             $tab[1]="c";
             $tab[2]=trim($regs[2]);
         }
         else if(preg_match("|^(<([^>]+)>)|",$text,$regs)) {
             $regs[2]=preg_replace("/^a/","a ",$regs[2]); 
             $tab[1]="o";
             $tab[2]=trim($regs[2]);

              if(preg_match("/(.+) (.+)='(.+)'/",$regs[2])) {
                 $tab1=preg_split("/ +/",$regs[2]);
                 $tab[2]=trim($tab1[0]);
                 while(list($i,$couple)=each($tab1))
                 {
                     if($i>0) {
                         $tab2=explode("=",$couple);
                         $tab2[0]=trim($tab2[0]);
                         $tab2[1]=trim($tab2[1]);
                         $end=strlen($tab2[1])-2;
                         $tab[$tab2[0]]=substr($tab2[1],1,$end);
                     }
                 }
             }
         }
          // Espace
          else if(preg_match("/^( )/",$text,$regs)) {
             $tab[1]="s";
             $tab[2]=' ';
         }
         // Texte
         else if(preg_match("/^([^< ]+)/",$text,$regs)) {
             $tab[1]="t";
             $tab[2]=trim($regs[1]);
         }
         // Elagage
         $begin=strlen($regs[1]);
          $end=strlen($text);
          $text=substr($text, $begin, $end);
         $tab[0]=$text;

         return $tab; // 0 : $text; 1 : type de balise (tag); 2 : élément
     }


     function MakeLine()
     {
         $this->Text.=" ";
         $this->LineLength=array();
         $this->TagHref=array();
         $Length=0;
         $this->nbSpace=0;

         $i=$this->BeginLine();
         $this->TagName=array();

         if($i==0) {
             $Length=$this->StringLength[0];
             $this->TagName[0]=1;
             $this->TagHref[0]=$this->href;
         }

         while($Length<$this->wTextLine)
         {
             $tab=$this->Parser($this->Text);
             $this->Text=$tab[0];
             if($this->Text=="") {
                 $this->LastLine=true;
                 break;
             }

             if($tab[1]=="o") {
                 array_unshift($this->PileStyle,$tab[2]);
                 $this->FindStyle($this->PileStyle[0],$i+1);

                 $this->DoStyle($i+1);
                 $this->TagName[$i+1]=1;
                 if($this->TagStyle[$tab[2]]['indent']!=-1) {
                     $Length+=$this->TagStyle[$tab[2]]['indent'];
                     $this->Indent=$this->TagStyle[$tab[2]]['indent'];
                 }
                 if($tab[2]=="a")
                     $this->href=$tab['href'];
             }

             if($tab[1]=="c") {
                 array_shift($this->PileStyle);
                 if(isset($this->PileStyle[0]))
                 {
                     $this->FindStyle($this->PileStyle[0],$i+1);
                     $this->DoStyle($i+1);
                 }
                 $this->TagName[$i+1]=1;
                 if($this->TagStyle[$tab[2]]['indent']!=-1) {
                     $this->LastLine=true;
                     $this->Text=trim($this->Text);
                     break;
                 }
                 if($tab[2]=="a")
                     $this->href="";
             }

             if($tab[1]=="s") {
                 $i++;
                 $Length+=$this->Space;
                 $this->Line2Print[$i]="";
                 if($this->href!="")
                     $this->TagHref[$i]=$this->href;
             }

             if($tab[1]=="t") {
                 $i++;
                 $this->StringLength[$i]=$this->GetStringWidth($tab[2]);
                 $Length+=$this->StringLength[$i];
                 $this->LineLength[$i]=$Length;
                 $this->Line2Print[$i]=$tab[2];
                 if($this->href!="")
                     $this->TagHref[$i]=$this->href;
              }

         }

         trim($this->Text);
         if($Length>$this->wTextLine || $this->LastLine==true)
             $this->EndLine();
     }


     function BeginLine()
     {
         $this->Line2Print=array();
         $this->StringLength=array();

         if(isset($this->PileStyle[0]))
         {
             $this->FindStyle($this->PileStyle[0],0);
             $this->DoStyle(0);
         }

         if(count($this->NextLineBegin)>0) {
             $this->Line2Print[0]=$this->NextLineBegin['text'];
             $this->StringLength[0]=$this->NextLineBegin['length'];
             $this->NextLineBegin=array();
             $i=0;
         }
         else {
             preg_match("/^(( *(<([^>]+)>)* *)*)(.*)/",$this->Text,$regs);
             $regs[1]=str_replace(" ", "", $regs[1]);
             $this->Text=$regs[1].$regs[5];
             $i=-1;
         }

         return $i;
     }


     function EndLine()
     {
         if(end($this->Line2Print)!="" && $this->LastLine==false) {
             $this->NextLineBegin['text']=array_pop($this->Line2Print);
             $this->NextLineBegin['length']=end($this->StringLength);
             array_pop($this->LineLength);
         }

         while(end($this->Line2Print)==="")
             array_pop($this->Line2Print);

         $this->Delta=$this->wTextLine-end($this->LineLength);

         $this->nbSpace=0;
         for($i=0; $i<count($this->Line2Print); $i++) {
             if($this->Line2Print[$i]=="")
                 $this->nbSpace++;
         }
     }


     function PrintLine()
     {
         $border=0;
         if($this->border==1)
             $border="LR";
         $this->Cell($this->wLine,$this->hLine,"",$border,0,'C',$this->fill);
         $y=$this->GetY();
         $this->SetXY($this->Xini+$this->lPadding,$y);

         if($this->Indent!=-1) {
             if($this->Indent!=0)
                 $this->Cell($this->Indent,$this->hLine);
             $this->Indent=-1;
         }

         $space=$this->LineAlign();
         $this->DoStyle(0);
         for($i=0; $i<count($this->Line2Print); $i++)
         {
             if(isset($this->TagName[$i]))
                 $this->DoStyle($i);
             if(isset($this->TagHref[$i]))
                 $href=$this->TagHref[$i];
             else
                 $href='';
             if($this->Line2Print[$i]=="")
                 $this->Cell($space,$this->hLine,"         ",0,0,'C',false,$href);
             else
                 $this->Cell($this->StringLength[$i],$this->hLine,$this->Line2Print[$i],0,0,'C',false,$href);
         }

         $this->LineBreak();
         if($this->LastLine && $this->Text!="")
             $this->EndParagraph();
         $this->LastLine=false;
     }


     function LineAlign()
     {
         $space=$this->Space;
         if($this->align=="J") {
             if($this->nbSpace!=0)
                 $space=$this->Space + ($this->Delta/$this->nbSpace);
             if($this->LastLine)
                 $space=$this->Space;
         }

         if($this->align=="R")
             $this->Cell($this->Delta,$this->hLine);

         if($this->align=="C")
             $this->Cell($this->Delta/2,$this->hLine);

         return $space;
     }


     function LineBreak()
     {
         $x=$this->Xini;
         $y=$this->GetY()+$this->hLine;
         $this->SetXY($x,$y);
     }


     function EndParagraph()
     {
         $border=0;
         if($this->border==1)
             $border="LR";
         $this->Cell($this->wLine,$this->hLine/2,"",$border,0,'C',$this->fill);
         $x=$this->Xini;
         $y=$this->GetY()+$this->hLine/2;
         $this->SetXY($x,$y);
     }
 // end add

 //bullet
     function MultiCellBltArray($w, $h, $blt_array, $border=0, $align='J', $fill=0)
     {
         if (!is_array($blt_array))
         {
             die('MultiCellBltArray requires an array with the following keys: bullet, margin, text, indent, spacer');
             exit;
         }

              //Save x
         $bak_x = $this->x;

              for ($i=0; $i<sizeof($blt_array['text']); $i++)
         {
             //Get bullet width including margin
             $blt_width = $this->GetStringWidth($blt_array['bullet'] . $blt_array['margin'])+$this->cMargin*2;

                  // SetX
             $this->SetX($bak_x);

                  //Output indent
             if ($blt_array['indent'] > 0)
                 $this->Cell($blt_array['indent']);

                  //Output bullet
             $this->Cell($blt_width, $h, $blt_array['bullet'] . $blt_array['margin'], 0, '', $fill);

                  //Output text
             $this->MultiCell($w-$blt_width, $h, $blt_array['text'][$i], $border, $align, $fill);

                  //Insert a spacer between items if not the last item
             if ($i != sizeof($blt_array['text'])-1)
                 $this->Ln($blt_array['spacer']);

                  //Increment bullet if it's a number
             if (is_numeric($blt_array['bullet']))
                 $blt_array['bullet']++;
         }

              //Restore x
         $this->x = $bak_x;
     }

 }


 $pdf=new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->SetFont('Times', '', 12);

 $column_width = $pdf->w-30;
 $sample_text = 'This is sample text.\n This is a new line of text';

 $test1 = array();
 $test1['bullet'] = chr(149);
 $test1['margin'] = ' ';
 $test1['indent'] = 0;
 $test1['spacer'] = 0;
 $test1['text'] = array();
 for ($i=0; $i<2; $i++)
 {
     $test1['text'][$i] = $sample_text;

 }
 $pdf->SetX(10);
 $pdf->MultiCellBltArray($column_width-$pdf->x, 6, $test1);
 $pdf->Ln(10);


 $pdf->Output();
 ?>

问题是输出看起来像这样:

• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text

当我希望它看起来像:

• This is bulleted text.
• This is a new line of text

这种代码行的作用似乎以复制上面的$ sample_text的方式:

for ($i=0; $i<2; $i++)
 {
     $test1['text'][$i] = $sample_text;

 }

您在哪里更改'$ i <2'以说'$ i <4',结果是:

• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text

但是,如果我有四个不同的变量,我将要将它们放在示例文本文件中,然后将代码迭代遍历它,使我在每个项目的前面都有子弹:

$sample_text = "item 1 \n item 2 \n item 3 \n item 4";

,结果将是:

• item 1
• item 2
• item 3
• item 4

推荐答案

您可以使用 chr()chr()/p>

//-------------
// bullet() - shows a bullet point (in current font)
//-------------
//
// @author munkywrench development
// @since 30/04/05
// @url http://munkywrench.co.nz
//
// @param Int $cellwidth - Sets width of indent
//--------------
function bullet($cellwidth = '0') {
  //bullet point is chr(127)
  $this->Cell($cellwidth, 5, chr(127), 0, 0, 'R');
}

参考

其他推荐答案

\n在单引号内带有print \n pdf,无论是否希望将其当作真正的线路中断,您需要使用双引号.

还要确保其期望文本而不是html

其他推荐答案

我找到了这个网站:

 dingbats true type字体

$pdf->SetFont('ZapfDingbats','', 8);
$pdf->Cell(6, 6, 'm', 1, 0);

之后,您必须返回您的字体:

pdf->SetFont('Arial','',8);

本文地址:https://www.itbaoku.cn/post/2090913.html

问题描述

I have been looking for a way to add bullets, or numbering of lists to my pdf output file.

Let's say I have three lines of text coming from the database:

Line 1
Line 2
Line 3

What I want is fpdf to read each line and place a bullet in front of each. Thus the output will look like this:

• Line 1
• Line 2
• Line 3

The best solution I have found is the code below. It is fully functional (if you have fpdf uploaded to your server). The problem is I cannot figure out how to generate a list from lines of text:

<? 
      define('FPDF_FONTPATH', 'font/');

 include('fpdf/fpdf.php');

 class PDF extends FPDF

 {




     var $wLine;
     var $hLine;
     var $Text; 
     var $border;
     var $align;
     var $fill;
     var $Padding;
     var $lPadding;
     var $tPadding;
     var $bPadding;
     var $rPadding;
     var $TagStyle;
     var $Indent;
     var $Space;
     var $PileStyle; 
          var $Line2Print;
     var $NextLineBegin; 
     var $TagName;
     var $Delta;
     var $StringLength; 
          var $LineLength;
     var $wTextLine;
     var $nbSpace;
     var $Xini;
     var $href;
     var $TagHref; 

     function WriteTag($w, $h, $txt, $border=0, $align="J", $fill=false, $padding=0)
     {
         $this->wLine=$w;
         $this->hLine=$h;
         $this->Text=trim($txt);
         $this->Text=preg_replace("/\n|\r|\t/","",$this->Text);
         $this->border=$border;
         $this->align=$align;
         $this->fill=$fill;
         $this->Padding=$padding;

         $this->Xini=$this->GetX();
         $this->href="";
         $this->PileStyle=array();        
              $this->TagHref=array();
         $this->LastLine=false;

         $this->SetSpace();
         $this->Padding();
         $this->LineLength();
         $this->BorderTop();

         while($this->Text!="")
         {
             $this->MakeLine();
             $this->PrintLine();
         }

         $this->BorderBottom();
     }


     function SetStyle($tag, $family, $style, $size, $color, $indent=-1)
     {
          $tag=trim($tag);
          $this->TagStyle[$tag]['family']=trim($family);
          $this->TagStyle[$tag]['style']=trim($style);
          $this->TagStyle[$tag]['size']=trim($size);
          $this->TagStyle[$tag]['color']=trim($color);
          $this->TagStyle[$tag]['indent']=$indent;
     }



     function SetSpace() 
     {
         $tag=$this->Parser($this->Text);
         $this->FindStyle($tag[2],0);
         $this->DoStyle(0);
         $this->Space=$this->GetStringWidth(" ");
     }


     function Padding()
     {
         if(preg_match("/^.+,/",$this->Padding)) {
             $tab=explode(",",$this->Padding);
             $this->lPadding=$tab[0];
             $this->tPadding=$tab[1];
             if(isset($tab[2]))
                 $this->bPadding=$tab[2];
             else
                 $this->bPadding=$this->tPadding;
             if(isset($tab[3]))
                 $this->rPadding=$tab[3];
             else
                 $this->rPadding=$this->lPadding;
         }
         else
         {
             $this->lPadding=$this->Padding;
             $this->tPadding=$this->Padding;
             $this->bPadding=$this->Padding;
             $this->rPadding=$this->Padding;
         }
         if($this->tPadding<$this->LineWidth)
             $this->tPadding=$this->LineWidth;
     }


     function LineLength()
     {
         if($this->wLine==0)
             $this->wLine=$this->w - $this->Xini - $this->rMargin;

         $this->wTextLine = $this->wLine - $this->lPadding - $this->rPadding;
     }


     function BorderTop()
     {
         $border=0;
         if($this->border==1)
             $border="TLR";
         $this->Cell($this->wLine,$this->tPadding,"",$border,0,'C',$this->fill);
         $y=$this->GetY()+$this->tPadding;
         $this->SetXY($this->Xini,$y);
     }


     function BorderBottom()
     {
         $border=0;
         if($this->border==1)
             $border="BLR";
         $this->Cell($this->wLine,$this->bPadding,"",$border,0,'C',$this->fill);
     }


     function DoStyle($tag)
     {
         $tag=trim($tag);
         $this->SetFont($this->TagStyle[$tag]['family'],
             $this->TagStyle[$tag]['style'],
             $this->TagStyle[$tag]['size']);

         $tab=explode(",",$this->TagStyle[$tag]['color']);
         if(count($tab)==1)
             $this->SetTextColor($tab[0]);
         else
             $this->SetTextColor($tab[0],$tab[1],$tab[2]);
     }


     function FindStyle($tag, $ind) 
     {
         $tag=trim($tag);

         if($this->TagStyle[$tag]['family']!="")
             $family=$this->TagStyle[$tag]['family'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['family']!="") {
                     $family=$this->TagStyle[$val]['family'];
                     break;
                 }
             }
         }

         $style="";
         $style1=strtoupper($this->TagStyle[$tag]['style']);
         if($style1!="N")
         {
             $bold=false;
             $italic=false;
             $underline=false;
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 $style1=strtoupper($this->TagStyle[$val]['style']);
                 if($style1=="N")
                     break;
                 else
                 {
                     if(strpos($style1,"B")!==false)
                         $bold=true;
                     if(strpos($style1,"I")!==false)
                         $italic=true;
                     if(strpos($style1,"U")!==false)
                         $underline=true;
                 } 
                  }
             if($bold)
                 $style.="B";
             if($italic)
                 $style.="I";
             if($underline)
                 $style.="U";
         }

         if($this->TagStyle[$tag]['size']!=0)
             $size=$this->TagStyle[$tag]['size'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['size']!=0) {
                     $size=$this->TagStyle[$val]['size'];
                     break;
                 }
             }
         }

         if($this->TagStyle[$tag]['color']!="")
             $color=$this->TagStyle[$tag]['color'];
         else
         {
             reset($this->PileStyle);
             while(list($k,$val)=each($this->PileStyle))
             {
                 $val=trim($val);
                 if($this->TagStyle[$val]['color']!="") {
                     $color=$this->TagStyle[$val]['color'];
                     break;
                 }
             }
         }

          $this->TagStyle[$ind]['family']=$family;
         $this->TagStyle[$ind]['style']=$style;
         $this->TagStyle[$ind]['size']=$size;
         $this->TagStyle[$ind]['color']=$color;
         $this->TagStyle[$ind]['indent']=$this->TagStyle[$tag]['indent'];
     }


     function Parser($text)
     {
         $tab=array();
         if(preg_match("|^(</([^>]+)>)|",$text,$regs)) {
             $tab[1]="c";
             $tab[2]=trim($regs[2]);
         }
         else if(preg_match("|^(<([^>]+)>)|",$text,$regs)) {
             $regs[2]=preg_replace("/^a/","a ",$regs[2]); 
             $tab[1]="o";
             $tab[2]=trim($regs[2]);

              if(preg_match("/(.+) (.+)='(.+)'/",$regs[2])) {
                 $tab1=preg_split("/ +/",$regs[2]);
                 $tab[2]=trim($tab1[0]);
                 while(list($i,$couple)=each($tab1))
                 {
                     if($i>0) {
                         $tab2=explode("=",$couple);
                         $tab2[0]=trim($tab2[0]);
                         $tab2[1]=trim($tab2[1]);
                         $end=strlen($tab2[1])-2;
                         $tab[$tab2[0]]=substr($tab2[1],1,$end);
                     }
                 }
             }
         }
          // Espace
          else if(preg_match("/^( )/",$text,$regs)) {
             $tab[1]="s";
             $tab[2]=' ';
         }
         // Texte
         else if(preg_match("/^([^< ]+)/",$text,$regs)) {
             $tab[1]="t";
             $tab[2]=trim($regs[1]);
         }
         // Elagage
         $begin=strlen($regs[1]);
          $end=strlen($text);
          $text=substr($text, $begin, $end);
         $tab[0]=$text;

         return $tab; // 0 : $text; 1 : type de balise (tag); 2 : élément
     }


     function MakeLine()
     {
         $this->Text.=" ";
         $this->LineLength=array();
         $this->TagHref=array();
         $Length=0;
         $this->nbSpace=0;

         $i=$this->BeginLine();
         $this->TagName=array();

         if($i==0) {
             $Length=$this->StringLength[0];
             $this->TagName[0]=1;
             $this->TagHref[0]=$this->href;
         }

         while($Length<$this->wTextLine)
         {
             $tab=$this->Parser($this->Text);
             $this->Text=$tab[0];
             if($this->Text=="") {
                 $this->LastLine=true;
                 break;
             }

             if($tab[1]=="o") {
                 array_unshift($this->PileStyle,$tab[2]);
                 $this->FindStyle($this->PileStyle[0],$i+1);

                 $this->DoStyle($i+1);
                 $this->TagName[$i+1]=1;
                 if($this->TagStyle[$tab[2]]['indent']!=-1) {
                     $Length+=$this->TagStyle[$tab[2]]['indent'];
                     $this->Indent=$this->TagStyle[$tab[2]]['indent'];
                 }
                 if($tab[2]=="a")
                     $this->href=$tab['href'];
             }

             if($tab[1]=="c") {
                 array_shift($this->PileStyle);
                 if(isset($this->PileStyle[0]))
                 {
                     $this->FindStyle($this->PileStyle[0],$i+1);
                     $this->DoStyle($i+1);
                 }
                 $this->TagName[$i+1]=1;
                 if($this->TagStyle[$tab[2]]['indent']!=-1) {
                     $this->LastLine=true;
                     $this->Text=trim($this->Text);
                     break;
                 }
                 if($tab[2]=="a")
                     $this->href="";
             }

             if($tab[1]=="s") {
                 $i++;
                 $Length+=$this->Space;
                 $this->Line2Print[$i]="";
                 if($this->href!="")
                     $this->TagHref[$i]=$this->href;
             }

             if($tab[1]=="t") {
                 $i++;
                 $this->StringLength[$i]=$this->GetStringWidth($tab[2]);
                 $Length+=$this->StringLength[$i];
                 $this->LineLength[$i]=$Length;
                 $this->Line2Print[$i]=$tab[2];
                 if($this->href!="")
                     $this->TagHref[$i]=$this->href;
              }

         }

         trim($this->Text);
         if($Length>$this->wTextLine || $this->LastLine==true)
             $this->EndLine();
     }


     function BeginLine()
     {
         $this->Line2Print=array();
         $this->StringLength=array();

         if(isset($this->PileStyle[0]))
         {
             $this->FindStyle($this->PileStyle[0],0);
             $this->DoStyle(0);
         }

         if(count($this->NextLineBegin)>0) {
             $this->Line2Print[0]=$this->NextLineBegin['text'];
             $this->StringLength[0]=$this->NextLineBegin['length'];
             $this->NextLineBegin=array();
             $i=0;
         }
         else {
             preg_match("/^(( *(<([^>]+)>)* *)*)(.*)/",$this->Text,$regs);
             $regs[1]=str_replace(" ", "", $regs[1]);
             $this->Text=$regs[1].$regs[5];
             $i=-1;
         }

         return $i;
     }


     function EndLine()
     {
         if(end($this->Line2Print)!="" && $this->LastLine==false) {
             $this->NextLineBegin['text']=array_pop($this->Line2Print);
             $this->NextLineBegin['length']=end($this->StringLength);
             array_pop($this->LineLength);
         }

         while(end($this->Line2Print)==="")
             array_pop($this->Line2Print);

         $this->Delta=$this->wTextLine-end($this->LineLength);

         $this->nbSpace=0;
         for($i=0; $i<count($this->Line2Print); $i++) {
             if($this->Line2Print[$i]=="")
                 $this->nbSpace++;
         }
     }


     function PrintLine()
     {
         $border=0;
         if($this->border==1)
             $border="LR";
         $this->Cell($this->wLine,$this->hLine,"",$border,0,'C',$this->fill);
         $y=$this->GetY();
         $this->SetXY($this->Xini+$this->lPadding,$y);

         if($this->Indent!=-1) {
             if($this->Indent!=0)
                 $this->Cell($this->Indent,$this->hLine);
             $this->Indent=-1;
         }

         $space=$this->LineAlign();
         $this->DoStyle(0);
         for($i=0; $i<count($this->Line2Print); $i++)
         {
             if(isset($this->TagName[$i]))
                 $this->DoStyle($i);
             if(isset($this->TagHref[$i]))
                 $href=$this->TagHref[$i];
             else
                 $href='';
             if($this->Line2Print[$i]=="")
                 $this->Cell($space,$this->hLine,"         ",0,0,'C',false,$href);
             else
                 $this->Cell($this->StringLength[$i],$this->hLine,$this->Line2Print[$i],0,0,'C',false,$href);
         }

         $this->LineBreak();
         if($this->LastLine && $this->Text!="")
             $this->EndParagraph();
         $this->LastLine=false;
     }


     function LineAlign()
     {
         $space=$this->Space;
         if($this->align=="J") {
             if($this->nbSpace!=0)
                 $space=$this->Space + ($this->Delta/$this->nbSpace);
             if($this->LastLine)
                 $space=$this->Space;
         }

         if($this->align=="R")
             $this->Cell($this->Delta,$this->hLine);

         if($this->align=="C")
             $this->Cell($this->Delta/2,$this->hLine);

         return $space;
     }


     function LineBreak()
     {
         $x=$this->Xini;
         $y=$this->GetY()+$this->hLine;
         $this->SetXY($x,$y);
     }


     function EndParagraph()
     {
         $border=0;
         if($this->border==1)
             $border="LR";
         $this->Cell($this->wLine,$this->hLine/2,"",$border,0,'C',$this->fill);
         $x=$this->Xini;
         $y=$this->GetY()+$this->hLine/2;
         $this->SetXY($x,$y);
     }
 // end add

 //bullet
     function MultiCellBltArray($w, $h, $blt_array, $border=0, $align='J', $fill=0)
     {
         if (!is_array($blt_array))
         {
             die('MultiCellBltArray requires an array with the following keys: bullet, margin, text, indent, spacer');
             exit;
         }

              //Save x
         $bak_x = $this->x;

              for ($i=0; $i<sizeof($blt_array['text']); $i++)
         {
             //Get bullet width including margin
             $blt_width = $this->GetStringWidth($blt_array['bullet'] . $blt_array['margin'])+$this->cMargin*2;

                  // SetX
             $this->SetX($bak_x);

                  //Output indent
             if ($blt_array['indent'] > 0)
                 $this->Cell($blt_array['indent']);

                  //Output bullet
             $this->Cell($blt_width, $h, $blt_array['bullet'] . $blt_array['margin'], 0, '', $fill);

                  //Output text
             $this->MultiCell($w-$blt_width, $h, $blt_array['text'][$i], $border, $align, $fill);

                  //Insert a spacer between items if not the last item
             if ($i != sizeof($blt_array['text'])-1)
                 $this->Ln($blt_array['spacer']);

                  //Increment bullet if it's a number
             if (is_numeric($blt_array['bullet']))
                 $blt_array['bullet']++;
         }

              //Restore x
         $this->x = $bak_x;
     }

 }


 $pdf=new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->SetFont('Times', '', 12);

 $column_width = $pdf->w-30;
 $sample_text = 'This is sample text.\n This is a new line of text';

 $test1 = array();
 $test1['bullet'] = chr(149);
 $test1['margin'] = ' ';
 $test1['indent'] = 0;
 $test1['spacer'] = 0;
 $test1['text'] = array();
 for ($i=0; $i<2; $i++)
 {
     $test1['text'][$i] = $sample_text;

 }
 $pdf->SetX(10);
 $pdf->MultiCellBltArray($column_width-$pdf->x, 6, $test1);
 $pdf->Ln(10);


 $pdf->Output();
 ?>

The problem is the output looks like this:

• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text

When I want it to look like:

• This is bulleted text.
• This is a new line of text

This line of code seems to act in such a way as to duplicate the $sample_text above:

for ($i=0; $i<2; $i++)
 {
     $test1['text'][$i] = $sample_text;

 }

Where is you change '$i<2' to lets say '$i<4', the result would be:

• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text
• This is bulleted text.\n This is a new line of text

But if I have a set of four different variables, I will want to place them in the sample text file, and then have the code iterate through it, leaving me with a bullet in front of each item:

$sample_text = "item 1 \n item 2 \n item 3 \n item 4";

And the result would then be:

• item 1
• item 2
• item 3
• item 4

推荐答案

You can use chr() function of PHP.

//-------------
// bullet() - shows a bullet point (in current font)
//-------------
//
// @author munkywrench development
// @since 30/04/05
// @url http://munkywrench.co.nz
//
// @param Int $cellwidth - Sets width of indent
//--------------
function bullet($cellwidth = '0') {
  //bullet point is chr(127)
  $this->Cell($cellwidth, 5, chr(127), 0, 0, 'R');
}

Reference

其他推荐答案

\n inside single quotes with print \n pdf or not, if you want it to be interrupted as a real line break you need to use double quotes.

Also make sure it expects text and not html

其他推荐答案

I found this website:

https://webprogramming.ninja/2015/12/28/fpdf-and-the-check-mark/

dingbats true type font

$pdf->SetFont('ZapfDingbats','', 8);
$pdf->Cell(6, 6, 'm', 1, 0);

After this you have to return to your font:

pdf->SetFont('Arial','',8);