PHP
  Home arrow PHP arrow Page 2 - Use PHP to Create Dynamic pdf Files
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Use PHP to Create Dynamic pdf Files
By: Ben Shepherd
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 121
    2003-04-07

    Table of Contents:
  • Use PHP to Create Dynamic pdf Files
  • Creating the pdfs on the Fly
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    Use PHP to Create Dynamic pdf Files - Creating the pdfs on the Fly


    (Page 2 of 3 )

    Firstly let’s find the extension that is needed for this exercise. If you installed php on a windows platform, then the php_pdf.dll, on my computer it is located at C:\PHP\php-4.3.1-Win32\extensions. If you are using a linux platform, believe you’ll need to download this dll file.

    Now, in your php.ini file remove the “;” character in front of the extension=php_pdf.dll line. Since we updated the ini file, we must restart the apache web server so that the web server can reinitialize the php with the php_pdf library included.

    Create a php file with the following code,

    <html>
    <head>
    <title>Name Entry Level</title>
    </head>
    <body>
    <table border="0" width="100%" height="100%">
    <tr>
    <td valign="middle" align="center">
    <form method="POST" action="pdf_file.php">
    <p>
    <font size="3" face="Arial">
    Enter your name here please => 
    <input type="text" name="userName" size="20" maxlength="20">
    </font>
    </p>
    <p>
    <input type="submit" value="Submit" name="B1">
    <input type="reset" value="Reset" name="B2">
    </p>
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>

    Now we will grab the post information. That is, the page we take the user information ($user) will be added to the hyperlink and pdf file.

    $user = $HTTP_POST_VARS["userName"];

    Now, we are ready to start. Firstly let’s create a blank pdf file, say bennyboy.pdf. To do this we set a object, say $pdf, to handle pdf manipulations.

    <?php
    $pdf = pdf_new();

    Pass the object at the first position in all of the PHP pdf functions when required for that page. To open the file, code we use the pdf_open_file function.

    pdf_open_file($pdf, "C:\bennyboy.pdf");

    This should create a blank new pdf file size 0kb. The new file has no properties, so let’s assign some. You’ll need to use the pdf_set_info function for this.

    pdf_set_info($pdf, "Author", "Ben Shepherd");
    pdf_set_info($pdf, "Title", "Creating a pdf");
    pdf_set_info($pdf, "Creator", "Ben Shepherd");
    pdf_set_info($pdf, "Subject", "Creating a pdf");

    Now we have all the particulars taken care of let’s do some pdf manipulation. Top begin we need to use the pdf_begin_page function. The parameters, apart from the first which is always $pdf, are measures in of the width and height respectively. A4 is 595 x 842, Letter is 612 x 792 and Legal is 612 x 1008.

    pdf_begin_page($pdf, 595, 842);

    Now it is time to assign a text font for the information to be displayed. Simply use the pdf_findfont and pdf_setfont to do this. I choose the Arial font type with size of 14.

    $arial = pdf_findfont($pdf, "Arial", "host", 1);
    pdf_setfont($pdf, $arial, 14);

    Now we have set the font type, it is time to use it. To display text in the pdf file you must use the pdf_show_xy function. The x-values (i.e. the third parameter), start from the left hand side of the page and move to the right. The y-values start from the bottom of the page and work towards the top.

    So, it is said that, when you work with the pdf_show_xy function the page starts at the bottom left hand corner of the page. So if we wish to type some text 50 units from the left of the page and 400 units from the bottom of the page you would type the following.

    pdf_show_xy($pdf, "<Type your info here>",50, 400);

    But you may not want just text on a page. If you are creating a pdf document for a client you may wish to display a logo. There are functions like pdf_open_gif and pdf_open_jpeg that will open up images and assign them to an object to use in the document.

    $gif_image = pdf_open_gif($pdf, "baseball.gif");

    To put the object onto the pdf file you use the pdf_place_image function with the parameter being pdf file, image file, x-value, y-value and scale repectively.

    pdf_place_image($pdf, $gif_image, 200, 300, 1.0);

    You must close the image to put it out of use.

    pdf_close_image($pdf, $gif_image);
     
    Let’s end the pdf manipulation process by using the pdf_end_page and the pdf_close functions.

    pdf_end_page($pdf);
    pdf_close($pdf);

    Now to view your pdf file, simply create a link to open the pdf in a new window.

    echo "<A HREF=\"C:\bennyboy.pdf\" TARGET=\"_blank\">Open pdf in a new window $user</A>"
    ?>

    More PHP Articles
    More By Ben Shepherd


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway