php Codeigniter 如何创建 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19806462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter how to create PDF
提问by Ralph Schipper
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.
我要创建一个发票系统,我现在正在为此做准备。我要使用 Codeigniter。问题是我想以 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.
小伙伴们有什么建议呢?我正在考虑将 HTML 转换为 PDF,或者在屏幕上显示发票并安装 pdf 打印机(当您点击打印并选择打印到 PDF 文件时)或者创建一个 word 文档并连接到我认为使用 odbc 的 MySQL 服务器.
Any of you guys have experience with this?
大家有这方面的经验吗?
回答by Siraj Khan
Please click on this link it should work ..
请单击此链接它应该可以工作..
http://www.php-guru.in/2013/html-to-pdf-conversion-in-codeigniter/
http://www.php-guru.in/2013/html-to-pdf-conversion-in-codeigniter/
Or you can see below
或者你可以在下面看到
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.
网络上有许多 PHP 库可以将 HTML 页面转换为 PDF 文件。当您在核心 PHP 中处理任何 Web 应用程序时,它们很容易实现和部署。但是当我们尝试将这些库与任何框架或模板集成时,如果我们使用的框架没有自己的库来将其与任何 PDF 库集成,那么工作就会变得非常乏味。当有一个要求将 HTML 页面转换为 PDF 文件并且我使用的框架是 codeigniter 时,同样的情况出现在我面前。
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.
我在网上搜索并获得了许多 PHP 库来将 HTML 页面转换为 PDF 文件。经过大量研究和谷歌搜索,我决定使用 TCPDF PHP 库将 HTML 页面转换为 PDF 文件以满足我的要求。我发现 TCPDf PHP 库很容易与 codeigniter 集成,并表示正在处理它。在成功完成我对 codeigniter 和 TCPDF 的集成后,我想到了在网络上分享这个脚本。
Now, let's start with implimentation of the code.
现在,让我们从代码的实现开始。
Download the TCPDF library code, you can download it from TCPDF website http://www.tcpdf.org/.
下载TCPDF库代码,可以从TCPDF网站http://www.tcpdf.org/下载。
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 开发的 Web 应用程序的“application/helpers/”目录中创建“tcpdf”文件夹。复制所有 TCPDF 库文件并将其粘贴到“application/helpers/tcpdf/”目录中。更新“application/helpers/tcpdf/config”目录下TCPDF的配置文件“tcpdf_config.php”,根据你的应用需求进行修改。我们可以在 cofing 文件中设置徽标、字体、字体大小、高度、标题等。授予对 tcpdf 文件夹中的“缓存”文件夹的读、写权限。在定义您的目录结构、更新配置文件和分配权限后,这里开始您的实际编码部分。
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
在codeigniter的“application/helpers/”目录下创建一个PHP helper文件,比如“pdf_helper.php”,然后复制下面给定的代码并粘贴到helper文件中
Helper: application/helpers/pdf_helper.php
助手:application/helpers/pdf_helper.php
function tcpdf()
{
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
}
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.
然后在控制器文件中调用上面的helper,假设我们的控制器文件是“createpdf.php”,它有pdf()方法,所以pdf()方法将加载“pdf_helper”helper,也有任何其他代码。
Controller: application/controllers/createpdf.php
控制器:application/controllers/createpdf.php
function pdf()
{
$this->load->helper('pdf_helper');
/*
---- ---- ---- ----
your code here
---- ---- ---- ----
*/
$this->load->view('pdfreport', $data);
}
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”,它也在控制器的 pdf() 方法中加载。所以在视图文件中我们可以直接调用我们在“pdf_helper”助手中定义的tcpdf()函数,它将加载所有需要的TCPDF类、函数、变量等。然后我们可以直接使用TCPDF示例代码,因为它是我们的当前控制器或视图。现在在当前视图“pdfreport”中复制下面给定的代码:
View: application/views/pdfreport.php
查看: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');
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.
因此,我们的 HTML 页面将使用 CodeIgniter 中的 TCPPDF 转换为 PDF。我们还可以使用 TCPDF 库在 PDF 文件中嵌入图像、CSS、修改。
回答by Manish
TCPDF is PHP class for generating pdf documents.Here we will learn TCPDF integration with CodeIgniter.we will use following step for TCPDF integration with CodeIgniter.
TCPDF 是用于生成pdf 文档的PHP 类。在这里我们将学习TCPDF 与CodeIgniter 的集成。我们将使用以下步骤将TCPDF 与CodeIgniter 集成。
Step 1
第1步
To Download TCPDF Click Here.
要下载 TCPDF,请单击此处。
Step 2
第2步
Unzip the above download inside application/libraries/tcpdf.
将上述下载文件解压到 application/libraries/tcpdf 中。
Step 3
第 3 步
Create a new file inside application/libraries/Pdf.php
在 application/libraries/Pdf.php 中创建一个新文件
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{ function __construct() { parent::__construct(); }
}
/*Author:Tutsway.com */
/* End of file Pdf.php */
/* Location: ./application/libraries/Pdf.php */
Step 4
第四步
Create Controller file inside application/controllers/pdfexample.php.
在 application/controllers/pdfexample.php 中创建控制器文件。
<?php
class pdfexample extends CI_Controller{
function __construct()
{ parent::__construct(); } function index() {
$this->load->library('Pdf');
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetTitle('Pdf Example');
$pdf->SetHeaderMargin(30);
$pdf->SetTopMargin(20);
$pdf->setFooterMargin(20);
$pdf->SetAutoPageBreak(true);
$pdf->SetAuthor('Author');
$pdf->SetDisplayMode('real', 'default');
$pdf->Write(5, 'CodeIgniter TCPDF Integration');
$pdf->Output('pdfexample.pdf', 'I'); }
}
?>
It is working for me. I have taken reference from http://www.tutsway.com/codeignitertcpdf.php
回答by Sudhir Khadka
I've used MPDFlibrary. For more information view this tutorial https://arjunphp.com/generating-a-pdf-in-codeigniter-using-mpdf/
我使用过MPDF库。有关更多信息,请查看本教程 https://arjunphp.com/generating-a-pdf-in-codeigniter-using-mpdf/
回答by Jeemusu
I've used dompdfwith some success before. Although it can be a bit fussy about malformed HTML and there are still some CSS methods that aren't supported (e.g. css float does not work).
我以前使用过dompdf并取得了一些成功。虽然它可能对格式错误的 HTML 有点挑剔,并且仍然有一些不受支持的 CSS 方法(例如 css float 不起作用)。
Download the package from github, and place it into a folder called dompdf
in your application/thirdparty
directory.
从 github 下载包,并将其放入目录中名为的文件夹dompdf
中application/thirdparty
。
You can then create a helper with some functions to use the dompdf library, here is an example:
然后你可以创建一个带有一些函数的帮助器来使用 dompdf 库,这里是一个例子:
dompdf_helper.php
:
dompdf_helper.php
:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE)
{
include APPPATH.'thirdparty/dompdf/dompdf_config.inc.php';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
if ($stream) {
$dompdf->stream($filename.".pdf", array("Attachment" => 0));
} else {
return $dompdf->output();
}
}
You simply pass the pdf_create
method your HTML as a string, the filename for the pdf file it will generate, and then an optional thirdparameter. The third parameter is a true/false flag to determine if it should save the file to your server before prompting the user to download it or not.
您只需将pdf_create
HTML 方法作为字符串传递,它将生成的 pdf 文件的文件名,然后是可选的第三个参数。第三个参数是一个真/假标志,用于确定是否应该在提示用户下载文件之前将文件保存到您的服务器。
回答by Ajay Malhotra
Create new pdf helper file in application\helpers folder and name it pdf_helper.php. Add below code in pdf_helper.php file:
在 application\helpers 文件夹中创建新的 pdf 帮助文件并将其命名为 pdf_helper.php。在 pdf_helper.php 文件中添加以下代码:
function tcpdf()
{
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
}
?>
For more Please follow the below link:
更多请点击以下链接:
http://www.ccode4u.com/how-to-generate-pdf-file-in-codeigniter.html
回答by Avnish alok
I have used mpdf in my project. In Codeigniter-3, putted mpdf files under application/third_party and then used in this way:
我在我的项目中使用了 mpdf。在Codeigniter-3中,将mpdf文件放在application/third_party下,然后这样使用:
/**
* This function is used to display data in PDF file.
* function is using mpdf api to generate pdf.
* @param number $id : This is unique id of table.
*/
function generatePDF($id){
require APPPATH . '/third_party/mpdf/vendor/autoload.php';
//$mpdf=new mPDF();
$mpdf = new mPDF('utf-8', 'Letter', 0, '', 0, 0, 7, 0, 0, 0);
$checkRecords = $this->user_model->getCheckInfo($id);
foreach ($checkRecords as $key => $value) {
$data['info'] = $value;
$filename = $this->load->view(CHEQUE_VIEWS.'index',$data,TRUE);
$mpdf->WriteHTML($filename);
}
$mpdf->Output(); //output pdf document.
//$content = $mpdf->Output('', 'S'); //get pdf document content's as variable.
}