简体   繁体   English

在CodeIgniter中使用DOMpdf生成PDF

[英]PDF generation using DOMpdf in CodeIgniter

I am trying to generate a pdf using dompdf. 我正在尝试使用dompdf生成pdf。

 <?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Dompdf_test extends CI_Controller {

    public function index() {
        // Load all views as normal
        //$this->load->view('phptopdfexample');
        $this->all_movements();
        // Get output html
        $html = $this->output->get_output();

        // Load library
        $this->load->library('dompdf_gen');

        // Convert to PDF
        $this->dompdf->load_html($html);

        $this->dompdf->render();
        $min = 1;
        $max = 1000;
        $name = rand($min, $max);
        $this->dompdf->stream($name . '.pdf');
    }

    public function all_movements() {
        $data['stocks'] = $this->inventory->getdepartmentalmovements();
        $data['meds'] = $this->inventory->get_meds();

        $this->load->view('deptartmental_issue_pdf', $data);
    }

}

When I run the script, I get an internal server error with the following error : A PHP Error was encountered 当我运行脚本时,我收到内部服务器错误,并出现以下错误:遇到PHP错误

Severity: Warning 严重性:警告

Message: Illegal string offset 'hex' 消息:非法字符串偏移'hex'

Filename: include/style.cls.php 文件名:include / style.cls.php

Line Number: 1422 行号:1422

How can I solve this problem? 我怎么解决这个问题?

This problem is fixed in dompdf 0.6 这个问题在dompdf 0.6中得到修复

or you may correct it by adding a condition in: 或者您可以通过添加以下条件来纠正它:

dompdf/include/style.cls.php DOMPDF /包括/ style.cls.php

then search for if ( is_null($col) ) (may be: Line 1422 or near of it) 然后搜索if(is_null($ col))(可能是:1422行或其附近)

if ( is_null($col) )
$col = self::$_defaults["color"];
//see __set and __get, on all assignments clear cache, not needed on direct set through __set
$this->_prop_cache["color"] = null;
$this->_props["color"] = $col["hex"];
}

add this condition also, and try. 也添加这个条件,并尝试。

if (is_array($col))
     $this->_props["color"] = $col["hex"];

Step 1: Download Dompdf library from " https://github.com/dompdf/dompdf/releases ". 步骤1:从“ https://github.com/dompdf/dompdf/releases ”下载Dompdf库。

Step 2: paste it in libraries folder like "\\xamppp\\htdocs\\codeigiter\\application\\libraries". 第2步:将其粘贴到库文件夹中,如“\\ xamppp \\ htdocs \\ codeigiter \\ application \\ libraries”。

Step 3: Also Create one file named like "Pdf.php" in libraries folder and paste below code in it. 步骤3:还在库文件夹中创建一个名为“Pdf.php”的文件,并将代码粘贴到其中。

require 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;

class Pdf extends Dompdf    
{
public function __construct()
    {
    parent::__construct();      
    $dompdf = new Dompdf(); 
    }
}
?>

Step 4: Create pdf controller and paste below code in this and run controller. 第4步:创建pdf控制器并粘贴下面的代码并运行控制器。

  <?php defined('BASEPATH') OR exit('No direct script access allowed');

class Printbill extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->library('pdf');
}

public function index()
{
    $this->load->library('pdf');
    $this->pdf->loadHtml('html code or variable');
    // $customPaper = array(0,0,570,570);
    //$this->pdf->set_paper($customPaper);
    $this->pdf->setPaper('A4','portrait');//landscape
    $this->pdf->render();
    $this->pdf->stream("abc.pdf", array('Attachment'=>0));
    //'Attachment'=>0 for view and 'Attachment'=>1 for download file        
}
}           
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM