我正在尝试使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();
}
但是我说错了Fatal error: Class 'DOMPDF' not found in C:\Apache24\htdocs\faceloan\faceloan\application\controllers\moneyexchange.php on line 2495
您需要使用v0.6。 v0.7使用CodeIgniter不支持的名称空间。
@ Zedd答案是正确的
但是对于截至发布日期的最新版本,以下将适用。
application / libraries / 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);
}
}
在您的控制器上
function pdf_gen(){
$this->load->library('pdf');
$this->pdf->load_view('main_report');
}
显然main_report是位于application / views / main_report的视图文件并且您也可以像下面一样发送视图文件的数据
$this->pdf->load_view('main_report', $data);
并注意
我将dompdf文件夹放置在应用程序/第三方文件夹中,而不是应用程序/库中。
请确保已注释掉控制器方法中的所有打印,回显功能,否则可能会出现错误,提示类似于“ pdf文件已损坏”。
希望这篇文章有用。
您可以通过两个简单的步骤将DomPDF与Codeigniter集成。
1)Download domPDF库并将其放置在application / library目录中。
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);
}
}
?>
现在在您的[[Controller文件中,将其称为此
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");
}
}
您可以获得完整的step by step configurations here
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');
}
尝试使用此库https://github.com/hanzzame/ci3-pdf-generator-library此库使用
use Dompdf\Dompdf;
在控制器的方法中使用:
$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);