简体   繁体   中英

Using dompdf library in codeigniter

can anybody help me, i'm trying to use dompdf library with codeigniter. When i do it on localhost its work. But it's not work properly when my website has been uploaded. Here's the library code

    <?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-0.6.2/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);
    }
}

this code to call create the pdf, i try to call the view only and it's work but when its called the "pdf" function, the screen is blank.

    $this->load->library('pdf');
    $this->pdf->load_view('mydocument', $data);
    $this->pdf->render();
    $this->pdf->stream("mydocument.pdf");

and this is structure of my files

enter image description here

As noted in the comments @jagad89 is right about including the autoload file from DOMPDF.

Instead of using it with a library wrapper, try installing with Composer

composer require dompdf/dompdf

And then setup the $config['composer_autoload'] = '/path/to/vendor/autoload.php'; in the config.php of your application and you should be good to use DOMPDF like this:

$dompdf = new Dompdf\Dompdf();
$dompdf->loadHtml('hello world');

// Render the HTML as PDF
$dompdf->render();
$dompdf->stream();

Use the following code in controller instead, It shoud be work for you.

$this->load->library('pdf');
$this->pdf->load_view('mydocument', $data);
$this->pdf->setPaper('A4', 'portrait');
$this->pdf->render();
$this->pdf->stream("mydocument", array("Attachment" => 0));

Download an archive of dompdf and extract it into the directory where dompdf will reside

Libraries/Pdf Class:

require_once APPPATH.'libraries/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf {
    $dompdf = new Dompdf();
    $dompdf->loadHtml('<h1>Hello Word</h1>');
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    $dompdf->stream('mydocument.pdf');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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