Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Use PHP to Create Dynamic pdf Files (http://www.go4expert.com/articles/php-create-dynamic-pdf-files-t3156/)

Sanskruti 26Feb2007 18:17

Use PHP to Create Dynamic pdf Files
The PDF (or portable document format) file is an amazingly versatile format for transferring what usually starts as a hard copy version of a document over the Internet. Many companies use the PDF format to transfer contracts, manuals, and other documents over the Internet without trying to convert them to HTML or changing the format of the document in any way.

This is particularly useful for organizations that already have established means for processing documents through the conventional paper format, but want to also take advantage of the Internet. With the help of PHP, an organization can take information online from the user without requiring any changes in how the forms are processed.

PHP creates PDF files through the help of Thomas Merz's PDFlib. There are over 100 different PDF functions available to you that allow you to control nearly every aspect of the PDF file you are working with. Because of the wealth of functions PHP supports when dealing with PDF files.

Creating/Opening the PDF file

PDF files can either be created or opened from within PHP. We'll start by creating a new PDF object using the PDF_new() function and then instruct PHP to create a new PDF document by using the PDF_open_file() function.

The syntax for these functions is:

Code: PHP

$var = PDF_new();
int PDF_open_file(<pdf object>, [filename]);

Where $var represents the variable to store the PDF object reference and represents an optional parameter specifying a already existing PDF file to open. If no filename is specified, then a new PDF document is created.

Code: PHP

$pdf = PDF_new();

Entering Document information

We have started with our PDF file, the next step is to fill in the PDF document information . This is done through the PDF_set_info() function.

The syntax for PDF_set_info() is:

Code: PHP

PDF_set_info(<pdf_object>, <property string>, <value>);

Where <property string> represents the property title (such as "author") and <value> represents the value to set the particular property.

Code: PHP

PDF_set_info($pdf, "author", "shabbir")
PDF_set_info($pdf, "title", "go4expert.com Example")
PDF_set_info($pdf, "creator", "go4expert.com")
PDF_set_info($pdf, "subject", "Invoke and begin invoke of control and delegates");

Creating a page in the PDF document

Now that we have opened the PDF file and set the document information, the next step is to create a page in the document. This is done through the use of the PDF_begin_page() function.

The syntax for PDF_begin_page() is:

Code: PHP

PDF_begin_page(<pdf object>, <page width>, <page height>);

Where <page width> and <page height> are the page width and height. In our example, we'll create a square page with a width and height of 500 points

Code: PHP

PDF_begin_page($pdf, 500, 500);

Postscript point

Postscript points represent 1/72 of a printed inch. The reason the PDF format uses these so called "postscript points" rather than pixels is because a pixel cannot be cut in half, while postscript points can be cut in half (1/144th of an inch). Although some PDF rendering devices can distort this measurement by assuming 1 point equals 1 pixel, the 72 points per inch conversion holds true for all hard copy.

Output the page

Now, we are finally ready to output the content to our PDF document. This process can range from extremely simple to extremely complex depending on the nature of the document.

For our example, we'll be simply setting our font and outputting a single line of text mid-page.

In order to accomplish this, we need to first find an acceptable font (using PDF_findfont() and PDF_setfont()) and then output the string to the PDF document using PDF_show_xy().

Using a font

In order for a font to be used from within a PDF document, it first must be initialized through the use of the PDF_findfont() function.

The syntax for PDF_findfont() is a follows:

Code: PHP

int PDF_findfont(<pdf object>, <fontname>, <encoding>,<embed>)

Where <fontname> is the string name of the font, <encoding> represents the encoding type to be used ("builtin", "macroman", "winansi", or "host") and <embed> represents a Boolean determining if the specified font should be embedded within the PDF document. Embedding of the font within the document is only necessary for the 14 fonts that do not exist in the PDF definition.

The 14 fonts that can be safely used without being embedded are:
Courier, Courier-Bold, Courier-Oblique, Courier-BoldOblique
Helvetica, Helvetica-Bold, Helvetica-Oblique, Helvetica-BoldOblique
Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic
Symbol, ZapfDingbats

If the desired typeface does not exist in the above list, it must be embedded into the document to be rendered properly.

Code: PHP

$font = PDF_findfont($pdf, "Helvetica-Bold""winansi",0);

Once a font has been initialized, the font can be set as the current "brush".

Setting the current font to be used in the PDF document can be done through the use of the PDF_setfont() function.

The syntax for PDF_setfont() is...

Code: PHP

PDF_setfont(<pdf object>, <font id>, <size>);

where <font id> represents the font handle returned from PDF_findfont() and <size> is the size to render the font at.

Code: PHP

PDF_setfont($pdf, $font, 12);

Outputting Text

Now that you have initialized and selected a font, the next step is to use the font in the PDF document. We will be using the PDF_show_xy(). As the name implies, PDF_show_xy() allows you to output text starting at a specific x-y coordinate in the document. The syntax for PDF_show_xy() is...

Code: PHP

PDF_show_xy(<pdf object>, <string>, <x value>, <y value>);

...where the <string> parameter represents the text to output at the coordinate represented by <x value> and <y value>.
Code: PHP

PDF_show_xy($pdf, "Hello, This is my pdf page ", 5, 225);

End the Page

Once you have completed a page, the next step is to signify that we have completed with the current page by calling PDF_end_page() in the following way

Code: PHP

PDF_end_page(<pdf object>);
...where the <pdf object> represents the handle to the PDF object .

Close the PDF file

After you have completed your PDF creation process the next step is to close the PDF file.

Closing the PDF file does not mean that the file will be outputted (or saved) but rather simply means you are finished compiling your PDF document. At this point, PHP will finalize the document, release any resources (such as fonts initialized), and ready it to be outputted.

This task is accomplished by calling the PDF_close() function

Code: PHP

PDF_close(<pdf object>)

In such way one can create dynamic pdf files in php

biniseifu 27Apr2007 13:30

Re: Use PHP to Create Dynamic pdf Files
pls upload the files

dean8710 4Aug2011 07:01

Re: Use PHP to Create Dynamic pdf Files
yeah, example of full application

All times are GMT +5.5. The time now is 08:06.