473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to wrap text with PHP

107 100+
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. require('fpdf.php');
  5.  
  6. class PDF extends FPDF
  7. {
  8. //Page header
  9. function Header()
  10. {
  11.     //Logo
  12.    //$this->Image('../images/bg.png',10,8,33);
  13.  
  14. }
  15.  
  16. //Page footer
  17. function Footer()
  18. {
  19.     $date =  date("F j, Y");
  20.     //Position at 1.5 cm from bottom
  21.     $this->SetY(-15);
  22.     //Arial italic 8
  23.     $this->SetFont('Arial','I',8);
  24.     //Page number
  25.     $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  26.     $this->Cell(0,1,'Date '.Date("F j, Y").'/{nb}',100,0,'L');
  27. }
  28. }
  29.  
  30.  
  31. $pdf = new PDF();
  32. $pdf->open();
  33. $pdf->AddPage();
  34. $pdf->AliasNbPages();   // necessary for x of y page numbers to appear in document
  35. $pdf->SetAutoPageBreak(false);
  36.  
  37. // document properties
  38. $pdf->SetAuthor('INSERT AUTHOR');
  39. $pdf->SetTitle('INSERT DOC TITLE');
  40.  
  41. $pdf->SetFont('Arial','B',10);
  42. $pdf->Cell(40,10,'TITLE FOR REPORT');
  43.  
  44. // Add date report ran
  45. //$pdf->SetFont('Arial','B',10);
  46. $date =  date("F j, Y");
  47. //$pdf->Cell(40,10,'Report date: '.$date);
  48.  
  49. $pdf->SetDrawColor(0, 0, 0); //black
  50.  
  51. //table header
  52. $pdf->SetFillColor(170, 170, 170); //gray
  53. $pdf->setFont("Arial","B","9");
  54. $pdf->setXY(10, 40); 
  55. $pdf->Cell(8, 10, "Sl #", 1, 0, "C", 1);
  56. $pdf->Cell(103, 10, "Col Title1", 1, 0, "L", 1);   
  57. $pdf->Cell(15, 10, "Col Title2", 1, 0, "C", 1);
  58. $pdf->Cell(30, 10, "Col Title3", 1, 0, "L", 1);
  59. $pdf->Cell(20, 10, "Col Title4", 1, 0, "L", 1); 
  60. $pdf->Cell(20, 10, "Col Title5", 1, 0, "L", 1); 
  61.  
  62. $y = 50;
  63. $x = 10;  
  64.  
  65. $pdf->setXY($x, $y);
  66.  
  67. $pdf->setFont("Arial","","9");
  68.  
  69. $id = $_GET['id'];
  70.  
  71. include("config.php");  // configure to point to your connection script.
  72. global $database;
  73. $query_result = "SELECT * FROM table WHERE id=$id and field6=0 and field7<'".$date."' order by field2 asc"; 
  74. $result = $database->query($query_result);
  75. $num = mysql_num_rows($result);
  76.  
  77. for($i=0; $i<$num; $i++)
  78. {
  79.         $pdf->Cell(8, 8, $i+1,1, 0, "C", 0);
  80.         $pdf->Cell(103, 8, mysql_escape_string(mysql_result($result,$i,"field1")), 1);   // CHANGE THESE TO REPRESENT YOUR FIELDS
  81.         $pdf->Cell(15, 8, mysql_result($result,$i,"field2"), 1, 0, "C", 0);
  82.         $pdf->Cell(30, 8, mysql_result($result,$i,"field3"), 1);
  83.         $pdf->Cell(20, 8, $database->mysql_to_ddmmyyyy(mysql_result($result,$i,"field4")), 1, 0, "C", 0);
  84.         $pdf->Cell(20, 8, $database->mysql_to_ddmmyyyy(mysql_result($result,$i,"field5")), 1, 0, "C", 0);
  85.  
  86.         $y += 8;
  87.  
  88.         if ($y > 260)    // When you need a page break
  89.         {
  90.             $pdf->AddPage();
  91.             $y = 10;
  92.  
  93.         }
  94.  
  95.         $pdf->setXY($x, $y);
  96. }
  97.  
  98. $pdf->Output();
  99. ?>
  100.  
Jul 13 '10 #1
3 5859
code green
1,726 Expert 1GB
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
Jul 14 '10 #2
raamay
107 100+
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.
Jul 19 '10 #3
code green
1,726 Expert 1GB
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
Jul 19 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark P | last post by:
My images are fixed in size and can't be redone. Many of them are tall and narrow. How can I wrap text so that the text starts on the right of the image at a level with the top of the image....
2
by: amber | last post by:
Hello, I have a datagrid column header that is quite long, and I would like it to appear on 2 lines instead of 1. Is this possible? TIA. Amber
1
by: Grant Hammond | last post by:
I assume I'm not alone in my frustration that the expression builder that comes (in part) with Access XP that dosnt wrap text when you open it on an exisitng expression in a query or form. I's...
0
by: Norman Fritag | last post by:
Hi there, 1) I is there a more elegant way, more professionally way this export could done, as I have written some code for the first time? 2) who could I wrap text cell a1 a3 via code ? 3) is...
4
by: searider86 | last post by:
Does anyone know if there is a snippet of code that automatically removes the wrap text format in excel? I have an access form that displays data and allows the user to click on a cmdbutton to...
1
by: Diego | last post by:
Hi, how do I set the wrap property to FALSE for a datagrid with autocolums enabled? I know there's a work around this bug when the columns are set up in design time but I need the autocolumns...
2
by: Rocco | last post by:
Hi, I have a drop down box with width=120px. The box length cannot be extended because of design issue. How can I wrap text in html drop down box ? Thanks, Rocco
0
by: Keithb | last post by:
I have a GridView control with 2 template columns. Both contail a Label control in their ItemTemplate. The right column label wraps text on long strings, the left column just gets wider with long...
15
by: removeps-groups | last post by:
How to wrap text in <ptag if the text has no spaces and is very long? Here is an example: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.