簡體   English   中英

在CodeIgniter中使用DOMpdf生成PDF

[英]PDF generation using DOMpdf in CodeIgniter

我正在嘗試使用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

我怎么解決這個問題?

這個問題在dompdf 0.6中得到修復

或者您可以通過添加以下條件來糾正它:

DOMPDF /包括/ 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"];

步驟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        
}
}           
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM