简体   繁体   中英

How to wrap a text inside a Cell function in FPDF?

样本输出

This is my codes:

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

I want to make the text go to the next line automatically if it have reached the end of the cell. I'm using the default format of Cell function in FPDF.

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

Is it possible to implement it just as the same with Cell function? Please help me. I got stuck here and this is the only thing I need to be done in order to finish my project. Thanks in advance!

Your question addresses mostly not the limited Cell method but rather the Table creation.

If you want to draw an table where each row entity has the same height independent on the content size you should consider to implement one as proposed by @Fky or grasp an already available solution.. for example this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM