php 带有 CODEIGNITER 的 DOMPDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37831516/
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
DOMPDF with CODEIGNITER
提问by XAF
I am trying to get dompdf working with codeigniter, I download the file from here https://github.com/dompdf/dompdf/releasesthe version was 0.7.0 and then I put inside my controller a piece of code like this
我试图让 dompdf 与 codeigniter 一起工作,我从这里下载文件https://github.com/dompdf/dompdf/releases版本是 0.7.0 然后我在我的控制器中放了一段这样的代码
public function generatepdf(){
$this->load->helper('file');
require_once(APPPATH.'third_party/dompdf/autoload.inc.php');
$domdpf = new DOMPDF();
$domdpf -> loadHtml('<h1>HELLO WORLD</h1>');
$domdpf -> setPaper('A4','Landscape');
$domdpf ->render();
$domdpf->stream();
}
But I am getting a error saying
Fatal error: Class 'DOMPDF' not found in C:\Apache24\htdocs\faceloan\faceloan\application\controllers\moneyexchange.php on line 2495
但我收到一个错误说
Fatal error: Class 'DOMPDF' not found in C:\Apache24\htdocs\faceloan\faceloan\application\controllers\moneyexchange.php on line 2495
采纳答案by Efekan
You need to use v0.6. v0.7 uses namespaces which are not supported in CodeIgniter.
您需要使用 v0.6。v0.7 使用了 CodeIgniter 不支持的命名空间。
回答by Abilash Arjunan
@Zedd Answer is right
@Zedd 答案是对的
but for the latest version as on this post date, the following will work.
但是对于此发布日期的最新版本,以下内容将起作用。
application/libraries/Pdf.php
应用程序/库/PDF.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PDF Library
*
* Generate PDF's in your CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Chris Harvey
* @license MIT License
* @link https://github.com/chrisnharvey/CodeIgniter- PDF-Generator-Library
*/
require_once APPPATH.'third_party/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf extends DOMPDF
{
/**
* Get an instance of CodeIgniter
*
* @access protected
* @return void
*/
protected function ci()
{
return get_instance();
}
/**
* Load a CodeIgniter view into domPDF
*
* @access public
* @param string $view The view to load
* @param array $data The view data
* @return void
*/
public function load_view($view, $data = array())
{
$dompdf = new Dompdf();
$html = $this->ci()->load->view($view, $data, TRUE);
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
$time = time();
// Output the generated PDF to Browser
$dompdf->stream("welcome-". $time);
}
}
At your controller
在您的控制器
function pdf_gen(){
$this->load->library('pdf');
$this->pdf->load_view('main_report');
}
Obviously main_report is the view file at application/views/main_report and you can send data for the view file like the following too
显然 main_report 是 application/views/main_report 中的视图文件,您也可以发送视图文件的数据,如下所示
$this->pdf->load_view('main_report', $data);
And Note
并注意
I placed the dompdf folder in the application/thirdparty folder and not in the application/libraries.
我将 dompdf 文件夹放在 application/thirdparty 文件夹中,而不是在 application/libraries 中。
Make sure you have commented out all print, echo functions in your controller method, else you might get an error saying something similar to "the pdf file is damaged".
确保您已注释掉控制器方法中的所有打印、回显功能,否则您可能会收到类似于“pdf 文件已损坏”的错误消息。
Hope this post was usefull.
希望这篇文章有用。
回答by Zedd Index
You can integrate the DomPDF with Codeigniter in two easy steps.
您可以通过两个简单的步骤将 DomPDF 与 Codeigniter 集成。
1) Downloadthe domPDF library and place it into application/library directory.
1) 下载domPDF库,放到application/library目录下。
2) Create a file name pdf.phpfile, place following code inside the file and place it into library directory.
2) 创建一个文件名pdf.php文件,将以下代码放入文件中并放入库目录中。
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PDF Library
*
* Generate PDF's in your CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Chris Harvey
* @license MIT License
* @link https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
*/
require_once(dirname(__FILE__) . '/dompdf/dompdf_config.inc.php');
class Pdf extends DOMPDF
{
/**
* Get an instance of CodeIgniter
*
* @access protected
* @return void
*/
protected function ci()
{
return get_instance();
}
/**
* Load a CodeIgniter view into domPDF
*
* @access public
* @param string $view The view to load
* @param array $data The view data
* @return void
*/
public function load_view($view, $data = array())
{
$html = $this->ci()->load->view($view, $data, TRUE);
$this->load_html($html);
}
}
?>
Now in your Controllerfile call it as this
现在在您的控制器文件中将其称为
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function my_DOMPDF(){
$this->load->library('pdf');
$this->pdf->load_view('common/template');
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}
}
You can get full step by step configurations here
回答by MuHanz
require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
use Dompdf\Dompdf;
class Pdf
{
public function create($html,$filename)
{
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream($filename.'.pdf');
}
Try this library https://github.com/hanzzame/ci3-pdf-generator-library
试试这个库https://github.com/hanzzame/ci3-pdf-generator-library
This library using
这个库使用
- Dompdf 0.8.0
- Codeigniter 3.x.x
- Support SSL Connection for image
- Dompdf 0.8.0
- 代码点火器 3.xx
- 支持图像的 SSL 连接
回答by PawelN
Since v. 0.7 you need to include in your code reference to dompdfs namespace:
从 v. 0.7 开始,您需要在代码中包含对 dompdfs 命名空间的引用:
use Dompdf\Dompdf;
回答by Amir Khan
The simplest way creating PDF file.
Download the library from
https://github.com/hanzzame/ci3-pdf-generator-library
Extract zip at app/application/libraries/extract-here
创建 PDF 文件的最简单方法。从
https://github.com/hanzzame/ci3-pdf-generator-library下载库
提取 zip 位于 app/application/libraries/extract-here
Use in method of Controller:
在Controller的方法中使用:
$this->load->library('pdf'); // change to pdf_ssl for ssl
$filename = "Document_name";
$html = $this->load->view('your_view_template');
$this->pdf->create($html, $filename);