There are quite a number of PHP libraries for creating PDF files, such as
FPDF ,
Panda and
dompdf , for
example , but in my opinion, the best is the
R & OS pdf class . I first learned about it from the book
PHP Anthology . I tried other libraries to create PDF, some only work on PHP5, some on earlier versions, but none of them provided me with the same control and ease of use as R & OS. And so I use this class in my example.
Let's start
Before we begin, download the
archive that I have prepared for you. It contains CodeIgniter 1.6.3, as well as all the code and libraries that are described in this example.
To work with the R & OS library you need 2 files, these are
class.ezpdf.php and
class.pdf.php . They are located in the
application / libraries directory. For R & OS to work properly, some font files are also needed, which are located in the root of the archive in the
fonts directory.
I made some changes to make everything work correctly in CodeIgniter. Renamed
class.ezpdf.php to
cezpdf.php and replaced line 3 with
include_once (APPPATH. 'libraries / class.pdf.php' );
I also created a
tutorial.php controller and a
pdf_helper helper.
php')
Hello world
function hello_world ()
{
$ this -> load-> library ( 'cezpdf' );
$ this -> cezpdf-> ezText ( 'Hello World' , 12, array ( 'justification' => 'center' ));
$ this -> cezpdf-> ezSetDy (-10);
$ content = 'The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.
Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs. ' ;
$ this -> cezpdf-> ezText ($ content, 10);
$ this -> cezpdf-> ezStream ();
}
Using the code above, we created
this PDF file.
First we connected the R & OS library. Then, using
ezText (), we set the title of our document. This function accepts the first text of the header text, then the font size and the last optional argument passed in the form of an array are additional configuration options. For example, alignment (in this case, center)
After the header, we will add some empty space using
ezSetDy () . Then we place the text contained in the $ content variable. To do this, again use
ezText () . And complete the creation of our document using
ezStream () . This function forms the document and sends it to the browser. Now the user can view or download our PDF.
We work with tabular data
When you work with reports, or any data that needs a table view, the task of displaying a table in PDF becomes non-trivial. But in an R & OS library, this is as easy as creating an array.
function tables ()
{
$ this -> load-> library ( 'cezpdf' );
$ db_data [] = array ( 'name' => 'Jon Doe' , 'phone' => '111-222-3333' , 'email' => 'jdoe@someplace.com' );
$ db_data [] = array ( 'name' => 'Jane Doe' , 'phone' => '222-333-4444' , 'email' => 'jane.doe@something.com' );
$ db_data [] = array ( 'name' => 'Jon Smith' , 'phone' => '333-444-5555' , 'email' => 'jsmith@someplacepsepsecial.com' );
$ col_names = array (
'name' => 'Name' ,
'phone' => 'Phone Number' ,
'email' => 'E-mail Address'
);
$ this -> cezpdf-> ezTable ($ db_data, $ col_names, 'Contact List' , array ( 'width' => 550));
$ this -> cezpdf-> ezStream ();
}
That's
what we did.
In the above code, I created the
$ db_data dataset , then created the
$ col_names array with the keys corresponding to the column names of the future table. To create a table, use the
ezTable () function. In the first argument, the array with the data of the table is passed, then the array with the names of the columns, the third argument is the name of the table, and the last, optional argument is the array with the configuration data. In this case, the width of the table.
Heading and Basement
Quite a large number of reports contain standard headers / footers. They may contain the date of creation, the name of the author of the document, the number of pages and so on.
The process of adding headers / footers is a bit more complicated. I brought this process to helper
function prep_pdf ($ orientation = 'portrait' )
{
$ CI = & get_instance ();
$ CI-> cezpdf-> selectFont (base_url (). '/ Fonts' );
$ all = $ CI-> cezpdf-> openObject ();
$ CI-> cezpdf-> saveState ();
$ CI-> cezpdf-> setStrokeColor (0,0,0,1);
if ($ orientation == 'portrait' ) {
$ CI-> cezpdf-> ezSetMargins (50,70,50,50);
$ CI-> cezpdf-> ezStartPageNumbers (500,28,8, '' , '{PAGENUM}' , 1);
$ CI-> cezpdf-> line (20,40,578,40);
$ CI-> cezpdf-> addText (50,32,8, 'Printed on' . Date ( 'm / d / Yh: i: s a' ));
$ CI-> cezpdf-> addText (50,22,8, 'CI PDF Tutorial - www.christophermonnat.com ' );
}
else {
$ CI-> cezpdf-> ezStartPageNumbers (750,28,8, '' , '{PAGENUM}' , 1);
$ CI-> cezpdf-> line (20,40,800,40);
$ CI-> cezpdf-> addText (50,32,8, 'Printed on'. Date ( 'm / d / Y h: i: s a' ));
$ CI-> cezpdf-> addText (50,22,8, 'CI PDF Tutorial - www.christophermonnat.com ' );
}
$ CI-> cezpdf-> restoreState ();
$ CI-> cezpdf-> closeObject ();
$ CI-> cezpdf-> addObject ($ all, 'all' );
}
An example of using this helper can be found in the
headers () function of the
tutorial.php controller.
Final PDF.
The above code is in the
prep_pdf () function of the
pdf_helper.php helper. This feature does all the work of creating a footer for a PDF file. All I need to do is load this helper and call the
prep_pdf () function in the controller.
Conclusion
I did not go into details in this example so that it was not voluminous. All I wanted to show is how easy it is to create PDF documents using CodeIgniter and the R & OS class.
There are many more ways to create PDF files,
some of
which are described in the
CodeIgniter framework
wiki .