DOM pdf codeigniter: Class 'DOMPDF' not fo

2019-01-27 02:28发布

问题:

I trying to add DOM PDF library to my codeigniter application

1.Download dompdf and copy the dompdf folder to libraries folder.

2.Create file named Dompdf.php in libraries folder

In my controller

public function pdf_test()
   {
       $this->load->library('Dompdf');
       $this->Dompdf->loadHtml('hello world');
       $this->pdf->render();
       $this->pdf->stream("welcome.pdf");
   }

Dompdf.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/autoload.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);
    }
}

But i will this error.

Message: Class 'DOMPDF' not found

Filename: libraries/Dompdf.php

Line Number: 16

I am using latest DOMPDF

回答1:

in my case. i use this

$dompdf = new Dompdf\DOMPDF();

require_once(_MAP."libraries/dompdf/autoload.inc.php");
$dompdf = new Dompdf\DOMPDF();
$html = 'rats :)';
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");


回答2:

Starting with v0.7.0 Dompdf uses namespaces. You probably need to add a use statement prior to referencing the class. Or reference it using the full namespace path.

Try:

require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
use Dompdf\Dompdf;
class Pdf extends Dompdf
{
...
}

Usage is available in the readme or on the wiki usage page.



回答3:

I have done pdf generation with tcpdf library but with a slightly different approach from you. Here is my solution with tcpdf. You can try it with Dompdf.

Download tcpdf and put in third_party folder Make a file PDF.php in libraries folder with the following contents

 require_once APPPATH."third_party/tcpdf/tcpdf.php";

    class PDF extends TCPDF {
        public function __construct() {
            parent::__construct();
        }
    }

In controllers folder create a file Createpdf.php with following codes

defined("BASEPATH") OR exit("No direct script access allowed");

    class Createpdf extends CI_Controller {

        public function pdf()
        {
            $this->load->library("pdf");
            $data["content"] = "Hello from CodeIgniter with TCPDF...";
            $this->load->view("pdfreport", $data);
        }
    }

and the pdfreport.php view is:

$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->AddPage();
$obj_pdf->writeHTML($content, true, false, true, false, '');
$obj_pdf->Output('output.pdf', 'I');