如何在FPDF的Cell函数内包装文本?

时间:2018-08-20 14:46:41

标签: php fpdf

sample output

这是我的代码:

$pdf->SetFillColor(255,255,255);
$pdf -> SetTextColor(0,0,0);
$pdf -> SetFont ("Times","B","10");
$pdf -> Cell(60,7,"NAME AND ADDRESS OF SCHOOL",'L,T',0,'L','false');
$pdf -> Cell(32,7,"LEVEL",'L,T',0,'L','false');
$pdf -> Cell(28,7,"DATE FROM",'L,T',0,'L','false');
$pdf -> Cell(28,7,"DATE TO",'L,T',0,'L','false');
$pdf -> Cell(28,7,"DEGREE",'L,T',0,'L','false');
$pdf -> Cell(20,7,"AWARDS",'L,T,R',1,'L','false');


    if (isset($_GET['id'])) {
        $sql = "SELECT * FROM education_info WHERE applicant_code = " . $_GET['id'];
        $result = mysqli_query($coonn, $sql);
        $resultCheck = mysqli_num_rows($result);

        if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result)) # end of first php code  
            {

            $pdf -> SetFont ("Times","","11");
            $pdf -> Cell(60,5, $row['educ_name'],'T,L',0,'L','false'); 
            $pdf -> Cell(32,5, "",'T,L',0,'L','false'); 
            $pdf -> Cell(28,5, "",'T,L',0,'L','false');
            $pdf -> Cell(28,5, "",'T,L',0,'L','false'); 
            $pdf -> Cell(28,5, "",'T,L',0,'L','false'); 
            $pdf -> Cell(20,5, "",'T,L,R',1,'L','false');    

            $pdf -> Cell(60,5, $row['educ_address'],'L,B',0,'L','false'); 
            $pdf -> Cell(32,5, $row['educ_level'],'L,B',0,'L','false');
            $pdf -> Cell(28,5, $row['educ_from'],'L,B',0,'L','false'); 
            $pdf -> Cell(28,5, $row['educ_to'],'L,B',0,'L','false'); 
            $pdf -> Cell(28,5, $row['educ_degree'],'L,B',0,'L','false'); 
            $pdf -> Cell(20,5, $row['educ_award'],'L,R,B',1,'L','false'); 
            }
        }
    }

如果文本到达单元格的末尾,我想使文本自动转到下一行。我在FPDF中使用的是Cell函数的默认格式。

function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
    {
        // Output a cell
        $k = $this->k;
        if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
        {
            // Automatic page break
            $x = $this->x;
            $ws = $this->ws;
            if($ws>0)
            {
                $this->ws = 0;
                $this->_out('0 Tw');
            }
            $this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation);
            $this->x = $x;
            if($ws>0)
            {
                $this->ws = $ws;
                $this->_out(sprintf('%.3F Tw',$ws*$k));
            }
        }
        if($w==0)
            $w = $this->w-$this->rMargin-$this->x;
        $s = '';
        if($fill || $border==1)
        {
            if($fill)
                $op = ($border==1) ? 'B' : 'f';
            else
                $op = 'S';
            $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
        }
        if(is_string($border))
        {
            $x = $this->x;
            $y = $this->y;
            if(strpos($border,'L')!==false)
                $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
            if(strpos($border,'T')!==false)
                $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
            if(strpos($border,'R')!==false)
                $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
            if(strpos($border,'B')!==false)
                $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
        }
        if($txt!=='')
        {
            if(!isset($this->CurrentFont))
                $this->Error('No font has been set');
            if($align=='R')
                $dx = $w-$this->cMargin-$this->GetStringWidth($txt);
            elseif($align=='C')
                $dx = ($w-$this->GetStringWidth($txt))/2;
            else
                $dx = $this->cMargin;
            if($this->ColorFlag)
                $s .= 'q '.$this->TextColor.' ';
            $s .= sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$this->_escape($txt));
            if($this->underline)
                $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
            if($this->ColorFlag)
                $s .= ' Q';
            if($link)
                $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
        }
        if($s)
            $this->_out($s);
        $this->lasth = $h;
        if($ln>0)
        {
            // Go to next line
            $this->y += $h;
            if($ln==1)
                $this->x = $this->lMargin;
        }
        else
            $this->x += $w;
    }

是否可以像Cell函数一样实现它?请帮我。我被困在这里,这是完成我的项目所需要做的唯一一件事。预先感谢!

1 个答案:

答案 0 :(得分:-1)

您的问题主要解决的不是有限的Cell方法,而是表的创建。

如果要绘制一个表格,其中每个行实体的高度均与内容大小无关,则应考虑按照@Fky的建议实施一个表格,或掌握一个已经可用的解决方案。例如:

http://www.fpdf.org/en/script/script50.php