How to wrap text with PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    How to wrap text with PHP

    I picked up the below code from some website and it is absolutely good working code except that the wrap text feature is not available. The value of field1 below do not go to a new line if the value is long instead it overlaps to the next column. Therefore, i would like to request if anybody could give me a piece of code to solve this issue.
    Code:
    <?php
     
    require('fpdf.php');
     
    class PDF extends FPDF
    {
    //Page header
    function Header()
    {
        //Logo
       //$this->Image('../images/bg.png',10,8,33);
       
    }
     
    //Page footer
    function Footer()
    {
    	$date =  date("F j, Y");
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        //Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    	$this->Cell(0,1,'Date '.Date("F j, Y").'/{nb}',100,0,'L');
    }
    }
     
     
    $pdf = new PDF();
    $pdf->open();
    $pdf->AddPage();
    $pdf->AliasNbPages();   // necessary for x of y page numbers to appear in document
    $pdf->SetAutoPageBreak(false);
     
    // document properties
    $pdf->SetAuthor('INSERT AUTHOR');
    $pdf->SetTitle('INSERT DOC TITLE');
     
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(40,10,'TITLE FOR REPORT');
     
    // Add date report ran
    //$pdf->SetFont('Arial','B',10);
    $date =  date("F j, Y");
    //$pdf->Cell(40,10,'Report date: '.$date);
     
    $pdf->SetDrawColor(0, 0, 0); //black
     
    //table header
    $pdf->SetFillColor(170, 170, 170); //gray
    $pdf->setFont("Arial","B","9");
    $pdf->setXY(10, 40); 
    $pdf->Cell(8, 10, "Sl #", 1, 0, "C", 1);
    $pdf->Cell(103, 10, "Col Title1", 1, 0, "L", 1);   
    $pdf->Cell(15, 10, "Col Title2", 1, 0, "C", 1);
    $pdf->Cell(30, 10, "Col Title3", 1, 0, "L", 1);
    $pdf->Cell(20, 10, "Col Title4", 1, 0, "L", 1); 
    $pdf->Cell(20, 10, "Col Title5", 1, 0, "L", 1); 
     
    $y = 50;
    $x = 10;  
     
    $pdf->setXY($x, $y);
     
    $pdf->setFont("Arial","","9");
    
    $id = $_GET['id'];
    
    include("config.php");  // configure to point to your connection script.
    global $database;
    $query_result = "SELECT * FROM table WHERE id=$id and field6=0 and field7<'".$date."' order by field2 asc"; 
    $result = $database->query($query_result);
    $num = mysql_num_rows($result);
     
    for($i=0; $i<$num; $i++)
    {
    	    $pdf->Cell(8, 8, $i+1,1, 0, "C", 0);
            $pdf->Cell(103, 8, mysql_escape_string(mysql_result($result,$i,"field1")), 1);   // CHANGE THESE TO REPRESENT YOUR FIELDS
            $pdf->Cell(15, 8, mysql_result($result,$i,"field2"), 1, 0, "C", 0);
            $pdf->Cell(30, 8, mysql_result($result,$i,"field3"), 1);
            $pdf->Cell(20, 8, $database->mysql_to_ddmmyyyy(mysql_result($result,$i,"field4")), 1, 0, "C", 0);
            $pdf->Cell(20, 8, $database->mysql_to_ddmmyyyy(mysql_result($result,$i,"field5")), 1, 0, "C", 0);
     
            $y += 8;
            
            if ($y > 260)    // When you need a page break
    		{
                $pdf->AddPage();
                $y = 10;
    			
    		}
            
            $pdf->setXY($x, $y);
    }
     
    $pdf->Output();
    ?>
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Don't think you can wrap text with FPDF.
    Imagine that FPDF creates a template for painting and you have to colour between the lines.

    Not very helpful I know.
    Maybe you could measure the text length and insert newlines at relevant points.
    (A tricky piece of code on its own)

    But you would have to allow for the Cell height now being one line height taller.

    FPDF is not dynamic enough to meet your needs.
    Someone may have written a wrapper class that handles this.
    There are other classes such as ezPDF but cannot comment on their performance

    Comment

    • raamay
      New Member
      • Feb 2007
      • 107

      #3
      I came up with better product called the mpdf which is much simpler and easy for creating dynamic pdf. Instead of dwelling with fpdf, i recommend trying the above class for faster and better result.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I'll take a look at mpdf raamay.
        Been using FPDF a while but I liken it to old school gaming code where every pixel had to be accounted for

        Comment

        Working...