简体   繁体   中英

how to adjust the cell aligment in fpdf

I have used the WordWrap function of fpdf but because of that the cell alignment is getting distorted. Extra blank columns are getting added and the other cells are getting affected. Here is the code. Thank you in advance.

<?php 
require('fpdf.php');
class PDF extends FPDF
{
function WordWrap(&$text, $maxwidth)
{
    $text = trim($text);
    if ($text==='')
        return 0;
    $space = $this->GetStringWidth(' ');
    $lines = explode("\n", $text);
    $text = '';
    $count = 0;

    foreach ($lines as $line)
    {
        $words = preg_split('/ +/', $line);
        $width = 0;

        foreach ($words as $word)
        {
            $wordwidth = $this->GetStringWidth($word);
            if ($wordwidth > $maxwidth)
            {
                // Word is too long, we cut it
                for($i=0; $i<strlen($word); $i++)
                {
                    $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                    if($width + $wordwidth <= $maxwidth)
                    {
                        $width += $wordwidth;
                        $text .= substr($word, $i, 1);
                    }
                    else
                    {
                        $width = $wordwidth;
                        $text = rtrim($text)."\n".substr($word, $i, 1);
                        $count++;
                    }
                }
            }
            elseif($width + $wordwidth <= $maxwidth)
            {
                $width += $wordwidth + $space;
                $text .= $word.' ';
            }
            else
            {
                $width = $wordwidth + $space;
                $text = rtrim($text)."\n".$word.' ';
                $count++;
            }
        }
        $text = rtrim($text)."\n";
        $count++;
    }
    $text = rtrim($text);
    return $text;
}
}

$pdf=new PDF();
$pdf->AddPage();
define("DBHOST","localhost");
define("DBNAME","users");
define("DBUSER","root");
define("PASS","");
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,DBUSER,PASS);
$query = $db->prepare("SELECT name,email,mobile,address,language FROM users ");
$query->execute();
$datas = $query->fetchAll(PDO::FETCH_ASSOC);
$pdf->SetFont('Times','B',12);
$pdf->Cell(20,7,'Name',1);
$pdf->Cell(50,7,'Email',1);
$pdf->Cell(30,7,'Mobile',1);
$pdf->Cell(50,7,'Address',1);
$pdf->Cell(30,7,'Language',1);
$pdf->Ln();
$pdf->SetFont('Times','',12);
foreach ($datas as $row)
{
    $pdf->Cell(20,7,$row['name'],1);
    $pdf->Cell(50,7,$row['email'],1);
    $pdf->Cell(30,7,$row['mobile'],1);

    $text = "An example of a long word is: Supercalifragulistic";
    $pdf->WordWrap($text,30);
    $pdf->Cell(50,7,$pdf->Write(4,$text),1);
    //$pdf->write("<br>");
    $pdf->Cell(30,7,$row['language'],1);
    $pdf->Ln();
}
//$pdf->SetFont('Arial','',12);
// $text='An example of a long word is: Supercalifragulistic';
// $pdf->WordWrap($text,15);
// $pdf->Cell(10,0,$pdf->Write(4,$text));
// $pdf->Write(5,"This paragraph has $nb lines:\n\n");
//  $pdf->Write(4,$text);
$pdf->Output();
?>

Try this

$str = "An example of a long word is: Supercalifragulistic"
$wrapped = wordwrap($str,15)
$pdf->Cell(50,7,$wrapped,1);
$pdf->Write("<br>");

Read

  1. Word wrap
  2. Write HTML

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