- 浏览: 13730609 次
- 性别:
- 来自: 洛杉矶
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
CodeIgniter 操作PDF ( Generating PDF files using CodeIgniter )
- 博客分类:
- Codeigniter / CakePHP
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
. 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. 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: 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. 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. 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. 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. 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. 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. 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.Quick Note
Getting Started
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();
}
Handling Tabular Data
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();
}
Headers and Footers
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');
}
Wrap Up
- pdf_tutorial.zip (1.3 MB)
- 下载次数: 28
发表评论
-
CakePHP你必须知道的21条技巧
2012-10-19 06:25 1882原文链接:http://www.avatarfinancial ... -
cakephp 1.3 Views
2012-10-09 06:12 1442Section 1 Views 视图 一个vie ... -
cakephp 1.3 Models
2012-10-09 06:07 2501Section 1 What is a model? ... -
cakephp 1.3 Controller
2012-10-09 05:49 3333Controller 一个controller用于管理 ... -
cakephp 1.3 配置CakePHP
2012-10-09 05:31 4644Section 1 数据库配置 app/config/ ... -
CakePHP 2.x十分钟博客教程
2012-10-07 01:27 244131. CakePHP2十分钟博客教 ... -
Create an Admin panel with CodeIgniter
2010-05-23 02:15 4175Create an Admin panel with Code ... -
Codeigniter Grid 使用方法 (flexigrid)
2010-05-23 02:05 2803来源:http://codeigniter.com/forum ... -
CI集成 ckeditor 配置
2010-05-23 01:34 3752配置 ckeditor 1.上传 下载 ckedito ... -
codeigniter 辅助函数 - 敏感词过滤
2010-05-05 06:18 4575我们都知道有些敏感的词汇是不适合出现在互联网上的,特别是在有用 ... -
实现简单 codeigniter 缓存 (cache)
2010-04-30 23:47 5278代码 class Test extends Contr ... -
CKEditor Helper for CodeIgniter
2010-04-19 00:37 3975Using CKEditor as a plugin in y ... -
codeigniter 生成 excel
2010-04-19 00:33 3322Excel Plugin Th ... -
CakePHP 中文手册
2010-04-14 21:04 2335基本概念 Section1 简介 ... -
利用 Cache_Lite代替codeigniter中的cache功能
2010-01-29 06:15 5508codeigniter的功能纵然强大,也有不足之处。其cach ... -
CodeIgniter 合作 Authorize.net
2009-12-30 00:25 1612function payment(){ // 略... ... -
CodeIgniter 合作paypal
2009-12-30 00:15 2350<?php class Paypal extend ... -
CodeIgniter 操作 CSV
2009-12-29 07:17 4632A Comma separated values (CSV) ... -
codeigniter 操作 Rss
2009-12-29 07:12 1970I wrote a codeigniter library t ... -
codeigniter操作xml
2009-12-29 06:57 3993This Simplexml class provides a ...
相关推荐
- 非命令行依赖:不强制要求使用命令行操作,支持图形界面。 - 简洁自由:不强制特定编码规则,追求简单直接的开发方式。 - 文档清晰:拥有完整且详尽的开发者文档,便于学习和参考。 - 模板语言可选:虽然不强制...
手册还专门讲解了CodeIgniter的常规主题,如项目结构、代码风格、安全性、维护等实际操作中经常遇到的问题,为开发者提供全面的指导。 此外,CodeIgniter鼓励开发者为框架做出贡献。如果用户希望贡献代码或文档,...
以下是对`codeigniter用户指南 pdf`内容的详细概述。 **一、CodeIgniter概述** CodeIgniter是一个轻量级的PHP框架,它提供了丰富的库和助手函数,以减少开发者在编写重复代码上的时间。它的核心理念是高效、简单和...
CodeIgniter以其小巧、高性能、易于使用和配置、不需要命令行、无需新的模板语言、以及简化的编码规则等特点受到开发者的青睐。CodeIgniter的核心思想是减少编码工作量,让开发者能够把精力集中在创新性工作的开发上...
在本文中,我们详细介绍了MVC模式和CodeIgniter框架的特点和优势,并讨论了使用CodeIgniter框架的好处。通过本文,读者可以了解到MVC模式和CodeIgniter框架的基本概念和应用,提高自己的开发技能和知识。
在“CodeIgniter操作Rss”这个主题中,我们将深入探讨如何在CodeIgniter框架中处理RSS(Really Simple Syndication) feeds,这是一种广泛使用的格式,用于发布和订阅新闻、博客文章和其他定期更新的内容。...
在CodeIgniter中,模型类用来与数据库交互,执行查询和数据操作。 2. **视图(View)**:视图是用户看到并与其交互的界面。它们通常包含HTML、CSS和JavaScript,不过在CodeIgniter中,视图文件仅包含HTML结构,其他...
**四、CodeIgniter的数据库操作** 1. 使用Active Record模式进行数据库查询,简化SQL语句编写。 2. 支持多种数据库引擎,包括MySQL、PostgreSQL等。 3. 数据库事务处理,确保数据的一致性和完整性。 **五、表单...
这本PDF教程可能是面向初学者的全面指南,它从基础开始介绍如何使用CodeIgniter构建Web应用。内容可能包括安装和设置开发环境,创建第一个“Hello, World!”程序,以及逐步讲解如何实现CRUD操作(创建、读取、更新...
在"codeigniter_files.rar"这个压缩包中,包含的文件是CodeIgniter框架的基础结构。 1. **index.php** - 这是CodeIgniter应用的入口点。所有HTTP请求都会指向这个文件。`index.php`负责加载框架的核心组件,包括...
1. `CodeIgniter and Ajax using jQuery.htm`:这可能是项目的主要HTML文件,包含了用于触发Ajax请求的jQuery代码。用户界面可能通过此文件与服务器进行交互,如表单提交、数据加载等。 2. `index.php`:这是...
本书详细讲解了 CI 的一些主要特性。本书并不包含 CI 的所有内容和全部细节。...本书系统地讲解了 CodeIgniter 的主要特性,并配合相应的代码范例进行了详尽的解释,使你能够由浅入深地掌握 CodeIgniter。
CodeIgniter-PDF-Generator-Library 已弃用:正在为此包寻找新的维护者。 如果有兴趣,请打开一个问题。 使用这个基于 domPDF 的易于使用的库在 CodeIgniter 中生成 PDF。安装将 pdf.php 库放入您的库目录中。 从...
CodeIgniter_2.1.2 中文操作手冊
PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter...