<?php namespace App\Http\Controllers;

/*use App\Custom\SQLConnection; 
use App\Custom\Misc;
use App\Custom\Mailer;*/
use Request;
use Session;
use DateTime;
use Illuminate\Support\Facades\Input;
use DB;
use stdClass;

// use Illuminate\Session\TokenMismatchException;

class PDFController extends Controller
{

    //Recibe un HTML entero y devuelve el nombre asociado ó carga en ese nombre asociado el html que le pasan.
    public static function saveHTML($html='',$filename=''){
        if($html == ''){
            $html = Request::input('html');
        }
        if($filename == ''){
            $filename = Request::input('filename');
        }
        $path_file = '';
        if($filename ==''){
            $path_file = '/var/www/html/fidelitas2/public/documentos/temp/';
            $random_name = substr(sha1(mt_rand()),17,6).'.html';
            $path_file = $path_file.$random_name;
        }
        $path_file = $path_file.$filename;
        file_put_contents($path_file,utf8_decode($html), FILE_APPEND | LOCK_EX);
        chmod($path_file, 0777);
        return $path_file;
    }

    //Recibe un path y genera el PDF POR GET. ;)
    public static function exportPDF($filename=''){
        if($filename == ''){
            $filename = Request::input('filename');
        }
        $pdf  = str_replace('.html', '.pdf', $filename);
        exec("/usr/local/bin/wkhtmltopdf -d 300 -L 10 -R 10 -T 10 -B 10 -O landscape --header-font-size 8 --header-right ' Pag. [page] de [toPage]'  --viewport-size 1920x1080 --zoom 1 ".$filename." ".$pdf);
        return response()->download($pdf);
    }
}