NaszvadiG
4/17/2014 - 8:35 AM

a.md

<?

class Pdf {
    
    public function __construct() {
        require_once(APPPATH . "helpers/dompdf/dompdf_config.inc.php");
        spl_autoload_register('DOMPDF_autoload');
    }

    function pdf_create($html, $filename, $stream = TRUE) {
        $dompdf = $this->pdf_create_as_output($html, true);
        if ($stream) {
            $dompdf->stream($filename . ".pdf");
        } else {
            $CI = & get_instance();
            $CI->load->helper('file');
            write_file($filename, $dompdf->output());
        }
    }

    function pdf_create_as_output($html, $getObject = false) {
        $dompdf = new DOMPDF();
        $dompdf->set_paper("a4", "portrait");
        $dompdf->load_html($html);
        $dompdf->render();
        return $getObject ? $dompdf : $dompdf->output();
    }

}
<?

class Mail extends Model {

    public function __construct() {
        parent::Model();
        $ci = & get_instance();
        $ci->load->library('mailer');
    }

    static function sent($filename, $to, $subject, $body, $closure = null) {
        try {
            $mail = new PHPMailer(true);
            $mail->IsSMTP();
            $mail->Mailer = "smtp";
            $mail->Host = "ssl://smtp.gmail.com";
            $mail->Port = 465;
            $mail->SMTPAuth = true;
            $mail->Username = "xxxxxxxxx@gmail.com";
            $mail->Password = 'xxxxxxxxx';
            $mail->From = "xxxxxxxxx@gmail.com";
            $mail->FromName = "Xxxxx Xxxxxxxxx XXX";
            $mail->AddAddress($to, $to);
            $mail->Subject = $subject;
            $mail->WordWrap = 50;
            $mail->Body = $body;
            if ($closure != null && is_callable($closure)) $closure($mail, $filename);
            return ($mail->Send()) ? 'true' : 'false';
        } catch (phpmailerException $e) {
            return false;
        } catch (Exception $e) {
            return false;
        }
    }

}
<?

class Sample extends Controller {

    function Sample() {
        parent::Controller();
    }
    
    function get_report() {
        $noindex = $this->input->get("noindex");
        $sent = $this->input->get("sent") == 'true';
        $pdf = $this->input->get("pdf") == 'true';
        $data = 'Xxxxxxx';
        
        $file_pdf = $this->load->view('xxx/xxxxx/xxxxx', array(
            'xx' => $data,
            'xxx' => $noindex,
            'xxxx' => $pdf || $sent
        ), $pdf || $sent);
        
        if ($pdf) {
            $this->load->library('pdf');
            $this->pdf->pdf_create($file_pdf, 'Xxxx');
        }
        
        if ($sent) {
            $this->load->library('pdf');
            $attachment = $this->pdf->pdf_create_as_output($file_pdf);
            Mail::sent('x', 'x@x.com', 'Xx Xxx', 'Xx Xx', function($mail, $filename) use ($attachment) {
                $mail->AddStringAttachment($attachment, "{$filename}.pdf");
            });
        }
    }
}

CodeIgniter - DOMPDF - PHPMailer Example

Description

Generating pdf output using codeigniter and dompdf or and sent it as attachment by phpmailer

How to use

Just read the code

Author

Noval Agung