本文介绍了Codeigniter如何创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个发票系统,我现在正在准备。
我要使用Codeigniter。
事情是,我想创建PDF的发票,所以我可以通过电子邮件发送。

I'm going to create an invoice system, I am preparing for that now.I'm gonna use Codeigniter.The thing is that I want to create invoice in PDF so I can send it through email.

你们的建议是什么?
我正在考虑HTML到PDF转换,
或者在屏幕上显示发票并安装一个pdf打印机(当您打印并选择打印到PDF文件)
或创建一个

What is your guys advice?I was thinking of HTML to PDF conversion,Or Show the invoice on screen and install a pdf printer (when you hit print and you choose to print to a PDF file)Or create a word document and connect to the MySQL server I think with odbc.

任何人都有这方面的经验?

Any of you guys have experience with this?

推荐答案

请点击此链接。

或者您可以在下面看到

有许多PHP库可以将HTML页面转换为PDF文件。当您在核心PHP中处理任何Web应用程序时,它们易于实施和部署。但是当我们尝试将这个库与任何框架或模板集成时,如果我们使用的框架没有自己的库来与任何PDF库集成,这将变得非常乏味的工作。同样的情况出现在我的面前,当有一个要求将HTML页面转换为PDF文件,我使用的框架是codeigniter。

There are number of PHP libraries on the web to convert HTML page to PDF file. They are easy to implement and deploy when you are working on any web application in core PHP. But when we try to integrate this libraries with any framework or template, then it becomes very tedious work if the framework which we are using does not have its own library to integrate it with any PDF library. The same situation came in front of me when there was one requirement to convert HTML page to PDF file and the framework I was using was codeigniter.

我在网上搜索,将HTML页面转换为PDF文件的PHP库数。经过大量的研究和谷歌搜索,我决定使用TCPDF PHP库将HTML页面转换为PDF文件为我的要求。我发现TCPDf PHP库很容易与codeigniter集成,并声明工作。在成功完成我的codeigniter和TCPDF的集成后,我想到了在网络上共享这个脚本。

I searched on web and got number of PHP libraries to convert HTML page to PDF file. After lot of research and googling I decided to go with TCPDF PHP library to convert HTML page to PDF file for my requirement. I found TCPDf PHP library quite easy to integrate with codeigniter and stated working on it. After successfully completing my integration of codeigniter and TCPDF, I thought of sharing this script on web.

现在,让我们从代码的实现开始。

Now, let’s start with implimentation of the code.

下载TCPDF库代码,您可以从TCPDF网站。

Download the TCPDF library code, you can download it from TCPDF website http://www.tcpdf.org/.

现在在由codeigniter开发的web应用程序的application / helpers /目录中创建tcpdf文件夹。复制所有TCPDF库文件,并将其粘贴到application / helpers / tcpdf /目录中。更新位于application / helpers / tcpdf / config目录中的TCPDF的配置文件tcpdf_config.php,根据应用程序要求进行更改。我们可以在cofing文件中设置徽标,字体,字体大小,高度,标题等。给tcpdf文件夹中的cache文件夹读取,写入权限。

Now create "tcpdf" folder in "application/helpers/" directory of your web application which is developed in codeigniter. Copy all TCPDF library files and paste it in "application/helpers/tcpdf/" directory. Update the configuration file "tcpdf_config.php" of TCPDF, which is located in "application/helpers/tcpdf/config" directory, do changes according to your applicatoin requirements. We can set logo, font, font size, with, height, header etc in the cofing file. Give read, write permissions to "cache" folder which is there in tcpdf folder. After defining your directory structure, updating configuration file and assigning permissions, here starts your actual coding part.

在codeigniter的application / helpers /目录中创建一个PHP帮助文件,说pdf_helper.php,然后复制以下给定代码并将其粘贴到辅助文件中

Create one PHP helper file in "application/helpers/" directory of codeigniter, say "pdf_helper.php", then copy below given code and paste it in helper file

帮助程序:application / helpers / pdf_helper.php

Helper: application/helpers/pdf_helper.php

function tcpdf()
{
    require_once('tcpdf/config/lang/eng.php');
    require_once('tcpdf/tcpdf.php');
}

然后在controller文件中调用上面的helper,假设我们的控制器文件是createpdf .php和它的方法为pdf(),所以方法pdf()将加载pdf_helper帮助器,也将有任何其他代码。

Then in controller file call the above helper, suppose our controller file is "createpdf.php" and it has method as pdf(), so the method pdf() will load the "pdf_helper" helper and will also have any other code.

application / controllers / createpdf.php

Controller: application/controllers/createpdf.php

function pdf()
{
    $this->load->helper('pdf_helper');
    /*
        ---- ---- ---- ----
        your code here
        ---- ---- ---- ----
    */
    $this->load->view('pdfreport', $data);
}

现在创建一个视图文件, views /目录,这也在控制器中的pdf()方法中加载。所以在视图文件中,我们可以直接调用我们在pdf_helper帮助中定义的tcpdf()函数,这将加载所有需要的TCPDF类,函数,变量等。然后我们可以直接使用TCPDF示例代码,电流控制器或视图。现在在out当前视图pdfreport复制给定的代码如下:

Now create one view file, say "pdfreport.php" in "application/views/" directory, which is also loaded in pdf() method in controller. So in view file we can directly call the tcpdf() function which we have defined in "pdf_helper" helper, which will load all required TCPDF classes, functions, variable etc. Then we can directly use the TCPDF example codes as it is in our current controller or view. Now in out current view "pdfreport" copy the given code below:

查看:application / views / pdfreport.php

View: application/views/pdfreport.php

tcpdf();
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$title = "PDF Report";
$obj_pdf->SetTitle($title);
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $title, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$obj_pdf->SetFont('helvetica', '', 9);
$obj_pdf->setFontSubsetting(false);
$obj_pdf->AddPage();
ob_start();
    // we can have any view part here like HTML, PHP etc
    $content = ob_get_contents();
ob_end_clean();
$obj_pdf->writeHTML($content, true, false, true, false, '');
$obj_pdf->Output('output.pdf', 'I');

因此,我们的HTML页面将在CodeIgniter中使用TCPDF转换为PDF。我们还可以通过使用TCPDF库在PDF文件中嵌入图片,CSS,修改。

Thus our HTML page will be converted to PDF using TCPDF in CodeIgniter. We can also embed images,css,modifications in PDF file by using TCPDF library.

这篇关于Codeigniter如何创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:38