<?php

namespace App\Utility;
use Cake\View\ViewBuilder;
use Knp\Snappy\Pdf;
use \Mpdf\Mpdf;


class PdfWriter {

    private $binaryPath;

    public function __construct() {
        $this->binaryPath = PDF_LIB_PATH;
        
    }

    public function createPdfWithConfiguration($data,$file_path) {
        require WWW_ROOT.'epicvelocity/vendor/autoload.php';
        $html = $data['body_html'];
        $cover_html = isset($data['cover_html']) ? $data['cover_html']:"";
        $page_no_format = "";//Page [page] of [topage]
        if(!empty($data['page_no_format'])){
            $page_no_format = $data['page_no_format'];
        }
       
        $snappy = new Pdf();
        $snappy->setBinary($this->binaryPath);
        if(!empty($cover_html)){
            $snappy->setOption('cover',$cover_html);
        }
        if(!empty($data['margin'])){
            $snappy->setOption('margin-left', $data['margin']);
            $snappy->setOption('margin-right', $data['margin']);
            $snappy->setOption('margin-top', $data['margin']);
            $snappy->setOption('margin-bottom', $data['margin']);
        }else{
            if(!empty($data['margin_left'])){
                $snappy->setOption('margin-left', $data['margin_left']);
            }

            if(!empty($data['margin_right'])){
                $snappy->setOption('margin-right', $data['margin_right']);
            }

            if(!empty($data['margin_top'])){
                $snappy->setOption('margin-top', $data['margin_top']);
            }
        
            if(!empty($data['margin_bottom'])){
                $snappy->setOption('margin-bottom', $data['margin_bottom']);
            }
        }
        
        if(!empty($page_no_format)){
            $snappy->setOption('footer-right', $page_no_format);
        }

        if(!empty($data['footer_left'])){
            $snappy->setOption('footer-left', $data['footer_left']);
        }
        
        if(!empty($data['header_right'])){
            $snappy->setOption('header-right', $data['header_right']);
        }
        
        return $snappy->generateFromHtml($html,$file_path,
                array(
                      'encoding' => 'utf-8'
                      
                    )
                );
        
    }
    
    public function write($html,$file_path) {

        $snappy = new Pdf();
        $snappy->setBinary($this->binaryPath);
        $snappy->setOption('margin-left', 5);
        $snappy->setOption('enable-local-file-access', true);
        $snappy->setOption('no-images', false);
        $snappy->setOption('print-media-type', true);
        $snappy->setOption('background', true);
//        $snappy->setOption('footer-right', "Page [page] of [topage]");
//        $html = str_replace('<p>&#160;</p>', '', $html);
//        dd($html);
//        pr($html);exit;
        $snappy->generateFromHtml($html,$file_path,array('encoding' => 'utf-8'));
//        $pdf = $snappy->getOutputFromHtml($html);
////        print $pdf;
//        file_put_contents($file_path, $pdf);
        chmod($file_path, 0664);
//        dd($snappy);
        return $snappy; 
        
    }
    public function pdfcreate($html,$file_path){
        require WWW_ROOT.'epicvelocity/vendor/autoload.php';
        $snappy = new Pdf();
        $snappy->setBinary($this->binaryPath);
        $snappy->setOption('margin-left', 5);
        $snappy->generateFromHtml($html, $file_path, array('encoding' => 'utf-8'));
        $file_url = $file_path;
    }

    public function download($html,$file_path){
        require WWW_ROOT.'epicvelocity/vendor/autoload.php';
        $snappy = new Pdf();
        $snappy->setBinary($this->binaryPath);
        $snappy->setOption('page-size','LETTER');
        $snappy->setOption('encoding', 'UTF-8');
        
        $snappy->generateFromHtml($html, $file_path, array('encoding' => 'utf-8'));
        $file_url = $file_path;
        header('Content-Type: application/octet-stream');
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
        readfile($file_url);
        @unlink($file_path);
        //return $file_path;
    }
    
    
    public function mpdfWrite($html,$file_path,$file_name,$extra = []){
        ini_set("pcre.backtrack_limit", "5000000");

        $html  = mb_convert_encoding($html, 'UTF-8', 'UTF-8');

        $mpdf = new \Mpdf\Mpdf(
            [
                'default_font_size'          => '12px',
                'default_font'               => 'sans-serif',
            ]
        );

        $mpdf->SetProtection(array("print"));
        $mpdf->SetHTMLHeader(''); // Clears any header
        $mpdf->SetHTMLFooter(''); // Clears any footer

        if(!empty($extra['footer'])){
            $mpdf->defaultfooterline = 0;
            $mpdf->setFooter($extra['footer']);
        }
        
//        echo $html;exit;
        $mpdf->WriteHTML($html);
        $mpdf->Output($file_path.$file_name);

    }
    
    public function mpdfdownload($html,$file_path,$file_name, $extra = []){
        ini_set("pcre.backtrack_limit", "5000000");
        include(WWW_ROOT."../vendor/mpdf_latest_vendor\vendor\autoload.php");
        //$html  = mb_convert_encoding($html, 'UTF-8', 'UTF-8');
        $mpdf = new \Mpdf\Mpdf();
        $mpdf->SetProtection(array("print"));
        //$mpdf->SetDisplayMode('fullpage');
        $mpdf->defaultfooterline = 0;
        if(!empty($extra['footer'])){
            $mpdf->setFooter($extra['footer']);
        }
        //echo $html;exit;
        $mpdf->WriteHTML($html);
        //$file_name = "invoice_" . $id . ".pdf"; //file name
        $mpdf->Output($file_name, 'D'); // I=preview, D=download
        @unlink($file_path);
        //return $file_path;
    }
    


}
