CodeIgniter 操作PDF ( Generating PDF files using CodeIgniter )

PDF files rock! Some of the programs used to view them could use some work, but the file format itself is real handy. As a programmer I have found PDF’s to be most helpful when generating reports that need to be printable. I know we are all supposed to be doing our part to make our offices “greener” and use less resources like paper. But some things just need to be printed (especially when your talking about the financial and legal industries).

When generating reports in PDF format you suddenly have a lot more control over layout and design than  you do with plain old HTML and CSS (although much progress is being made with print style sheets). You can create some really nice reports on the fly that your users can view, save for later or e-mail to their co-workers for review. In this post I will show you how I generate PDF reports using CodeIgniter .

 

Quick Note

There are a number of PHP libraries out there for generating PDF files (like FPDF , Panda  and dompdf ) but the best one I have come across is the R&OS pdf class . I was first introduced to it from the PHP Anthology  first edition by Harry Fuecks. I have tried other PDF libraries (some PHP 5 specific and some not) and none of them have been able to provide me with the same control or ease of use that the R&OS class has which is why I’m using it for this tutorial.

Getting Started

Before we start, lets get everyone to the same place so you can follow along as we go. I’ve prepared a .zip file containing everything you’ll need to follow along. The archive includes CI 1.6.3 along with all the code and libraries we will discuss in this tutorial. Simply download the archive , unzip it on your web server and follow along.

I have done all the work of downloading the code library and putting the files in their right place for you, but I wanted to mention where things were for the detail oriented among you. There are 2 files that are necessary in order to use the R&OS library: class.ezpdf.php  and class.pdf.php . Those two files have been placed in the application/library folder. R&OS also requires some font files in order to function and they have been placed at the root of the .zip file in a folder called fonts .

You do have to make a minor modification to the class.ezpdf.php  file so that it will work properly within CI. First I renamed the file to cezpdf.php , which makes it easier to load using the CI loader. Then you have to modify the include statement on line 3 to:

Php代码 
  1. include_once(APPPATH . 'libraries/class.pdf.php');  
 

This will keep PHP from saying that it can’t find included file. Once those modifications are made the R&OS class is ready to use with CI.

In the archive, I have also made a controller called tutorial.php  and a helper called pdf_helper .php  both in their respective directories. With the background info. out of the way, lets get our hands dirty with a real simple example.

Hello World

Php代码 
  1. function hello_world()  
  2. {  
  3. $this->load->library('cezpdf');  
  4.   
  5. $this->cezpdf->ezText('Hello World', 12, array('justification' => 'center'));  
  6. $this->cezpdf->ezSetDy(-10);  
  7.   
  8. $content = 'The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.  
  9. Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.';  
  10.   
  11. $this->cezpdf->ezText($content, 10);  
  12.   
  13. $this->cezpdf->ezStream();  
  14. }  
 

The above code produces a PDF file like this .

In the above, first thing we do is load the R&OS library for use. Next we use the ezText()  function to create a title for our document. This function takes the text it will display as the first argument, the size of that text and an optional array of additional configuration options. In this instance we pass along a justification option of center. That will center our title at the top of our document.

After the title we insert some extra white space using the ezSetDy()  function. After the whites pace we put the rest of the content for the document in a variable called $content and add it to our document using the ezText() function again. Finally, we create our document using the ezStream()  function which actually creates the document and sends it to the users which prompts them to view/download the generated PDF document.

Handling Tabular Data

When your dealing with business reports the odds are good that you will need to generate reports with tables to display tabular data. From my experience with other PDF libraries, this is not an easy task. But with the R&OS library it’s about as hard as creating an array.

Php代码 
  1. function tables()  
  2. {  
  3. $this->load->library('cezpdf');  
  4.   
  5. $db_data[] = array('name' => 'Jon Doe''phone' => '111-222-3333''email' => 'jdoe@someplace.com');  
  6. $db_data[] = array('name' => 'Jane Doe''phone' => '222-333-4444''email' => 'jane.doe@something.com');  
  7. $db_data[] = array('name' => 'Jon Smith''phone' => '333-444-5555''email' => 'jsmith@someplacepsecial.com');  
  8.   
  9. $col_names = array(  
  10. 'name' => 'Name',  
  11. 'phone' => 'Phone Number',  
  12. 'email' => 'E-mail Address'  
  13. );  
  14.   
  15. $this->cezpdf->ezTable($table_data$col_names'Contact List'array('width'=>550));  
  16. $this->cezpdf->ezStream();  
  17. }  
 

The above code should produce a PDF file like this .

In the above code I create an array of data called $db_data. I put this together so that it imitates a typical database result set because that’s usually where you will be getting your data from. Below my data array I have created a $col_names array that associates the data elements in the $db_data array with a column title for the table. This is where the R&OS gets the title to display at the top of each table column. Once I have the data and column titles I create the table by calling the ezTable()  function. This function takes the data array, an associative array for column names, the title for the table and an optional array of configuration options. There are a number of options that can be configured through that last optional array, but I’m not going to go into them in this tutorial.

Headers and Footers

Most reports in a corporate setting come with some kind of standard header and/or footer. They can include anything from the date/time generated, the user who generated them, to page numbers and the like. Headers and footers are handy to have, but unfortunately there isn’t a real good way to add them to printable reports generated in HTML and CSS. There’s one more reason to use the portable document format when creating printable reports.

The process of adding headers and footers to a PDF using the R&OS library is the slightest bit complicated. There are a lot of lines of code just to accomplish it, that’s why I have created a helper file. Since the code is in a helper function, you just need load the helper and call the function whenever you need to add headers/footers to a PDF.

Php代码 
  1. function prep_pdf($orientation = 'portrait')  
  2. {  
  3. $CI = & get_instance();  
  4.   
  5. $CI->cezpdf->selectFont(base_url() . '/fonts');  
  6.   
  7. $all = $CI->cezpdf->openObject();  
  8. $CI->cezpdf->saveState();  
  9. $CI->cezpdf->setStrokeColor(0,0,0,1);  
  10. if($orientation == 'portrait') {  
  11. $CI->cezpdf->ezSetMargins(50,70,50,50);  
  12. $CI->cezpdf->ezStartPageNumbers(500,28,8,'','{PAGENUM}',1);  
  13. $CI->cezpdf->line(20,40,578,40);  
  14. $CI->cezpdf->addText(50,32,8,'Printed on ' . date('m/d/Y h:i:s a'));  
  15. $CI->cezpdf->addText(50,22,8,'CI PDF Tutorial - http://www.christophermonnat.com');  
  16. }  
  17. else {  
  18. $CI->cezpdf->ezStartPageNumbers(750,28,8,'','{PAGENUM}',1);  
  19. $CI->cezpdf->line(20,40,800,40);  
  20. $CI->cezpdf->addText(50,32,8,'Printed on '.date('m/d/Y h:i:s a'));  
  21. $CI->cezpdf->addText(50,22,8,'CI PDF Tutorial - http://www.christophermonnat.com');  
  22. }  
  23. $CI->cezpdf->restoreState();  
  24. $CI->cezpdf->closeObject();  
  25. $CI->cezpdf->addObject($all,'all');  
  26. }  
 

An example of how I use this helper is provided in the headers()  function of the tutorial.php  controller. That code produces a PDF file like this .

The above code is the prep_pdf()  function located in the pdf_helper.php  file. This function does all the hard work of creating a footer for my PDF reports for me. All I have to do is load the helper in my controller and call the function. Since the reports could be portrait or landscape I have also included the ability to pass the orientation to the function and the code will modify the document margins accordingly.

I’m not going to go into a lot of detail about the above code because this tutorial could become very long. But the general idea is that I’m using the R&OS library to modify the margins of the document I’m creating, add page numbers and text to the very bottom of the document. R&OS has some functions that makes this easy likeezStartPageNumbers()  and line() . Once all that is done I can add any kind of content to the document back in my controller and then just call ezStream()  to generate the final document.

Wrap Up

I barely scratched the surface of what you can do with the R&OS PDF library in this tutorial. I encourage you to spend some quality time with the readme.pdf  documentation file that comes with the library when you download it. That file goes over all the functions and their options in great detail.

So there you have it, generating PDF files with CodeIgniter and the R&OS library. If this method doesn’t quite do it for you, there are a few helpful articles on the CodeIgniter wiki , like this one  and this one , which walk you through some additional options. Whatever solution you choose I hope this tutorial has helped to introduce you to some of the options you have at your disposale for creating PDF reports with CodeIgniter.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值