<?php
//xePDF, version 1.0 - 11 February 2011
//this package use xePersian Package and texLive,
//before run this script please install it on your system.
//class writed by Meysam GanJi [hide@address.com]
//and published under GPL3.0,
//Hope that this became useful for you :)
class xePDF
{
private $fileN;
private $tableData;
private $currDir;
private $package;
private $tableFont;
public function xePDF($lan,$dataGet,$font)
{
$this->tableData=$dataGet;
$this->fileN=time();
$this->currDir=getcwd();
$this->command="";
$this->package=$lan;
$this->tableFont=$font;
}//Constructor
private function makeColumns($arr)
{
$columns="";
foreach($arr as $key)
{
foreach($key as $row)
$columns.="c ";
break;
}
return $columns;
}
private function makeTableData($arr)
{
$latexLine=array();
$count=0;
foreach($arr as $dim1)
{
$latexLine[$count]="";
foreach($dim1 as $key)
{
$latexLine[$count].=$key." & ";
}
$latexLine[$count]=substr_replace($latexLine[$count],"",strlen($latexLine[$count])-3);
$count++;
}
for($i=0;$i<count($latexLine);$i++)
$latexLine[$i].=" \\\\ ";
$latexLine[0].=" [0.5ex] \hline ";
$latexLine[count($latexLine)-1].=" [1ex] ";
$tex="";
foreach($latexLine as $line)
$tex.=$line;
return $tex;
}//makeLatexLine
private function packageSelect()
{
//if you use another language package of LaTeX, only
//change persian and xePersian with your language and package :)
if($this->package=="persian")
{
return "\usepackage{"."xepersian"."} \settextfont{".$this->tableFont."}";
}
else
{
return "";
}
}
private function makeTable($arr)
{
$tables="";
foreach($arr as $key)
{
$tables.="\begin{table}[ht]
\caption{".$key['title']."}
\centering
\begin{tabular}{".$this->makeColumns($key['data'])."}
\hline\hline
".$this->makeTableData($key['data'])."
\hline
\end{tabular}
\label{table:nonlin}
\end{table} ";
}
return $tables;
}
private function makeTex()
{
if(!($fp=@fopen($this->fileN.".tex","a+")))
{
die("<span style=\"color:#f00;font-weight:bold;\">Can't write The file!<br />Please cheak the temprature folder permission! :|</span>");
} else
{
$latexCode="\documentclass{article}"
.$this->packageSelect()."\begin{document}"
.$this->maketable($this->tableData).
"\end{document}";
fputs($fp,$latexCode);
fclose($fp);
}
}//makeTex
private function makePDF()
{
chdir("temp");
$this->makeTex();
$command="xelatex --interaction=nonstopmode ".$this->fileN.".tex";
exec($command);
}//makePDF
private function cleanF()
{
$this->makePDF();
chdir($this->currDir);
unlink("temp/".$this->fileN.".tex");
unlink("temp/".$this->fileN.".aux");
unlink("temp/".$this->fileN.".log");
}//cleanF
public function downloadPDF()
{
$this->cleanF();
return "temp/".$this->fileN;
}//showPDF
}//Class
?>