Friday, January 28, 2011

Exsport to PDF with dompdf in Code Igniter

  1. cDownload dompf from here and exstract the file.
  2. Then put to the directory ./system/application/plugins/.
  3. After that then create new file on ./system/application/plugins/dompdf_pi.php like this
  4. <?php
    
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    /**
     * Try increasing memory available, mostly for PDF generation
     */
    ini_set("memory_limit", "32M");
    
    function pdf_create($html, $filename, $stream=TRUE, $orientation="portrait") {
        require_once(BASEPATH . "application/plugins/dompdf/dompdf_config.inc.php");
    
        $dompdf = new DOMPDF();
        $dompdf->set_paper("a4", $orientation);
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) { //open only
            $dompdf->stream($filename . ".pdf");
        } else { // save to file only, your going to load the file helper for this one
            write_file("pdf/$filename.pdf", $dompdf->output());
        }
    }
    
    ?>
  5. Create controler mypdf.php
  6. <?php
    
    /**
     * @property CI_DB_active_record $db
     */
    class mypdf extends MY_Controller {
    
        function mypdf() {
            parent::MY_Controller();
      $this->load->plugins('dompdf_pi');
        }
    
        function index() {
         $html="Hello Word";
      $filename="Mypdf";
            pdf_create($html, $filename, $stream=TRUE, $orientation='portrait');
        }
    
     }
    ?>

1 comments:

Post a Comment