DOMPDF with CODEIGNITER

后端 未结 6 642
刺人心
刺人心 2021-01-07 12:16

I am trying to get dompdf working with codeigniter, I download the file from here https://github.com/dompdf/dompdf/releases the version was 0.7.0 and then I put inside my co

相关标签:
6条回答
  • 2021-01-07 12:16

    You need to use v0.6. v0.7 uses namespaces which are not supported in CodeIgniter.

    0 讨论(0)
  • 2021-01-07 12:24

    You can integrate the DomPDF with Codeigniter in two easy steps.

    1) Download the domPDF library and place it into application/library directory.

    2) Create a file name pdf.php file, place following code inside the file and place it into library directory.

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

    Now in your Controller file call it as this

    defined('BASEPATH') OR exit('No direct script access allowed');
    class Welcome extends CI_Controller {
     function my_DOMPDF(){
      $this->load->library('pdf');
      $this->pdf->load_view('common/template');
      $this->pdf->render();
      $this->pdf->stream("welcome.pdf");
     }
    }
    

    You can get full step by step configurations here

    0 讨论(0)
  • 2021-01-07 12:30

    @Zedd Answer is right

    but for the latest version as on this post date, the following will work.

    application/libraries/Pdf.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 APPPATH.'third_party/dompdf/autoload.inc.php';
    
    use Dompdf\Dompdf;
    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())
    {
        $dompdf = new Dompdf();
        $html = $this->ci()->load->view($view, $data, TRUE);
    
        $dompdf->loadHtml($html);
    
        // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'landscape');
    
        // Render the HTML as PDF
        $dompdf->render();
        $time = time();
    
        // Output the generated PDF to Browser
        $dompdf->stream("welcome-". $time);
    }
    }
    

    At your controller

    function pdf_gen(){
    
        $this->load->library('pdf');
        $this->pdf->load_view('main_report');
    }
    

    Obviously main_report is the view file at application/views/main_report and you can send data for the view file like the following too

    $this->pdf->load_view('main_report', $data);
    

    And Note

    I placed the dompdf folder in the application/thirdparty folder and not in the application/libraries.

    Make sure you have commented out all print, echo functions in your controller method, else you might get an error saying something similar to "the pdf file is damaged".

    Hope this post was usefull.

    0 讨论(0)
  • 2021-01-07 12:35

    The simplest way creating PDF file. Download the library from
    https://github.com/hanzzame/ci3-pdf-generator-library
    Extract zip at app/application/libraries/extract-here

    Use in method of Controller:

    $this->load->library('pdf'); // change to pdf_ssl for ssl
    $filename = "Document_name";
    $html = $this->load->view('your_view_template');
    $this->pdf->create($html, $filename);
    
    0 讨论(0)
  • 2021-01-07 12:38

    Since v. 0.7 you need to include in your code reference to dompdfs namespace:

    use Dompdf\Dompdf;
    
    0 讨论(0)
  • 2021-01-07 12:41
    require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');
    use Dompdf\Dompdf;
    
    class Pdf
    {
     public function create($html,$filename)
     {
       $dompdf = new Dompdf();
       $dompdf->loadHtml($html);
       $dompdf->render();
       $dompdf->stream($filename.'.pdf');
     }
    

    Try this library https://github.com/hanzzame/ci3-pdf-generator-library

    This library using

    • Dompdf 0.8.0
    • Codeigniter 3.x.x
    • Support SSL Connection for image
    0 讨论(0)
提交回复
热议问题