`
天梯梦
  • 浏览: 13730609 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

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 FPDFPanda  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:

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

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();
}
 

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.

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@someplacepsecial.com');

$col_names = array(
'name' => 'Name',
'phone' => 'Phone Number',
'email' => 'E-mail Address'
);

$this->cezpdf->ezTable($table_data, $col_names, 'Contact List', array('width'=>550));
$this->cezpdf->ezStream();
}
 

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.

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:s a'));
$CI->cezpdf->addText(50,22,8,'CI PDF Tutorial - http://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 - http://www.christophermonnat.com');
}
$CI->cezpdf->restoreState();
$CI->cezpdf->closeObject();
$CI->cezpdf->addObject($all,'all');
}
 

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.

分享到:
评论
1 楼 亚飞正传 2012-07-24  
thanks!

相关推荐

    CodeIgniter.pdf

    - 非命令行依赖:不强制要求使用命令行操作,支持图形界面。 - 简洁自由:不强制特定编码规则,追求简单直接的开发方式。 - 文档清晰:拥有完整且详尽的开发者文档,便于学习和参考。 - 模板语言可选:虽然不强制...

    CodeIgniter3.00中文手册pdf

    手册还专门讲解了CodeIgniter的常规主题,如项目结构、代码风格、安全性、维护等实际操作中经常遇到的问题,为开发者提供全面的指导。 此外,CodeIgniter鼓励开发者为框架做出贡献。如果用户希望贡献代码或文档,...

    codeigniter用户指南 pdf

    以下是对`codeigniter用户指南 pdf`内容的详细概述。 **一、CodeIgniter概述** CodeIgniter是一个轻量级的PHP框架,它提供了丰富的库和助手函数,以减少开发者在编写重复代码上的时间。它的核心理念是高效、简单和...

    CodeIgniter3中文手册

    CodeIgniter以其小巧、高性能、易于使用和配置、不需要命令行、无需新的模板语言、以及简化的编码规则等特点受到开发者的青睐。CodeIgniter的核心思想是减少编码工作量,让开发者能够把精力集中在创新性工作的开发上...

    基于MVC模式的PHP开发框架CodeIgniter.pdf

    在本文中,我们详细介绍了MVC模式和CodeIgniter框架的特点和优势,并讨论了使用CodeIgniter框架的好处。通过本文,读者可以了解到MVC模式和CodeIgniter框架的基本概念和应用,提高自己的开发技能和知识。

    codeigniter 操作 Rss

    在“CodeIgniter操作Rss”这个主题中,我们将深入探讨如何在CodeIgniter框架中处理RSS(Really Simple Syndication) feeds,这是一种广泛使用的格式,用于发布和订阅新闻、博客文章和其他定期更新的内容。...

    CodeIgniter

    在CodeIgniter中,模型类用来与数据库交互,执行查询和数据操作。 2. **视图(View)**:视图是用户看到并与其交互的界面。它们通常包含HTML、CSS和JavaScript,不过在CodeIgniter中,视图文件仅包含HTML结构,其他...

    PHP 敏捷开发框架 CodeIgniter

    **四、CodeIgniter的数据库操作** 1. 使用Active Record模式进行数据库查询,简化SQL语句编写。 2. 支持多种数据库引擎,包括MySQL、PostgreSQL等。 3. 数据库事务处理,确保数据的一致性和完整性。 **五、表单...

    CodeIgniter 相关文档资料

    这本PDF教程可能是面向初学者的全面指南,它从基础开始介绍如何使用CodeIgniter构建Web应用。内容可能包括安装和设置开发环境,创建第一个“Hello, World!”程序,以及逐步讲解如何实现CRUD操作(创建、读取、更新...

    codeigniter_files.rar

    在"codeigniter_files.rar"这个压缩包中,包含的文件是CodeIgniter框架的基础结构。 1. **index.php** - 这是CodeIgniter应用的入口点。所有HTTP请求都会指向这个文件。`index.php`负责加载框架的核心组件,包括...

    CodeIgniter and Ajax using jQuery.zip

    1. `CodeIgniter and Ajax using jQuery.htm`:这可能是项目的主要HTML文件,包含了用于触发Ajax请求的jQuery代码。用户界面可能通过此文件与服务器进行交互,如表单提交、数据加载等。 2. `index.php`:这是...

    PHP敏捷开发框架CodeIgniter

    本书详细讲解了 CI 的一些主要特性。本书并不包含 CI 的所有内容和全部细节。...本书系统地讲解了 CodeIgniter 的主要特性,并配合相应的代码范例进行了详尽的解释,使你能够由浅入深地掌握 CodeIgniter。

    CodeIgniter-PDF-Generator-Library:使用这个基于 domPDF 的易于使用的库在 CodeIgniter 中生成 PDF

    CodeIgniter-PDF-Generator-Library 已弃用:正在为此包寻找新的维护者。 如果有兴趣,请打开一个问题。 使用这个基于 domPDF 的易于使用的库在 CodeIgniter 中生成 PDF。安装将 pdf.php 库放入您的库目录中。 从...

    CodeIgniter_2.1.2 中文操作手冊

    CodeIgniter_2.1.2 中文操作手冊

    PHP框架之CodeIgniter留言板实例

    PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter...

Global site tag (gtag.js) - Google Analytics