<?php
//Configure the directory where you have the dompdf
require_once "dompdf/autoload.inc.php";
foreach($_GET  as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}  
foreach($_POST as $key => $value){${$key}=trim($value);$_PARAMS[$key]=${$key};}

// use Dompdf\Dompdf; //para incluir el namespace de la librería
use Dompdf\Dompdf;
use Dompdf\Options;

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);

$dompdf = new Dompdf($options);
if(!empty($clave))
{
//      $url_docu='https://'.$empr.'.vcrxol.com/index3.php?scri=info_docu3&prnt=1&mailsend=0&clave='.$clave;
     $url_docu='https://'.$empr.'.vcrxol.com/?scri=info_docu3&prnt=1&mailsend=0&clave='.$clave;
     $html_docu=file_get_contents($url_docu);
     
     $file_html='../'.$empr.'/-docu/'.$clave.'.html';
     $file_pdf='../'.$empr.'/-docu/'.$clave.'.pdf';
     $html_content=file_get_contents($file_html);
}

if(!empty($info))
{
//      $file_html='../'.$empr.'/info/'.$info.'.html';
//      $file_pdf='../'.$empr.'/info/'.$info.'.pdf';
     $file_html='../'.$empr.'/-info/'.$info.'.html';
     $file_pdf='../'.$empr.'/-info/'.$info.'.pdf';
     $html_content=file_get_contents($file_html);
}

if(empty($html_content)){return;}

//Y debes activar esta opción "TRUE"
// $options->set('isRemoteEnabled', TRUE);


$header='';
if($desti>=2)
{
//      $header.='header("Content-Type: text/html; charset=utf-8");';
//      $header.='<!DOCTYPE html>';
//      $header.='<html>';
//      $header.='<meta charset="UTF-8">';

//      $html_content=$header.$html_content;
//      $html_content.='</html>';
}

if($desti==3)
{
     $html_content=str_replace('<a','<span',$html_content);     
     $html_content=str_replace('</a','</span',$html_content);     
}

// $html_content=utf8_encode($html_content);


// valida html
{
//      $html = '<html><body><h1>Hola</h1></body></html>'; // Tu HTML aquí
     $html=$html_content;
     libxml_use_internal_errors(true); // Captura errores de DOM

     $doc = new DOMDocument();
     $doc->loadHTML($html);

     foreach (libxml_get_errors() as $error)
     {
         echo "Error en HTML: " . $error->message . "<br>";
     }
     libxml_clear_errors();
}




// $dompdf->loadHtml('hello world');
$dompdf->loadHtml($html_content);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// Render the HTML as PDF
$dompdf->render();



// $totalPages = $dompdf->get_canvas()->get_page_count();

// echo '====='.$totalPages;

// Output the generated PDF to Browser
if(empty($desti)){$desti=1;}



if($desti==1) // browser
{
     header("Content-type: application/pdf");
     header("Content-Disposition: inline; filename=".$file_pdf);
     echo $dompdf->output();
}     

if($desti==2) // download
{
     // $dompdf->stream($clave, array("Attachment" => false));
//      header('Content-Type: text/html; charset=utf-8');
     if(!empty($clave)){$dompdf->stream($clave);}
     if(!empty($info)){$dompdf->stream($info);}
}     

if($desti==3) // disk
{
//      header('Content-Type: text/html; charset=utf-8');
     $diskfile=$dompdf->output();
     file_put_contents($file_pdf,$diskfile);    
}     


?>


