Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: generate a PDF document using PHP

  1. #1
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default generate a PDF document using PHP

    Hi there, ive been stumped on something for a couple of weeks now and its driving me insane!
    Basically throught PHP (connecting to a MySQL database) my pages will generate tables of data based upon specified dates.
    Anyway, I then want the data thats displayed on the page to be made up into a PDF document (upon user request).

    Ive been on various tutorial websites, ive looked at the suggestions on PHP Manual- but nothing is working. Ive not even come close to getting it to work.

    Has anyone done this before and got code to share that may be of help?

  2. #2
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Are you using the PHP PDF functions?

    http://uk.php.net/pdf

    Sample:

    PHP Code:
    <?php

    try {
        
    $p = new PDFlib();

        
    /*  open new PDF file; insert a file name to create the PDF on disk */
        
    if ($p->begin_document("""") == 0) {
            die(
    "Error: " $p->get_errmsg());
        }

        
    $p->set_info("Creator""hello.php");
        
    $p->set_info("Author""Rainer Schaaf");
        
    $p->set_info("Title""Hello world (PHP)!");

        
    $p->begin_page_ext(595842"");

        
    $font $p->load_font("Helvetica-Bold""winansi""");

        
    $p->setfont($font24.0);
        
    $p->set_text_pos(50700);
        
    $p->show("Hello world!");
        
    $p->continue_text("(says PHP)");
        
    $p->end_page_ext("");

        
    $p->end_document("");

        
    $buf $p->get_buffer();
        
    $len strlen($buf);

        
    header("Content-type: application/pdf");
        
    header("Content-Length: $len");
        
    header("Content-Disposition: inline; filename=hello.pdf");
        print 
    $buf;
    }
    catch (
    PDFlibException $e) {
        die(
    "PDFlib exception occurred in hello sample:\n" .
        
    "[" $e->get_errnum() . "] " $e->get_apiname() . ": " .
        
    $e->get_errmsg() . "\n");
    }
    catch (
    Exception $e) {
        die(
    $e);
    }
    $p 0;
    ?>
    Last edited by Warren Ashcroft; 17th March 2008 at 04:00 AM. Reason: Updated with correct syntax for PHP5
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  3. #3
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    http://www.internal.tascomms.co.uk/test2.php

    This is the error that script generates.

  4. #4
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    Are function names case sensitive, you know I can't even think. Not able to test it for you, but I think the function official is written:
    PHP Code:
    PDF_begin_document() 

  5. #5
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    No, it's not that. In php 5 it looks like you need to use object method calls instead:

    PHP Code:
    $p->begin_document("",""); 
    http://uk3.php.net/manual/en/ref.pdf.php

    I haven't tried it…

  6. #6
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Updated my post...
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  7. #7
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    http://www.internal.tascomms.co.uk/test2.php

    please see above link, im getting a FastCGI Error when I use that exact code.

  8. #8
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    Ive been looking at something called fpdf, not sure if anyone has used it before here?

    But basically this is the code:

    Code:
    <?php
    require('fpdf.php');
    
    class PDF extends FPDF
    {
    //Load data
    function LoadData($file)
    {
        //Read file lines
        $lines=file($file);
        $data=array();
        foreach($lines as $line)
            $data[]=explode(';',chop($line));
        return $data;
    }
    
    //Simple table
    function BasicTable($header,$data)
    {
        //Header
        foreach($header as $col)
            $this->Cell(40,7,$col,1);
        $this->Ln();
        //Data
        foreach($data as $row)
        {
            foreach($row as $col)
                $this->Cell(40,6,$col,1);
            $this->Ln();
        }
    }
    
    //Better table
    function ImprovedTable($header,$data)
    {
        //Column widths
        $w=array(40,35,40,45);
        //Header
        for($i=0;$i<count($header);$i++)
            $this->Cell($w[$i],7,$header[$i],1,0,'C');
        $this->Ln();
        //Data
        foreach($data as $row)
        {
            $this->Cell($w[0],6,$row[0],'LR');
            $this->Cell($w[1],6,$row[1],'LR');
            $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
            $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
            $this->Ln();
        }
        //Closure line
        $this->Cell(array_sum($w),0,'','T');
    }
    
    //Colored table
    function FancyTable($header,$data)
    {
        //Colors, line width and bold font
        $this->SetFillColor(255,0,0);
        $this->SetTextColor(255);
        $this->SetDrawColor(128,0,0);
        $this->SetLineWidth(.3);
        $this->SetFont('','B');
        //Header
        $w=array(40,35,40,45);
        for($i=0;$i<count($header);$i++)
            $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
        $this->Ln();
        //Color and font restoration
        $this->SetFillColor(224,235,255);
        $this->SetTextColor(0);
        $this->SetFont('');
        //Data
        $fill=0;
        foreach($data as $row)
        {
            $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
            $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
            $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
            $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
            $this->Ln();
            $fill=!$fill;
        }
        $this->Cell(array_sum($w),0,'','T');
    }
    }
    
    $pdf=new PDF();
    //Column titles
    $header=array('Country','Capital','Area (sq km)','Pop. (thousands)');
    //Data loading
    $data=$pdf->LoadData('countries.txt');
    $pdf->SetFont('Arial','',14);
    $pdf->AddPage();
    $pdf->BasicTable($header,$data);
    $pdf->AddPage();
    $pdf->ImprovedTable($header,$data);
    $pdf->AddPage();
    $pdf->FancyTable($header,$data);
    $pdf->Output();
    ?>
    and it outputs this: http://www.internal.tascomms.co.uk/tutorial/tuto5.php

    The only thing is that the data is presented in a text file like this:

    Code:
    Austria;Vienna;83859;8075
    Belgium;Brussels;30518;10192
    Denmark;Copenhagen;43094;5295
    Finland;Helsinki;304529;5147
    France;Paris;543965;58728
    Germany;Berlin;357022;82057
    Greece;Athens;131625;10511
    Ireland;Dublin;70723;3694
    Italy;Roma;301316;57563
    Luxembourg;Luxembourg;2586;424
    Netherlands;Amsterdam;41526;15654
    Portugal;Lisbon;91906;9957
    Spain;Madrid;504790;39348
    Sweden;Stockholm;410934;8839
    United Kingdom;London;243820;58862
    

    im not sure how I would adjust this to work with mySQL database and bring data using code similar to this:

    Code:
    $GetList2 = "SELECT * FROM internal_items WHERE order_id = $displayOrderID";
    $ResultList2 = mysql_query($GetList2);
    $items2 = mysql_num_rows($ResultList2);
     
    while ($iitems2 < $items2) {
    $itemID = mysql_result($ResultList2,$iitems2,"id");
    $quantity = mysql_result($ResultList2,$iitems2,"quantity");
    $unit_price = mysql_result($ResultList2,$iitems2,"unit_price");
    $line_price = mysql_result($ResultList2,$iitems2,"line_price");
    $description = mysql_result($ResultList2,$iitems2,"description");
    $quantityHSC = stripslashes (htmlspecialchars ($quantity));
    $unit_priceHSC = stripslashes (htmlspecialchars ($unit_price));
    $descriptionHSC = stripslashes (htmlspecialchars ($description));
     
    echo "<tr>\n";
    echo "<td class=\"tableCell\" align=\"left\"><p class=\"tableCellText\">\n";
    echo "$quantityHSC\n";
    echo "</p></td>\n";
    echo "<td class=\"tableCell\" align=\"left\"><p class=\"tableCellText\">\n";
    echo "$descriptionHSC\n";
    echo "</p></td>\n";
    echo "<td class=\"tableCell\" align=\"left\"><p class=\"tableCellText\">\n";
    $unit_priceDisplay = number_format($unit_priceHSC, 2);
    echo "£$unit_priceDisplay\n";
    echo "</p></td>\n";
    echo "<td class=\"tableCell\" align=\"left\"><p class=\"tableCellText\">\n";
    $line_priceDisplay = number_format($line_price, 2);
    echo "£$line_priceDisplay\n";
    echo "</p></td>\n";
    echo "</tr>\n";
    $iitems2++;
    }
    

  9. #9
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    here is some other code im trying, still get errors:

    http://www.internal.tascomms.co.uk/test5.php

    Code:
    <?php
    try {
    // create new instance of the 'PDFlib' class
    $pdf=new PDFlib();
    // open new PDF file
    if(!$pdf->begin_document("","")){
    throw new PDFlibException("Error creating PDF document. ".$pdf->get_errmsg());
    }
    $pdf->set_info("Creator","pdf_example.php");
    $pdf->set_info("Author","Alejandro Gervasio");
    $pdf->set_info("Title","Example on using PHP to create PDF docs");
    $pdf->begin_page_ext(595,842,"");
    $font=$pdf->load_font("Helvetica-Bold","winansi","");
    $pdf->setfont($font,24.0);
    $pdf->set_text_pos(50,800);
    $pdf->show("PHP is great for creating PDF documents!");
    // end page
    $pdf->end_page_ext("");
    // end document
    $pdf->end_document("");
    // get buffer contents
    $buffer=$pdf->get_buffer();
    // get length of buffer
    $len=strlen($buffer);
    // display PDF document
    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=example.pdf");
    echo $buffer;
    }
    catch (PDFlibException $e){
    echo 'Error Number:'.$e->get_errnum()."n";
    echo 'Error Message:'.$e->get_errmsg();
    exit();
    }
    ?>

  10. #10
    Join Date
    Feb 2005
    Posts
    153
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    FastCGI Error

    The FastCGI Handler was unable to process the request. Error Details:
    • The FastCGI process exited unexpectedly
    • Error Number: -2147467259 (0x80004005).
    • Error Description: Unspecified error
    HTTP Error 500 - Server Error.
    Internet Information Services (IIS)




    im wondering if something on my server isnt set up correctly?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PDF to JPEG/PNG?
    By Mr Ed in forum PHP
    Replies: 3
    Last Post: 29th September 2006, 05:20 PM
  2. PDF's
    By Furzetech in forum Sales and Service Feature Enquiries
    Replies: 6
    Last Post: 29th September 2006, 01:51 PM
  3. Default Document
    By CookingFat in forum General Technical Support
    Replies: 6
    Last Post: 11th August 2006, 07:50 PM
  4. Opening PDF files in IE
    By medasset in forum General Technical Support
    Replies: 9
    Last Post: 5th June 2006, 10:44 AM

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
  •