【问题标题】:PDF generation using DOMpdf in CodeIgniter在 CodeIgniter 中使用 DOMpdf 生成 PDF
【发布时间】:2014-01-31 11:20:32
【问题描述】:

我正在尝试使用 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);
    }

}

当我运行脚本时,我收到一个内部服务器错误,并出现以下错误: 遇到 PHP 错误

严重性:警告

消息:非法字符串偏移 'hex'

文件名:include/style.cls.php

行号:1422

我该如何解决这个问题?

【问题讨论】:

  • 您尝试以某种方式或其他方式将字符串用作数组。 style.cls.php 的第 1422 行是什么?

标签: php codeigniter dompdf


【解决方案1】:

dompdf 0.6 修复了这个问题

或者您可以通过在以下位置添加条件来纠正它:

dompdf/include/style.cls.php

然后搜索 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"];
}

也添加这个条件,然后尝试。

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

【讨论】:

    【解决方案2】:

    第 1 步:从“https://github.com/dompdf/dompdf/releases”下载 Dompdf 库。

    第 2 步:将其粘贴到库文件夹中,例如“\xamppp\htdocs\codeigiter\application\libraries”。

    第 3 步:还在库文件夹中创建一个名为“Pdf.php”的文件,并将以下代码粘贴到其中。

    require 'dompdf/autoload.inc.php';
    use Dompdf\Dompdf;
    
    class Pdf extends Dompdf    
    {
    public function __construct()
        {
        parent::__construct();      
        $dompdf = new Dompdf(); 
        }
    }
    ?>
    

    第 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        
    }
    }           
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-17
      • 2017-08-14
      • 2012-06-06
      • 2015-10-30
      • 2020-07-12
      • 2012-09-13
      • 2011-06-27
      • 2011-06-29
      相关资源
      最近更新 更多