SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP to PDF using fPDF

    Hi ,
    i want a php (whose contents are in mysql db) and are accessed as $row['title'], $row['content'] etc; to be converted into pdf. I am using fpdf for doing so. In it's tutorial they have given the following code for header, footer and content generation.

    PHP Code:
    <?php
    //$lastPage = ceil($numRows/$perPage);
    // Database Connection
    include("$_SERVER[DOCUMENT_ROOT]/includes/connect.inc.php");
    include(
    "$_SERVER[DOCUMENT_ROOT]/includes/stripgpcslash.inc.php");
    //pdf

    include('../fpdf.php');


    // If current page number, use it
    // if not, set one!

    if(!isset($_GET['page']))
      {
        
    $page 1;
      }
    else 
      {
        if(
    ctype_digit($_GET['page']))
          {
         
    $page=trim(mysql_real_escape_string($_GET['page']));
          }
        else
          {
        echo 
    "invalid query";
            exit;
            }
       }

    // Define the number of results per page
    $max_results 1;

    // Figure out the limit for the query based
    // on the current page number.
    $from = (($page $max_results) - $max_results); 

    // Perform MySQL query on only the current page number's results

    $sql mysql_query("SELECT * FROM wow WHERE `trusted` = 0 ORDER BY `id` ASC LIMIT $from$max_results");

    while(
    $row mysql_fetch_array($sql)){
    class 
    PDF extends FPDF
    {
    //Page header
    function Header()
    {
        
    //Logo
        
    $this->Image('logo_pb.jpg',10,8,33);
        
    //Arial bold 15
        
    $this->SetFont('Arial','B',15);
        
    //Move to the right
        
    $this->Cell(80);
        
    //Title
        
    $this->Cell(30,10,$row['title'],1,0,'C');
        
    //Line break
        
    $this->Ln(20);
    }

    //Page footer
    function Footer()
    {
        
    //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');
    }
    }

    //Instanciation of inherited class
    $pdf=new PDF();
    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Times','',12);
    $pdf->Cell(0,10,$row['content'],0,1);
    $pdf->Output();


    //pdf
      

    }


    mysql_close($con);
    ?>
    Instead of title i want to print the $row['title'] . hen i use the above code it says

    Notice: Undefined variable: row in c:\program files\easyphp1-8\www\fpdf\tutorial\pages.php on line 55

    Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\fpdf\tutorial\pages.php:55) in c:\program files\easyphp1-8\www\fpdf\fpdf.php on line 1022
    FPDF error: Some data has already been output to browser, can't send PDF file. How do i print the $row['title'] instead of static 'title'?
    Last edited by krishnakhanna; Aug 26, 2007 at 23:40.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of putting the class definition in a loop.. you should place it outside and then create instances of the class from within the loop. Example:

    Code:
    class PDF extends FPDF
    {
    // bla
    }
    
    while ($row = ...)
    {
    $pdf = new PDF();
    $pdf->createHeader($row);
    }
    and so on.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason for the first error message is likely because you're query is failing or is bringing back no results. Thoughout the entire process of connecting to and using a database, or any other external program, you can encounter errors or other conditions that you need to compensate for.

    One of those is that a query fails due to an syntax error or a table, field or database name in the query that doesn't exist. Another is when you get no results from a query.

    The second error is due to the first, as PHP is outputting error text before the headers appropriate for sending the PDF have been sent.

    Class definitions and function definitions should be done in a separate file, or at least at the top of the script, and as mentioned not in a loop.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •