Saturday, January 22, 2011

How To Create PDF With Php ?

The first thing to do, when we want to generate a PDF file
with PHP, is to check that the PHP libraries, that support the
creation of PDF files, are present and available to PHP.
When you install a PHP distribution on your own home test-machine,
you have to make shure that these PHP PDF libraries are available.
On the astahost.com server, they are available and active.
Here is how to check this:
- Create a new text file, let's name it phpinfo.php.
- Insert the following code:



Upload it to your web site.
- Browse to your phpinfo.php file on your web site.
- Check the results.

The phpinfo() function generates a html page with all the information
about the PHP installation, including the information about the Apache
web server, the version and settings of the PHP compiler, and the
supplemental PHP libraries that are installed and activated.

We have to check the pdf libraries.
The results should look something like this:

PDF Support: enabled
PDFlib Gmbh Version: 5.0.3
Revision: $Revision: 1.112.2.11 $

(This is the result of astahost.com - your own home test-machine may show
a different result, depending on your PHP distribution.)

Now that we have confirmation that the PDF libraries for PHP are OK,
let's go on with our first PHP application:


(2)
A simple test.

The creation of a PDF with PHP takes place a the web server, in memory.
The first thing to do is to create a PHP PDF object,
and the last thing to do is to destroy it.
Here is that bit of code:
CODE


With this PHP PDF object, let's create a PDF file. In memory, not on the
file-system of the web-server.
The first thing to do is to open a PHP PDF file,
and the last thing to do is to close it.
Here is that bit of code:
CODE



In between these instructions, we will insert the rest of the PHP code.

Once the PHP PDF file is open, we can start writing PDF pages to it.
The first thing to do is to start a PHP PDF page,
and the last thing to do is to end it.
Here is that bit of code:
CODE


First we have to choose a text font, and a text font size, in points.
Here is that bit of code:
CODE

Our example can now start to show some text on the page.
Here is that bit of code:
CODE


Now the PHP PDF file - in memory is complete.
How do we get it into the browser of our surfer?

Here is that bit of code:
CODE


Save this code as a new text file, let's name it gen01.php.
- Upload it to your web site.
- Browse to your gen01.php file on your web site.
- Check the results.
- Your first PDF, generated by PHP, will show on your machine ! Congratulations !


So, where do we go from here?
Let's add some graphics to our PDF page.
CODE
$myimage =
PDF_open_image_file($mypdf, "jpeg", "training_bground.jpg");
PDF_place_image($mypdf,
$myimage, 50, 650, 0.6);

PHP PDF function PDF_open_image_file() looks for the specified image file on the web server,
and sets the $myimage PHP variable.
Then the PHP PDF function PDF_place_image() uses the PHP PDF object,
and the $myimage PHP variable to place a copy of the image onto the PDF page.
Here is the complete code:
CODE


- Save this code as a new text file, let's name it gen02.php.
- Upload it to your web site.
- Browse to your gen02.php file on your web site.
- Check the results.

The PHP PDF function PDF_setcolor() takes several parameters.
CODE
PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);


The actual drawing is a three-stage process:
- Determine the first point. PDF_moveto()
- Determine the second pont. PDF_lineto()
- Draw. PDF_stroke()
CODE
PDF_moveto($mypdf, 20, 735);
PDF_lineto($mypdf,
575, 735);
PDF_stroke($mypdf);

No comments:

Post a Comment