Client Login
Email Id
Password
Save Log In
Sign up
Forgot Password

Solution Blog(We are fullfill your needs)


We are verified by

Building Trust in Transactions (tm)
 Pay me securely with any major credit card through PayPal!
Solution Blog How it Works Get Quote
 Articles :: How To Sections :: How to create PDF documents dynamically using PHP
How to create PDF documents dynamically using PHP
Introduction

The PDF (Portable Document Format) file is a widely used file format originally developed by Adobe. This format is rapidly becoming the defacto standard for publishing on the web. Many companies uses the PDF format to overcome their Reports, Contracts, Manuals, Billings and other documents over the Intranet. The main reason for this is the file look the same on the screen and in the printer and it is highly compressed, allows complex information to be downloaded efficiently.

PDF Files not only produce high quality screen viewing, conveniently and quickly wide information including text, tables, links and graphics and allows the reliable reproduction of published material on many different platforms.

Advantages of Creating PDF dynamically

Creating PDF dynamically provides many features because of its reliability and ease of usability. Most of the web developers creates dynamic PDF in purpose of reusability, ease of maintenance and most importantly to get live data whenever the web users needs it. Server side generation is typically the result of a Client side request from a web page or any Client application, to get their needed document on fly. Meanwhile good coding for generating PDF also provides high security control of the file and also getting the PDF downloadable without storing in the server. The documents can be easily secured with password protection and 128-bit encryption. Anyone from anywhere can open the PDF file at anytime to get their on time information, only thing is that they need Acrobat Reader Software.

More organizations uses Script for getting any processing PDF form documents from the Sever and filling the information in the Form perfectly and provide easy way of print out or as PDF File to the user. An organization can take online information as a report from the user from their forms.

"What has to come more than these high features... from creating a dynamic PDF document on fly."

Components, Tools or Add-Ins for document Creation

To generate PDF documents on the fly, PDFLib is one of the best component that allows the user to create PDF file within the server or on the Client-side software. PDFLib is available for all major operating environments and development environments. Easy functions and methods from PDFLib is very useful for the developers to code the Script in easy and quick way.

There also COM Components like DynamicPDF generator, DynamicPDF ReportWriter and few add-ins like ActivePDF toolkit to create a PDF documents on fly. ActivePDF has more advance feature like converting from Word Document to PDF.

Creating PDF Files Using PDFLIB
Thomas Merz's PDFLib is used to Create PDF Files in PHP. There are about 100 different wealthy and easy functions that allows to control(Create/Modify) PDF files. Here in this article we can see about basic fundamentals of creating PDF files dynamically using PDFLib.

Various functions in the PDFLib are:

  • General Functions.
  • Font Functions.
  • Text Output Functions.
  • Graphics Functions.
  • Color Functions.
  • Image Functions.
  • PDF Import(PDI) Functions.
  • Block Filling Functions.
  • Hypertext Functions.

These are very various Functions that has been used to create a PDF File.

The 8 steps to create a basic PDF file using PDFLib Functions are:

Creating/Opening the PDF File

Using PDFLib functions, the PDF files can be either created or opened within PHP. The function used for creating new PDF file is PDF_new(), and to create new PDF Document or modify PDF File PDF_open_file() has been used.

The basic syntax for these functions are:

$objPDF = PDF_new();

int PDF_open_file($objPDF, $fileName);

$objPDF represents the variable to store the PDF object reference. $fileName represents the file name of the PDF that has to be open.

Let us see with an example of creating new PDF file without giving the file name.


$objPdf = PDF_new();
PDF_open_file($bjPDF);

?>
Setting Document information for the PDF File.

The next step is to Fill the document information for the PDF File like author, title, creator, subject. PDF_set_info() function is used to fill the documentation information.

The basic syntax for this function is

PDF_set_info($objPDF,$property,$value);

Here $property represents the property like title,author,subject etc., and $value represents the corresponding value for the property.

Eg:-

PDF_set_info($objPDF, "author", "AllquickFix");
PDF_set_info($objPDF, "title", "AllquickFix Articles Example");
PDF_set_info($objPDF, "creator", "www.allquickfix.com");
PDF_set_info($objPDF, "subject", "How to create PDF Documents dynamically");
Setting various common values for PDF File.

Now the next important step for a good programming is to set the various common values for the PDF File. The common variables are like Font Name, Font Size, Page Height, Page Width. This is good to declare them before going for the page creation. If needed the line indent values can also be set because the creation of PDF is graphical creating for which the each line has to be moved by changing the Y values and each letter in a sentence moved by changing X values. So its good to keep a constant value for line indent to get a good result of PDF.

Eg:-

$width = 595;
$height = 842;
$lMargin = 10;
$rMargin = 10;
$tMargin = 5;
$bMargin = 5;
$lIdent = 4;
$font = PDF_findfont($objPDF, "Helvetica-Bold", "winansi",0);


Creating a Page for the PDF document.

The next step is to create a new page in the document. The function used for it is PDF_begin_page();

The basic syntax is given below is

PDF_begin_page($objPDF, $width, $height);

Where $width and $height are width and height of the page. The table below lists common standard page sizes.

format width height format width height format width height
A0 2380 3368 A4 595 842 letter 612 792
A1 1684 2380 A5 421 595 legal 612 1008
A2 1190 1684 A6 297 421 ledger 1224 792
A3 842 1190 B5 501 709 11x17 792 1224
Creating the Page content for the PDF Document.

The next step in the PDF creating is to doing various functionalities to bring the contents to the page. Like to write any text from a point x,y and more. The function used for it is PDF_show_xy(). The basic syntax for this is,

PDF_show_xy($objPDF,$string,$x,$y);

where $x & $y are x and y positions respectively, $string is your string to write.

Eg:-

PDF_show_xy($objPDF,100,200,"Hello World! From AllquickFix");

Ending the Page.

After finishin of creating the page content the final step in page creation is to properly close the page. The function used for ending the page is PDF_end_page().

In order to create a new page in the PDF document the previous page has to be closed. So that will able to create the new fresh page for the further contents.

Eg:-

PDF_end_page($objPDF);

Closing the PDF File.

The final step is to close the PDF document that has been created. This task is done by calling the function PDF_close(); The basic syntax for closing the PDF document is as follows.

PDF_close($objPDF);

Output the Buffer and save the File.

Finally we need to bring this PDF content for viewing or to save it. We can use the PDF buffer itself to bring the content for viewing. The function used for buffering the content is PDF_get_buffer() which will retrieve the content that have been created for the PDF document and this can be either written to the file system using the PHP standard calls or can be directly outputted to the browser for viewing, through the use of appropriate headers and print statement.

To output the buffer we need to use the headers in the following format..

Content-type : application/PDF

Content-length : $length

Content-disposition:inline;filename:$filename

Eg:-

$buffer = PDF_get_buffer($objPdf);
header("Content-type: application/pdf");
header("Content-Length: ".strlen($objPdf));
header("Content-Disposition: inline; filename=yourfile.pdf");
echo $buffer;
The final step is to delete the content from the memory that is to release or clean up memory. The function that to be used for it is PDF_delete().
Conclusion
So far it is very clear of how to create the PDF document dynamically using PDFlib with PHP. The various functions and steps shown here will help more to program a good procedures to create dynamic PDF. And if you are interested with how PHP work with PDF files and to learn more functions and advantages for PDF document creations, we have a good consultants for dynamically creating PDF documents and you can contact us.


Indian Search Engine| Indian Business Directory | Free Blogs - Free Subdomains | India Classifieds | People | Video

All other products mentioned are registered trademarks or trademarks of their respective companies.
Copyright 2003-2004 allquickfix.com. All rights reserved. Terms of Use | Privacy Policy

Questions or problems regarding this web site should be directed to [support@allquickfix.com].