<?php defined('BASEPATH') OR exit('No direct script access allowed');



include_once APPPATH.'/vendor/dompdf/autoload.inc.php';
 
 use Dompdf\Dompdf as Dompdf;
class myPDF 
{
		  
		  public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait")
		  {
			 
				$dompdf = new Dompdf(array('enable_remote' => true,'DOMPDF_ENABLE_PHP'=>true,'DOMPDF_UNICODE_ENABLED' => true));
				$dompdf->load_html($html);
				$dompdf->set_paper($paper, $orientation);
				$dompdf->set_option("isPhpEnabled", true);
				$dompdf->set_option("enable_font_subsetting", true);
				$dompdf->render();
				
				$canvas = $dompdf->get_canvas();
				$total = $dompdf->get_canvas()->get_page_count();
				
				$font = null;
			
				//$canvas->page_text(515,760, "Page {PAGE_NUM} of {PAGE_COUNT}", $font,8, array(0,0,0));
				//echo "{PAGE_NUM}";
				// if($total == "{PAGE_NUM}")
				// {
				// 	echo "{PAGE_NUM}";
				// 	echo $total.'TOT';
				// 	$canvas->page_text(515,700, "Page {PAGE_COUNT} of {PAGE_COUNT}", $font,8, array(0,0,0));
				// }
				//$canvas->get_cpdf()->setEncryption('userpass', 'ownerpass', array('print', 'copy'));
				//$canvas->get_cpdf()->setEncryption('userpass');

				if ($stream) {
					$dompdf->stream($filename.".pdf", array("Attachment" => 0));
				} else {
					return $dompdf->output();
					//$output_file2 = APPPATH."/docs/sample.pdf";	
					//echo file_put_contents($output_file2, $dompdf->output());
					// $CI =& get_instance();
					// $CI->load->helper('file');
					// write_file(APPPATH."/docs/sample.pdf",$dompdf->output());
				}
		  }
}


?>