<?php
error_reporting(E_ALL);
ini_set("display_errors", 1); 
set_time_limit(90);
//ini_set('memory_limit', '128M');



include_once ("bo/config/server.inc.php");
//require_once ("bo/include/dompdfbeta/dompdf_config.inc.php");
//require_once ("bo/include/dompdfalfa/dompdf_config.inc.php");


set_include_path(get_include_path() . PATH_SEPARATOR . "./bo");
include_once ("db/dbMini.class.php");
$db=new dbMini;

$r=$_REQUEST['r'];
if (!is_numeric($r))exit;

$linharevista=$db->getLinha("revista", $r);
$titulo=$linharevista['categoria'];
$titulo=str_replace(" ", "_" , $titulo);
$titulo=str_replace("|_", "-_" , $titulo);
$titulo=substr($titulo, 3);

$titulo=str_replace("ç", "c", $titulo);

$nome="teste2w.pdf";
$dirnome="netimages/pdf/" . $nome;

$mydir=dirname(__FILE__);
//$wpath="\"C:\Program Files (x86)/wkhtmltopdf/wkhtmltopdf.exe\"";
$wpath=$mydir . "/inc/wkhtmltopdf";
//$wpath="wkhtmltopdf";
$woptions="--page-size A4 --orientation Landscape  --zoom 1.2 --disable-smart-shrinking";
$html_file=$mydir ."/netimages/html/caritas_" . $titulo . ".html";
$pdf_file=$mydir ."/netimages/pdf/caritas_" . $titulo . ".pdf";

$sql="select * from revista_pagina where idCat=$r order by num_pagina";
$revista_paginas=$db->get($sql);
$tam_revista_paginas=sizeof($revista_paginas);
//print $sql;

$html='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="http://' . $serverbase . '"><link rel="stylesheet" type="text/css" href="'. 'http://'. $serverbase .'pdfs.css"></head><body>';

for ($i=-1; $i<$tam_revista_paginas; $i+=2){
	
	if($i< $tam_revista_paginas-1) $pagbr="pagbreak"; else $pagbr="";
	
	$html.='<div class="pag_deitada clearfix '.$pagbr.'">';
	$html.=pagina($i, "esq");
	$html.=pagina($i+1, "dir");	
	$html.='</div>';
}   

function pagina($i, $lado){
	global $revista_paginas, $serverbase, $tam_revista_paginas;	

	if ($revista_paginas[$i]['background']) {
		$style="style=\"background: url('http://".$serverbase .  $revista_paginas[$i]['background']."') no-repeat; background-size: cover;\"";
	} else {
		$style="";
	} 
	if($i>0 && $i< $tam_revista_paginas) $pagnum=$i+1; else $pagnum="";
	
	if ($lado=="esq") $class1="aesq"; else $class1="adir";
	
	if(@$revista_paginas[$i]){
		$html='<div class="paginapdf '.$lado.'"><div class="pag" id="pag'.$revista_paginas[$i]['num_pagina'].'" '.$style.'>'.$revista_paginas[$i]['texto'].'</div><div class="pagnum '.$class1.'">'.$pagnum.'</div></div>';
	} else $html="";

	return $html;
}

$html.= '</body></html>';
$html=str_ireplace("height=\"99%\"", "", $html );

//echo $html;
file_put_contents($html_file, $html);

print "Gerou $titulo <br>";

print "$wpath $woptions $html_file $pdf_file";

//$ret = shell_exec("$wpath $woptions $html_file $pdf_file");
//print $ret;

print "<br>";
exec("$wpath $woptions $html_file $pdf_file", $reta);
var_dump($reta);
?>