Jump to content
xisto Community
signatureimage

How To: Create PDF With Php Create on-the-fly PDF on the web server

Recommended Posts

In this tutorial, we will explore the possibilities

of generating a PDF file - on-the-fly - with PHP.

 

The samples that are presented can be run on Xisto.com.

 

 

Why would we want to generate a PDF on-the-fly ?

 

Well, we might want to include in the PDF some data that

must be entered by our surfer, by means of a html form.

Or we might want to include in the PDF some data that comes

from a database that is updated by another process.

Or some other reason. You invent one. All reasons are legit!

 

 

(1)

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 Xisto.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:

<?phpinfo();?>
- 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 Xisto.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:

<?php$mypdf = PDF_new();PDF_delete($mypdf);?>
In between these two instructions, we will insert the rest of the PHP 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:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_close($mypdf);PDF_delete($mypdf);?>
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:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);PDF_end_page($mypdf);PDF_close($mypdf);PDF_delete($mypdf);?>
In between these instructions, we will insert the rest of the PHP code.

 

What do these two numbers (595 and 842) mean?

They are the size of the page, measured in 1/72 inch.

Our example specifies a width of 595 points = 595 x 1/72 inch = A4 paper

Our example specifies a height of 842 points = 842 x 1/72 inch = A4 paper

Your needs may vary...

 

Once the PHP PDF page has been started, we can start writing dots to it.

Let's write some text.

First we have to choose a text font, and a text font size, in points.

Here is that bit of code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_end_page($mypdf);PDF_close($mypdf);PDF_delete($mypdf);?>
The PHP PDF function PDF_findfont() looks for the Times-Roman font,

and sets the $myfont PHP variable.

Then the PHP PDF function PDF_setfont() uses the PHP PDF object,

and the $myfont PHP variable to select a font text size of 10 points.

 

Our example can now start to show some text on the page.

Here is that bit of code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Sample PDF, constructed by PHP in real-time.", 50, 750);PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);PDF_end_page($mypdf);PDF_close($mypdf);PDF_delete($mypdf);?>
The PHP PDF function PDF_show_xy() prints the text at the specified positions.

X=50 and Y=750 for the first text line, and

X=50 and Y=730 for the second text line.

Again, the values are in 1/72 inch.

The origin (0,0) is in the BOTTOM-lefthand corner!

So: Y=750 is ABOVE Y=730!

 

Now the PHP PDF file - in memory is complete.

How do we get it into the browser of our surfer?

We will use the PHP PDF function PDF_get_buffer() to obtain a copy of the PHP PDF object

in our own memory buffer, determine the length of it, create the http headers that are required

for a PDF file, and let the web server send everything to the browser of the surfer:

Here is that bit of code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Sample PDF, constructed by PHP in real-time.", 50, 750);PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen01.pdf");print $mybuf;PDF_delete($mypdf);?>
- 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.

$myimage = PDF_open_image_file($mypdf, "jpeg", "training_bground.jpg");PDF_place_image($mypdf, $myimage, 50, 650, 0.6);
The 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:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Sample PDF, constructed by PHP in real-time.", 50, 750);PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);PDF_show_xy($mypdf, "A JPEG image, on 60 % of its original size.", 50, 710);$myimage = PDF_open_image_file($mypdf, "jpeg", "training_bground.jpg");PDF_place_image($mypdf, $myimage, 50, 650, 0.6);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen02.pdf");print $mybuf;PDF_delete($mypdf);?>
- 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.

 

Of course you will have to upload the jpeg image also !!!!!

 

 

And we can add line-drawing functions too.

The PHP PDF function PDF_setcolor() takes several parameters.

PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);
The first parameter is the PHP PDF object.

The second parameter instructs that the color is used for drawing lines.

The third parameter indicates that the color definition will be in RGB (Red-Green-Blue).

The following parameters are the values for the Red, Green, and Blue components of the color.

Our example specifies 100% Red (1) - 0% Green (0) - 0% Blue (0).

Look, another PDF quirck: 1 means 100%.

 

The actual drawing is a three-stage process:

- Determine the first point. PDF_moveto()

- Determine the second pont. PDF_lineto()

- Draw. PDF_stroke()

PDF_moveto($mypdf,  20, 735);PDF_lineto($mypdf, 575, 735);PDF_stroke($mypdf);
Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 12);$myimage = PDF_open_image_file($mypdf, "jpeg", "training_bground.jpg");PDF_place_image($mypdf, $myimage, 30, 750, 0.6);// RGB colors: 1 = 100 % (255)PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);PDF_moveto($mypdf,  20, 735);PDF_lineto($mypdf, 575, 735);PDF_stroke($mypdf);PDF_moveto($mypdf,  20, 55);PDF_lineto($mypdf, 575, 55);PDF_stroke($mypdf);PDF_show_xy($mypdf, "A sample document.", 50, 40);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen03.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen03.php.

- Upload it to your web site.

- Browse to your gen03.php file on your web site.

- Check the results.

 

 

 

Besides lines, we can also draw rectangles and circles. And fill them with a color.

The sample PHP PDF function PDF_setcolor($mypdf, "fill", "rgb", 1, 1, 0);

prepares the fill color as yellow (Red = 100% - Green = 100% - Blue = 0%)

The sample PHP PDF function PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);

prepares the draw color as black (Red = 0% - Green = 0% - Blue = 0%)

 

The actual drawing is a two-stage process:

- Determine the area. PDF_rect() or PDF_circle()

- Draw. PDF_fill_stroke()

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Drawings in PDF.", 450, 765);// RGB colors: 1 = 100 % (255)PDF_setcolor($mypdf, "fill", "rgb", 1, 1, 0);PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);PDF_rect($mypdf, 50, 500, 200, 300);PDF_fill_stroke($mypdf);PDF_setcolor($mypdf, "fill", "rgb", 0, 1, 0);PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 1);PDF_circle($mypdf, 400, 600, 100);PDF_fill_stroke($mypdf);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen04.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen04.php.

- Upload it to your web site.

- Browse to your gen04.php file on your web site.

- Check the results.

 

 

 

With the PHP instruction for () {} to do some repetitive things,

we can easily create a page that contains a matrix of small squares:

First the vertical lines, left to right:

for ($x=0; $x<=595; $x+=20) { PDF_moveto($mypdf, $x, 0); PDF_lineto($mypdf, $x, 842); PDF_stroke($mypdf); }
Then the horizontal lines, bottom to top:

for ($y=0; $y<=842; $y+=20) { PDF_moveto($mypdf, 0, $y); PDF_lineto($mypdf, 595, $y); PDF_stroke($mypdf); }

Moreover, let's make two pages, slightly different.

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_parameter($mypdf, "openaction", "fitpage");PDF_begin_page($mypdf, 595, 842);PDF_setcolor($mypdf, "stroke", "rgb", 0.90, 0.90, 0.90);for ($x=0; $x<=595; $x+=20) { PDF_moveto($mypdf, $x, 0); PDF_lineto($mypdf, $x, 842); PDF_stroke($mypdf); }for ($y=0; $y<=842; $y+=20) { PDF_moveto($mypdf, 0, $y); PDF_lineto($mypdf, 595, $y); PDF_stroke($mypdf); }$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Drawingpaper.", 450, 765);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setcolor($mypdf, "stroke", "rgb", 0.725, 0.725, 0.725);for ($x=0; $x<=595; $x+=50) { PDF_moveto($mypdf, $x, 0); PDF_lineto($mypdf, $x, 842); PDF_stroke($mypdf); }for ($y=0; $y<=842; $y+=50) { PDF_moveto($mypdf, 0, $y); PDF_lineto($mypdf, 595, $y); PDF_stroke($mypdf); }PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen05.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen05.php.

- Upload it to your web site.

- Browse to your gen05.php file on your web site.

- Check the results.

 

Those of you who were very eager to learn, will have noticed one new PHP PDF function.

Read the previous code again and try to find it. (It's in the begining of the code.)

Or cheat and read the next paragraph.

 

 

 

 

 

 

 

The PHP PDF function PDF_set_parameter($mypdf, "openaction", "fitpage"); allows you

to have some influence over the behaviour of the Adobe Acrobat Reader,

when the PDF is shown to your surfer.

The setting of PDF parameters will be elaborated upon later in this post.

But now is the time to add some PDF properties to our PDF file:

The PHP PDF function PDF_set_info(); allows you to add meta information

to the PDF file. Meta information is information about information. The information we will

add will be visible with the full version of the Adobe Acrobat Reader, as document properties.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen06.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Sample with the Document Properties. Can be observed in the full Acrobat Reader.", 50, 750);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen06.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen06.php.

- Upload it to your web site.

- Browse to your gen06.php file on your web site.

- Check the results.

 

 

 

So, what have we learned so far?

- We create and destroy a PHP PDF object.

- We open and close a PHP PDF file - in memory.

- We start and end a PDF page.

- We select a text-font and a text-font-size.

- We draw text at a specified place on the page.

- We select a fill color and a stroke color.

- We draw lines, filled rectangles and circles.

- We have influence on the behaviour of the Adobe Acrobat Reader.

- We add meta data to the PDF file.

- We copy the PDF file into a PHP buffer and send this - via http - to the browser of the surfer.

 

Neat. What now?

 

Let's find out how many lines of text we can put on one page, shall we?

First approach: we use the PHP PDF function PDF_show_xy(); to draw the text,

and with the PHP instruction for () {} we determine the vertical position of the line.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen07.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);for ($y=820; $y>=00; $y-=15) { PDF_show_xy($mypdf, $y, 50, $y); PDF_show_xy($mypdf, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 70, $y);}PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen07.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen07.php.

- Upload it to your web site.

- Browse to your gen07.php file on your web site.

- Check the results.

 

 

But why bother with calculating the vertical position ourselves?

What if we change the text font size?

Let's have the PHP PDF library do the hard work for us.

Second approach: we use the PHP PDF function PDF_continue_text(); to draw the text,

and we let the PHP PDF library calculate the exact vertical position of the line, according to

the selected text font size...

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen08.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_set_parameter($mypdf, "openaction", "fitpage");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Times-Roman; 10 points. First line on: x = 30; y = 830", 30, 830);for ($y=0; $y<=80; $y++) { PDF_continue_text($mypdf, "80 Lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");} PDF_continue_text($mypdf, "Last line. (Line 82)");PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen08.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen08.php.

- Upload it to your web site.

- Browse to your gen08.php file on your web site.

- Check the results.

 

So, now we know that Times-Roman 10 point text fits 82 lines on a A4 page.

How about Times Roman 12 point?

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen09.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_set_parameter($mypdf, "openaction", "fitpage");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 12);PDF_show_xy($mypdf, "Times-Roman; 12 points. First line on: x = 30; y = 830", 30, 830);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 Lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");} PDF_continue_text($mypdf, "Last line. (Line 68)");PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen09.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen09.php.

- Upload it to your web site.

- Browse to your gen09.php file on your web site.

- Check the results.

 

Yes, now we know that Times Roman 12 point text fits only 68 lines on a A4 page.

 

 

Let's compare three different text font sizes in one single PDF file, on three PDF pages.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen10.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);PDF_show_xy($mypdf, "Times-Roman; Page one", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 12);PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);PDF_show_xy($mypdf, "Times-Roman; Page two", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 14);PDF_setcolor($mypdf, "stroke", "rgb", 1, 0, 0);PDF_show_xy($mypdf, "Times-Roman; Page three", 50, 800);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen10.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen10.php.

- Upload it to your web site.

- Browse to your gen10.php file on your web site.

- Check the results.

 

 

Enough simple text PDF pages.

 

 

 

Ever wondered how they create these left-side "Bookmarks" and "Thumbnails" in PDF?

Well we can create that with PHP too !

 

Let's create the "Thumbnails" first.

The PHP PDF function PDF_add_thumbnail(); will create one "Thumnail" image

for the PDF page that we are creating. So, this function must be called for every PDF page.

When the Adobe Acrobat Reader presents the PDF file, your surfer will be able to click on one

of the "Thumbnail" images, and the Adobe Acrobat Reader will then jump to the appropriate PDF page.

Furthermore, we will use the PHP PDF function PDF_set_parameter($mypdf, "openmode", "thumbnails");

to instruct the Adobe Acrobat Reader to show the "Thumbnail" tab when the PDF file is opened.

 

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen11.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_set_parameter($mypdf, "openmode", "thumbnails");PDF_set_parameter($mypdf, "openaction", "fitpage");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page one", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board1.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page two", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board2.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page three", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board3.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page four", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board4.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page five", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board5.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page six", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board6.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page seven", 50, 800);$myimage = PDF_open_image_file($mypdf, "jpeg", "board7.jpg");PDF_place_image($mypdf, $myimage, 90, 750, 0.5);PDF_place_image($mypdf, $myimage, 90, 650, 0.5);PDF_place_image($mypdf, $myimage, 90, 550, 0.5);PDF_place_image($mypdf, $myimage, 90, 450, 0.5);PDF_place_image($mypdf, $myimage, 90, 350, 0.5);PDF_place_image($mypdf, $myimage, 90, 250, 0.5);PDF_place_image($mypdf, $myimage, 190, 750, 0.5);PDF_place_image($mypdf, $myimage, 190, 650, 0.5);PDF_place_image($mypdf, $myimage, 190, 550, 0.5);PDF_place_image($mypdf, $myimage, 190, 450, 0.5);PDF_place_image($mypdf, $myimage, 190, 350, 0.5);PDF_place_image($mypdf, $myimage, 190, 250, 0.5);PDF_place_image($mypdf, $myimage, 290, 750, 0.5);PDF_place_image($mypdf, $myimage, 290, 650, 0.5);PDF_place_image($mypdf, $myimage, 290, 550, 0.5);PDF_place_image($mypdf, $myimage, 290, 450, 0.5);PDF_place_image($mypdf, $myimage, 290, 350, 0.5);PDF_place_image($mypdf, $myimage, 290, 250, 0.5);PDF_place_image($mypdf, $myimage, 390, 750, 0.5);PDF_place_image($mypdf, $myimage, 390, 650, 0.5);PDF_place_image($mypdf, $myimage, 390, 550, 0.5);PDF_place_image($mypdf, $myimage, 390, 450, 0.5);PDF_place_image($mypdf, $myimage, 390, 350, 0.5);PDF_place_image($mypdf, $myimage, 390, 250, 0.5);PDF_add_thumbnail($mypdf,$myimage);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen11.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen11.php.

- Upload it to your web site.

- Browse to your gen11.php file on your web site.

- Check the results.

 

Of course you will have to upload the jpeg images also !!!!!

 

 

 

And now is the time to create PDF bookmarks.

The PHP PDF function PDF_add_bookmark(); will create one "Bookmark"

for the PDF page that we are creating. So, this function must be called for every PDF page.

When the Adobe Acrobat Reader presents the PDF file, your surfer will be able to click on one

of the "Bookmark" lines, and the Adobe Acrobat Reader will then jump to the appropriate PDF page.

Furthermore, we will use the PHP PDF function PDF_set_parameter($mypdf, "openmode", "bookmarks");

to instruct the Adobe Acrobat Reader to show the "Bookmarks" tab when the PDF file is opened.

 

What is so special about the PDF bookmarks implementation, is the fact that the PDF bookmarks

are "hierarchical". This means that the PDF bookmarks view is like a tree view, with different levels.

Furthermore, some of these levels are visible, while other levels are invisible, or "collapsed".

The PHP PDF function PDF_add_bookmark(); takes the required parameters to

obtain these results.

- The first parameter is the PHP PDF object.

- The second parameter is the text of the bookmark.

- The third parameter is the level (0 for the highest level)

- The fourth parameter is 1 for children-are-visible, and 0 for children-are-invisible (collapsed).

Sample:

$mytopparent = PDF_add_bookmark($mypdf, "First page", 0, 1);$mychild = PDF_add_bookmark($mypdf, "Second page", $mytopparent, 1);$mytopparent = PDF_add_bookmark($mypdf, "Third page", 0, 1);$mychild = PDF_add_bookmark($mypdf, "Fourth page", $mytopparent, 0);$mygrandchild = PDF_add_bookmark($mypdf, "Fifth page", $mychild, 1);$mygrandchild = PDF_add_bookmark($mypdf, "Sixth page", $mychild, 1);$mytopparent = PDF_add_bookmark($mypdf, "Seventh page", 0, 1);

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen12.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_set_parameter($mypdf, "openmode", "bookmarks");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page one", 50, 800);$mytopparent = PDF_add_bookmark($mypdf, "First page", 0, 1);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page two", 50, 800);$mychild = PDF_add_bookmark($mypdf, "Second page", $mytopparent, 1);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page three", 50, 800);$mytopparent = PDF_add_bookmark($mypdf, "Third page", 0, 1);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page four", 50, 800);$mychild = PDF_add_bookmark($mypdf, "Fourth page", $mytopparent, 0);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page five", 50, 800);$mygrandchild = PDF_add_bookmark($mypdf, "Fifth page", $mychild, 1);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page six", 50, 800);$mygrandchild = PDF_add_bookmark($mypdf, "Sixth page", $mychild, 1);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page seven", 50, 800);$mytopparent = PDF_add_bookmark($mypdf, "Seventh page", 0, 1);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen12.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen12.php.

- Upload it to your web site.

- Browse to your gen12.php file on your web site.

- Check the results.

 

 

 

How about some Powerpoint Presentation Effects ?

The Adobe Acrobat Reader is capable of showing some nifty page-transition effects.

We will use PHP to add the instructions required for these effects into our PDF file.

The PHP PDF function PDF_set_parameter($mypdf, "transition", $effect); will be used.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen13.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Demonstration of PDF effects.  Please Paginate.", 50, 800);PDF_set_parameter($mypdf, "openaction", "fitpage");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page one", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "split");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page two", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page three", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "blinds");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page four", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page five", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "box");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page six", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page seven", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "wipe");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page eight", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page nine", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "dissolve");PDF_set_value($mypdf, "duration", 3);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page ten", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page eleven", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "glitter");PDF_set_value($mypdf, "duration", 3);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page twelve", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page thirteen", 50, 800);for ($y=0; $y<=66; $y++) { PDF_continue_text($mypdf, "66 lines. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}PDF_set_parameter($mypdf, "transition", "replace");PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen13.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen13.php.

- Upload it to your web site.

- Browse to your gen13.php file on your web site.

- Check the results.

 

 

 

And one more particularity of the PDF file, is the possibility to add "notes".

The PHP PDF function PDF_add_note(); will be used.

When the Adobe Acrobat Reader presents the PDF file to your surfer, he/she will

have the possibility to open the note by double-clicking on the little note icon,

placed somewhere in the text of the PDF page.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen14.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "When we look more closely at this way of working,", 50, 750);PDF_add_note($mypdf, 245, 660, 550, 770, "Yes, what is going to happen here? We are not used to work that way, and now this statement stares at us ! ", "What do you mean, WORK ?", "note", 0);PDF_show_xy($mypdf, "we can clearly see the more open side of this project.", 50, 730);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = s

Edited by vujsa (see edit history)

Share this post


Link to post
Share on other sites

After reviewing my post, I found out that the original post was truncated.

 

I will continue here, and restart with the sample # 14:

 

 

 

And one more particularity of the PDF file, is the possibility to add "notes".

The PHP PDF function PDF_add_note(); will be used.

When the Adobe Acrobat Reader presents the PDF file to your surfer, he/she will

have the possibility to open the note by double-clicking on the little note icon,

placed somewhere in the text of the PDF page.

 

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen14.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "When we look more closely at this way of working,", 50, 750);PDF_add_note($mypdf, 245, 660, 550, 770, "Yes, what is going to happen here? We are not used to work that way, and now this statement stares at us ! ", "What do you mean, WORK ?", "note", 0);PDF_show_xy($mypdf, "we can clearly see the more open side of this project.", 50, 730);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen14.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen14.php.

- Upload it to your web site.

- Browse to your gen14.php file on your web site.

- Check the results.

 

 

 

 

And how about a PDF file with links?

Well, the PDF format was in use BEFORE the global proliferation of the Internet

and the World Wide Web. Linking was already provided for in a PDF file.

The Adobe Acrobat Reader will happily jump to a PDF page when the user clicks

on a PDF link. Let's add some links. Let's create a "Table Of Contents" page

at the beginning of our PDF file.

How is it done?

- Draw the text lines that will function as Table Of Contents text.

- Draw some rectangles around these text lines, as a visual clue that it are links,

and add a link towards other PDF pages to the surface area delimited by the rectangles.

 

Sample: Draw the text lines of the Table Of Contents PDF page:

PDF_show_xy($mypdf, "First page",  150, 670);PDF_show_xy($mypdf, "Second page",  150, 655);PDF_show_xy($mypdf, "Third page",   150, 640);PDF_show_xy($mypdf, "Fourth page",  150, 625);PDF_show_xy($mypdf, "Fifth page",  150, 610);PDF_show_xy($mypdf, "Sixth page",   150, 595);PDF_show_xy($mypdf, "Seventh page", 150, 580);

Sample: Add a link to the other PDF pages by drawing light-blue dashed rectangles:

PDF_set_border_style($mypdf, "dashed", 1);PDF_set_border_color($mypdf, 0.8, 0.8, 1);PDF_set_border_dash($mypdf, 3, 1);PDF_add_locallink($mypdf, 140, 670-3, 225, 670+9, 2, "retain");PDF_add_locallink($mypdf, 140, 655-3, 225, 655+9, 3, "retain");PDF_add_locallink($mypdf, 140, 640-3, 225, 640+9, 4, "retain");PDF_add_locallink($mypdf, 140, 625-3, 225, 625+9, 5, "retain");PDF_add_locallink($mypdf, 140, 610-3, 225, 610+9, 6, "retain");PDF_add_locallink($mypdf, 140, 595-3, 225, 595+9, 7, "retain");PDF_add_locallink($mypdf, 140, 580-3, 225, 580+9, 8, "retain");

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen15.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 20);PDF_show_xy($mypdf, "Table of contents", 50, 700);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "First page",  150, 670);PDF_show_xy($mypdf, "Second page",  150, 655);PDF_show_xy($mypdf, "Third page",   150, 640);PDF_show_xy($mypdf, "Fourth page",  150, 625);PDF_show_xy($mypdf, "Fifth page",  150, 610);PDF_show_xy($mypdf, "Sixth page",   150, 595);PDF_show_xy($mypdf, "Seventh page", 150, 580);PDF_set_border_style($mypdf, "dashed", 1);PDF_set_border_color($mypdf, 0.8, 0.8, 1);PDF_set_border_dash($mypdf, 3, 1);PDF_add_locallink($mypdf, 140, 670-3, 225, 670+9, 2, "retain");PDF_add_locallink($mypdf, 140, 655-3, 225, 655+9, 3, "retain");PDF_add_locallink($mypdf, 140, 640-3, 225, 640+9, 4, "retain");PDF_add_locallink($mypdf, 140, 625-3, 225, 625+9, 5, "retain");PDF_add_locallink($mypdf, 140, 610-3, 225, 610+9, 6, "retain");PDF_add_locallink($mypdf, 140, 595-3, 225, 595+9, 7, "retain");PDF_add_locallink($mypdf, 140, 580-3, 225, 580+9, 8, "retain");PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page one", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page two", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page three", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page four", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page five", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page six", 50, 800);PDF_end_page($mypdf);PDF_begin_page($mypdf, 595, 842);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Page seven", 50, 800);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen15.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen15.php.

- Upload it to your web site.

- Browse to your gen15.php file on your web site.

- Check the results.

 

 

 

 

 

 

So, we now have learned enough basic PDF writing stuff to finally

make the web application we were interested in in the first place:

 

- Accept some data from our surfer, via a html form.

- Run a PHP script that interprets this data, and generates a unique PDF - on-the-fly.

 

 

This is the scheme:

- We present a html form (form16.html)

- The surfers enters some data, and submits the form.

- The form action calls a PHP script (gen16.php)

- The PHP script interprets the data, entered by the surfer.

- The PHP script generates a PDF file and sends it - via http - to the surfer's browser.

- The surfer is impressed !

 

We will ask the surfer to enter a series of numbers, separated by commas. Let's say 5 numbers or so.

We will then generate a PDF file with a colored pie-chart, based on these numbers. Cool !

 

Part One: The html form.

Here is the complete code:

<html><head><title>pdf by php</title><style>body  {font-family:verdana,arial;font-size:12pt;}p     {font-family:verdana,arial;font-size:12pt;}input {font-family:verdana,arial;font-size:12pt;}table {font-family:verdana,arial;font-size:12pt;}tr    {font-family:verdana,arial;font-size:12pt;}td    {font-family:verdana,arial;font-size:12pt;background-color:#eef;}th    {font-family:verdana,arial;font-size:12pt;background-color:#ddf;font-weight:bolder;}a     {color:#039;}h1    {font-family:verdana,arial;font-size:24pt;color:#039;background-color:#ffc;border:6px solid #ccb;margin:0px 30px 0px 30px;padding:6px 6px 8px 6px;font-weight:bolder;}</style></head><body><h1 align="center">generate a PDF, based on values from a html form.</h1><br><form action="gen16.php" method="post"><table border="1" cellpadding="6" cellspacing="0" align="center"><tr><th colspan=2>give the numerical values, separated by commas</th></tr><tr><td align="right"> ===></td><td><input type="text" name="data"></td></tr><tr><th colspan=2><input name="submit" type="submit" value="generate a pie chart in PDF with PHP"></th></tr></table></form></body></html>
- Save this code as a new text file, let's name it form16.html.

- Upload it to your web site.

- Browse to your form16.html file on your web site.

- Check the results.

 

 

Part Two: The PHP script.

Here is the complete code:

<?php$mypdf = PDF_new();PDF_open_file($mypdf, "");PDF_set_info($mypdf, "Creator", "gen07.php");PDF_set_info($mypdf, "Author", "John");PDF_set_info($mypdf, "Title", "Prototype (PHP)");PDF_set_info($mypdf, "Subject", "Sample");PDF_set_info($mypdf, "Keywords", "PHP PDF PDFlib");PDF_begin_page($mypdf, 595, 842);$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);PDF_setfont($mypdf, $myfont, 10);PDF_show_xy($mypdf, "Pie chart:", 450, 765);$mydata = $_POST['data'];$myslices = explode(",", $mydata);PDF_show_xy($mypdf, $mydata, 50, 765);$mysum = 0;$mydegrees = Array();$mydiameter = 250;$myradius = $mydiameter / 2;$mycolours = array(array(0.7, 0.7, 0.7),array(  0,   0,   1),array(  0,   1,   0),array(  1,   0,   0),array(  1,   1,   0),array(  0,   1,   1),array(  1,   0,   1),array(  0,   0, 0.5),array(  0, 0.5,   0),array(0.5,   0,   0),array(  0, 0.5, 0.5),array(0.5, 0.5,   0),array(0.5,   0, 0.5),);$mysum = array_sum($myslices);for ($y=0; $y<=sizeof($myslices); $y++) { $mydegrees[$y] = $myslices[$y] / $mysum * 360; PDF_setfont($mypdf, $myfont, 6); PDF_show_xy($mypdf, $mydegrees[$y], 50, 300 - (10 * $y));}PDF_setcolor($mypdf, "stroke", "rgb", 0.50, 0.50, 0.50);PDF_moveto($mypdf, 250, 600);PDF_lineto($mypdf, 350, 600);PDF_stroke($mypdf);$mylastangle = 0;for ($z=0; $z<sizeof($myslices); $z++){ PDF_setcolor($mypdf, "fill", "rgb", $mycolours[$z] [0], $mycolours[$z] [1], $mycolours[$z] [2]); $myendx = round(250 + ($myradius * cos($mylastangle * pi() / 180) ) ); $myendy = round(600 + ($myradius * sin($mylastangle * pi() / 180) ) ); PDF_moveto($mypdf, 250, 600); PDF_lineto($mypdf, $myendx, $myendy); PDF_arc($mypdf, 250, 600, $myradius, $mylastangle, ($mylastangle + $mydegrees[$z]) ); $mylastangle = $mylastangle + $mydegrees[$z]; PDF_fill_stroke($mypdf);}PDF_setcolor($mypdf, "stroke", "rgb", 0.00, 0.00, 0.00);PDF_circle($mypdf, 250, 600, $myradius);PDF_stroke($mypdf);PDF_end_page($mypdf);PDF_close($mypdf);$mybuf = PDF_get_buffer($mypdf);$mylen = strlen($mybuf);header("Content-type: application/pdf");header("Content-Length: $mylen");header("Content-Disposition: inline; filename=gen07.pdf");print $mybuf;PDF_delete($mypdf);?>
- Save this code as a new text file, let's name it gen16.php.

- Upload it to your web site.

- Browse to your form16.html file on your web site.

- Enter some numbers, separated by commas.

- Check the results.

- Show off to your relatives.

- Get the credits for it.

- Give John a Reputation point !

Edited by vujsa (see edit history)

Share this post


Link to post
Share on other sites

Unfortunately,PDFlib is quite expensive for commerce use (free for private and educational use).But there is free library - pdfClass (http://forums.xisto.com/no_longer_exists/). :-)It is very simple in use(but quite powerfull) and don't require installationon server (just copy files to your directory)-Kacper Chrapa

Share this post


Link to post
Share on other sites

Question actually.I have problems at GEN02.Php: The function PDF_open_image_file (I also tried with PDF_load_image)Always return 0.I tried a lot of things (SearchPath, different type of pictures, different pictures...) but still return 0.Please help!Tks,Ferasse.-Mr. Tea

Share this post


Link to post
Share on other sites

i borrowed a book from a friend which had how to generate a pdf with php. it wasnt exactly as big as yours but anyway. i never tried it because im not good enough at php to do hardly anything.joomla also generates a pdf of the articles and content. i spose u could also go through the code of the pages and work out how to do it from that.

Share this post


Link to post
Share on other sites

How to ADD Verdana Font?

How To: Create PDF With Php

 

How to ADD Verdana Font ?

I have created required files with tools provided on fpdf.Org.

Can anyone help me ?

 

-vinayak

Share this post


Link to post
Share on other sites

how to create pdf files in apache2triad

How To: Create PDF With Php

 

Please help, I'm new to php and I'm using apache2triad 2.2 with php 5.1.2, I want to create certificates using data taken from a database, I want to generate the certificate in pdf format and I'm total lost I don't know where to start, I also want to put signatureson the bottom of the certificates

 

-thando

Share this post


Link to post
Share on other sites

Question actually.I have problems at GEN02.Php:
The function PDF_open_image_file (I also tried with PDF_load_image)
Always return 0.

I tried a lot of things (SearchPath, different type of pictures, different pictures...) but still return 0.

Please help!

Tks,

Ferasse.

-Mr. Tea


*******************************************************************
Hello
here is the solution
<?php
$mypdf = PDF_new();
PDF_open_file($mypdf, "");
PDF_begin_page($mypdf, 595, 842);
$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);
PDF_setfont($mypdf, $myfont, 10);
//PDF_show_xy($mypdf, "Sample PDF, constructed by PHP in real-time.", 50, 750);
//PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);
//PDF_show_xy($mypdf, "A JPEG image, on 60 % of its original size.", 50, 710);
//$myimage = PDF_open_image_file($mypdf, "jpeg", "trump.jpg");
$myimage = PDF_load_image($mypdf, "jpeg", "D:\wamp\www\srms_test\pdf\a.jpg", "");
//PDF_place_image($mypdf, $myimage, 50, 650, 0.6);
PDF_fit_image($mypdf, $myimage, 0, 700, "boxsize {200 100} fitmethod meet");
PDF_end_page($mypdf);
PDF_close($mypdf);
$mybuf = PDF_get_buffer($mypdf);
$mylen = strlen($mybuf);
header("Content-type: application/pdf");
header("Content-Length: $mylen");
header("Content-Disposition: inline; filename=gen02.pdf");
print $mybuf;
PDF_delete($mypdf);
?>

Share this post


Link to post
Share on other sites

Font weight and style

How To: Create PDF With Php

 

How do make text bold or italic, and such?

Thanks a lot for the tutorial, it's very helpful!

 

Philippe Alves

Share this post


Link to post
Share on other sites

I needed to create a printable version of a html application form with PHP, due to printing the html from a browser was quite lame, so I thought to use PDF generation and really found a great class which doesn't require any extra libs like in the tutorial and it works with PHP4 and PHP5, there are two different versions for support for both of them..

the class is called TCPDF, you'll find it through google, as I know a lot of PHP apps are using it to generate PDF files and it's really powerful, there are ~22 examples in the package how would you need to create them, at first it was quite hard for me, but in a couple of hours I got myself a PHP script to generate PDF files I wanted.. before that I tried fpdf, I think tcpdf is written the way fpdf is, but tcpdf has much more features and the main thing it has UTF-8 support, fpdf doesn't support UTF-8, thats why I had problems with some Lithuanian letters and found TCPDF, really a great class! :)

FPDF: http://www.fpdf.org/
TCPDF: http://forums.xisto.com/no_longer_exists/

Share this post


Link to post
Share on other sites

This is a very good tutorial nonetheless lengthy. I personally do not see much use for PDF documents unless you are scanning things in and converting those to PDF files.This method can be useful if you are trying to convert Word documents into PDF and you do not have access to certain PDF software or OpenOffice.org.I may decide to try this sometime when I need it.

Share this post


Link to post
Share on other sites

Euro Symbol with pdflib

How To: Create PDF With Php

 

How Can I print the euro symbol in a pdf document generated with pdflib???

I try print it by different ways but I can't do it.

 

Anyone can help me???

 

Thank in advance.

 

-reply by Jorge Roman

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.