Page 1 of 1

PHP and PDF

Posted: Sun Feb 01, 2004 12:49 pm
by basdog22
Hi, i don't know if this is good to be here. You can move it anytime :wink:

I have a class that creates PDF files on the fly and it works fine. The only problem is that i can't add line feeds.

I want to create pdfs that will hold text which exist in my database. So i do something like this:

Code: Select all

<?php
$pdf->text(100, 100, "$headline");                            // Text at x=100 and y=100.
    $pdf->setFont('Arial', 'BI', 9);
    $pdf->text(100, 130, "by $writer");
    $pdf->text(50, 170, "$intro");
    $pdf->text(50, 200, "$story");
?>
The result is :

blah.. blah.. blah.. blah.. blah.. blah.. blah.. blah.. blah..

instead of :


blah.. blah.. blah..
blah.. blah.. blah..
blah.. blah.. blah..
blah.. blah.. blah..

This makes the text flow out of the pdf screen :roll:

Any help???

I tried <br>, \n, \r\n, \r
but nothing.

Is there a special char that says to pdfs to create a line feed???

Posted: Tue Feb 03, 2004 3:28 pm
by matthiasone
I use an app from http://www.fpdf.org/ for creating PDF files

Posted: Tue Feb 03, 2004 3:34 pm
by ol4pr0
if i am not mistaken zend.com has a nice tutorial on this subject to

;-) which creates nice pdf formats as well.

Posted: Tue Feb 03, 2004 4:11 pm
by basdog22
my code is from zend's tutorial.

I finally did it. I exploded my text ("\n")'s and did a $pdf->text("$story[$i]");
thing.

It works great now :D :D :D :D

Posted: Tue Feb 03, 2004 4:27 pm
by pickle
Any chance you're using pdfLib? I just made an automatic billing system using this library. Unfortunately, I don't remember having this particular problem. When I did have trouble with co-ordinates, I did find it helpful to put something outrageous for co-ordinates, then move closer to my target co-ordinate - to see how things went. I suggest putting the $story at something like 200,0 - just to see what happens.

Posted: Wed Feb 04, 2004 8:51 am
by basdog22
This is how i did it:

Code: Select all

<?php
session_start();
$nick=$_SESSION['username'];
$cat=$_GET['cat'];
$news=$_GET['news'];
$aid=$_GET['aid'];
$time=date("d-m-Y");
if(isset($cat) || isset($news) || isset($aid))
{
  if(isset($cat))
  {
    $xquery="select * from news where status='ON' and category_id='$cat'";
  }
  elseif(isset($news))
  {
    $xquery="select * from news where status='ON' and id='$news'";
  }
  elseif(isset($aid))
  {
    $xquery="select * from news where status='ON' and writer='$aid'";
  }
  require 'PDF.php';                                              // Require the class.
  $pdf = &PDF::factory('p', 'a4');                                // Set up the pdf object.
  $pdf->open();                                                   // Start the document.
  $pdf->setCompression(true);                                     // Activate compression.
  $pdf->addPage();                                                // Start a page.
  $pdf->setFont('Courier', 'B', 22);                               // Set font to courier 12 pt.
  $pdf->text(200, 100, 'ZeusCMS News PDF');                       // Text at x=100 and y=100.
  $pdf->setFontSize(20);                                          // Set font size to 20 pt.
  $pdf->image('zeuspdf.jpg', 1, 300);                               // Image at x=100 and y=300.
  //$pdf->setFillColor('rgb', 1, 0, 0);                             // Set text color to red.
  $pdf->text(100, 200, "You are reading the News of $time");      // Text at x=100 and y=200.
  $pdf->setFont('Arial', 'I', 12);
  $pdf->text(100, 250, "This file downloaded by $nick");      // Personalize the PDF

  //$pdf->setDrawColor('rgb', 0, 0, 1);                             // Set draw color to blue.
  //$pdf->line(100, 202, 240, 202);                                 // Draw a line.
  //$pdf->setFillColor('rgb', 1, 1, 0);                             // Set fill/text to yellow.
  //$pdf->rect(200, 300, 100, 100, 'fd');                           // Draw a filled rectangle.
  include "db.php";
  $pages=1;
  $query=mysql_query($xquery);
  while($row=mysql_fetch_array($query))
  {
    $headline=$row['headline'];          //get the headline
    $intro=$row['intro'];                //get the intro
    $story=$row['story'];                //get the story
    $category=$row['category_name'];
    $story=wordwrap($story,125,"\n");    //here we wrap the story by 130 chars
    $story=explode("\n",$story);         //break it into an array
    $intro=wordwrap($intro,125,"\n");    //the same as above
    $intro=explode("\n",$intro);         //break this too
    $writer=$row['writer'];              //get the author
    $pdf->addPage();                                                // Add a new page.

    $pdf->setFont('Arial', 'BI', 9);                               // Set font to arial bold
                                                                    // italic 12 pt.
    $pdf->text(520, 20, "Page: $pages");
    $pages++;                                    //up one page
    $pdf->rect(10, 10, 580, 815, 'd');          //draw the borders
    $pdf->setFont('Arial', 'BI', 16);
    $pdf->setFillColor('rgb', 1, 0, 0);    //set color to red
    $pdf->text(60, 100, $headline);                            // Text at x=100 and y=100.
    $pdf->setFillColor('rgb', 0, 0, 0);    //back to black
    $pdf->setFont('Arial', 'BI', 9);
    $pdf->text(100, 130, "by $writer");

    $pdf->text(50, 170, $intro['0']);
    $p=count($intro);
    $height=170;
    for($a=0;$a<$p;$a++)
    {
      $pdf->text(50, $height, $intro[$a]);
      $height=$height+10;
    }
    $i=count($story);
    $pdf->text(50, 200, $story['0']);
    $height=$height+20;
    for($j=0;$j<$i;$j++)
    {
      $pdf->setFont('Arial', 'BI', 9);
      $pdf->setFillColor('rgb', 0, 0, 0);
      $pdf->text(50, $height, $story[$j]);
      $height=$height+10;
      if($height==800)                                            //if the end of the page
      {
        $pdf->addPage();                                                // Start a new page.
        $pdf->setFont('Courier', 'B', 22);
        $pdf->setFont('Arial', 'BI', 9);
        $pdf->text(520, 20, "Page: $pages");                                    //type the page number
        $pdf->rect(10, 10, 580, 815, 'd');
        $height=90;
        $pages++;                                                       //up one page
      }
    }
    $height=170;
    //$pdf->setLineWidth(4);                                          // Set line width to 4 pt.
    //$pdf->line(100, 202, 240, 202);                                 // Draw a line.
    //$pdf->circle(200, 300, 150, 'd');                               // Draw a non-filled
                                                                  // circle.
  }
?>
it works great and it has a rectangle around the text to make it look better