1) Get data from DB into variable $data
Code:
$data = array("A" => "green", "B" => "brown", "B" => "blue");
problems: none
2) Assign this data to Smarty:
Code:
$smarty->assign('data', $data);
problems:
1 - this works without problems until variable "$data" have no special (for latex) chars like # $ % .. which need to be replaced by \# \$ \% -this is needed for work of pdflatex with no crashes - it looks like it is trivial problem but it is not (for me) because if table "$data" has very complicated and unknown structure then replacing special chars in every its element can be problem.
solution:
1- needed :) very much !
3) Run the Smarty:
Code:
$contents = $smarty->fetch('main.tex');
where:
"$contents" - contents of tex file which later will be saved to "file.tex"
"main.tex" - smarty template
problems:
1 - in Smarty alike as in Latex there are this same special chars !!! : "}" and "{"
solution:
1 - in template for char "{" I use "<" and for char "}" I use ">" - this is very uncomfortably !! and makes template code very unclear
4) Save to file "file.tex" variable "$contents" (in this point I use simplification - you should use temporary and unique file name - this is needed for appropriate work of pdflatex)
5) Run pdflatex
Code:
exec('
TEXMFOUTPUT='.$temp_dir.' &&
export TEXMFOUTPUT &&
TEXINPUTS=:'.$temp_dir.' &&
export TEXINPUTS &&
'.$pdflatex_path.' --interaction=batchmode '.$latex_file_path);
where:
"$temp_dir" - temporary directory with input .tex file and output .pdf file (and few other generated by pdflatex), this directory is unaccessible from web and writable/readable for server apache user
"$pdflatex_path" - path to the executable programm "pdflatex"
"$latex_file_path" - path to the .tex file - must be in "$temp_dir"
problem:
1 - this is not working in safe mode :( -
2 - "pdflatex" has no option to to set input and output directory
solution:
1- needed :) very much !
2 - I guess that there is way to change source code of pdflatex and add additional options to set output and input directory but if someone else would like to use this method of generating .pdf will need to have this changed source code of "pdflatex" :( - so only authors of "pdflatex" can change it in new version so all people will have this additional oprions
Some explanations:
I put input .tex file "file.tex" and output .pdf file into "$temp_dir" - this directory in unaccessible from web - very important !!! then outpu .pdf file is readed into webbrowser by using:
Code:
header('Content-type: application/pdf');
header('Content-Length: '.filesize($unique.'.pdf'));
readfile($unique.".pdf");
this is made to protect from reading .pdf file by other webusers (hint: use as "$unique" session_id of user)
Option "--interaction=batchmode" is absolutely needed ! - without it in most cases we will get no output .pdf file, because if "pdflatex" detect even smalles problems (this is often) will stop.
While debuging don't remove files from "$temp_dir" and if there is no output .pdf file (or the file is damaged) read carefully .log and .tex files.
After debuging add removing all old files from "$temp_dir" (there will be .log, .tex, .pdf, .aux and other)
problem:
There is additionally BIG problem if we use pictures in .pdf file - picture file need to be puted into "$temp_dir" and coverted into suitable format for latex -best is .jpg. For converting I'm using "convert" from ImageMagick but there are sometimes problems - "convert" hangs when picture file has resolution [DPI] set to 0 or 1. Normaly this is not happening.
solution:
help me
Code:
$cmd = $convert_path." -quality 100 -resample 72x72 -geometry '".$max_x."x".$max_y.">'"
." "$file_path
." ".$temp_dir.session_id().".".jpg;
where:
"$convert_path" - path to executable "convert"
"$max_x" - maximal horizontal size of picture (if is smaller there is no conversion)
"$max_y" - maximal vertical size of picture (if is smaller there is no conversion)
"$file_path" - path where oryginal file is stored
This conversion keeps proportion of picture.