Home arrow PHP arrow Page 2 - Use PHP to Create Dynamic pdf Files

Use PHP to Create Dynamic pdf Files

The php_pdf.dll extension is enough to produce pdf files on the fly. Once you have read this article you may start to produce high quality pdf files for your clients.

Author Info:
By: Ben Shepherd
Rating: 4 stars4 stars4 stars4 stars4 stars / 270
April 07, 2003
  1. · Use PHP to Create Dynamic pdf Files
  2. · Creating the pdfs on the Fly
  3. · Conclusion

print this article

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,

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

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.

$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.


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>"

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials