Creating PDF in CodeIgniter using the R&OS pdf class
- Transfer
There are quite a lot of PHP libraries for creating PDF files, for example, such as FPDF , Panda and dompdf , but the best, in my opinion, is the R&OS pdf class . I first found out about it from the PHP Anthology book . I tried other libraries for creating PDFs, 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.
Before we begin, download the archive that I 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, 2 files are required , 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 so that everything works correctly as part of CodeIgniter. Renamed class.ezpdf.php to cezpdf.php and replace line 3 with
I also created the tutorial.php controller and the pdf_helper helper . php
Using the code above, we created this PDF file.
First we hooked up the R&OS library. Then, using ezText (), we set the title of our document. This function takes the header text as the first argument, then the font size and the last optional argument passed as an array are additional configuration options. For example, alignment (in this case, centered)
After the header, we will add some empty space using ezSetDy () . Then we place the text contained in the variable $ content. To do this, again use ezText () . And finish creating our document using ezStream (). This function forms a document and sends it to the browser. Now the user can view or download our PDF.
When you work with reports, or any data that needs a table view, the task of displaying a table in PDF is not trivial. But in the R&OS library, it is as simple as creating an array.
That's what we got.
In the above code, I created a $ db_data data array , then created an $ col_names array with the keys corresponding to the column names of the future table. To create a table, use the ezTable () function . The first argument contains an array with table data, then an array with column names, the third argument is the table name, and the last, optional argument is an array with configuration data. In this case, the width of the table is indicated.
A fairly large number of reports contain standard headers / footers. They can contain the date of creation, the name of the author of the document, the number of pages, etc.
The process of adding headers / footers is a bit more complicated. I brought this process to the helper
An example of using this helper can be found in the headers () function of the tutorial.php controller . Final PDF.
The above code is a function of prep_pdf () helper pdf_helper.php . This feature does all the work of creating a footer for a PDF file. All I have to do is load this helper and call prep_pdf () in the controller.
I did not go into details in this example so that it is not voluminous. All I wanted to show was how easy it is to create PDF documents using CodeIgniter and the R&OS class.
There are many ways to create PDF files, some of which are described in the wiki framework CodeIgniter .
Let's start
Before we begin, download the archive that I 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, 2 files are required , 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 so that everything works correctly as part of CodeIgniter. Renamed class.ezpdf.php to cezpdf.php and replace line 3 with
include_once (APPPATH. 'libraries / class.pdf.php' );
I also created the tutorial.php controller and the 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 hooked up the R&OS library. Then, using ezText (), we set the title of our document. This function takes the header text as the first argument, then the font size and the last optional argument passed as an array are additional configuration options. For example, alignment (in this case, centered)
After the header, we will add some empty space using ezSetDy () . Then we place the text contained in the variable $ content. To do this, again use ezText () . And finish creating our document using ezStream (). This function forms a document and sends it to the browser. Now the user can view or download our PDF.
Working with tabular data
When you work with reports, or any data that needs a table view, the task of displaying a table in PDF is not trivial. But in the R&OS library, it is as simple 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' =>, 'email' => 'jsmith@someplacepsecial.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 got.
In the above code, I created a $ db_data data array , then created an $ col_names array with the keys corresponding to the column names of the future table. To create a table, use the ezTable () function . The first argument contains an array with table data, then an array with column names, the third argument is the table name, and the last, optional argument is an array with configuration data. In this case, the width of the table is indicated.
Header and Basement
A fairly large number of reports contain standard headers / footers. They can contain the date of creation, the name of the author of the document, the number of pages, etc.
The process of adding headers / footers is a bit more complicated. I brought this process to the 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 / Y h: i: sa' ));
$ 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 a function of prep_pdf () helper pdf_helper.php . This feature does all the work of creating a footer for a PDF file. All I have to do is load this helper and call prep_pdf () in the controller.
Conclusion
I did not go into details in this example so that it is not voluminous. All I wanted to show was how easy it is to create PDF documents using CodeIgniter and the R&OS class.
There are many ways to create PDF files, some of which are described in the wiki framework CodeIgniter .