00001 <?php
00025 class pdfService
00026 {
00032 public function __construct()
00033 {
00034 }
00035
00043 public function renderView($viewName)
00044 {
00045 $viewObj = BizSystem::getObject($viewName);
00046 if($viewObj)
00047 {
00048 $viewObj->setConsoleOutput(false);
00049 $sHTML = $viewObj->render();
00050
00051
00052 $domPdf = new DOMPDF();
00053 $domPdf->load_html($sHTML);
00054
00055 $domPdf->render();
00056 $this->output($domPdf);
00057
00058 }
00059 }
00060
00061
00067 public function output($domPdf)
00068 {
00069
00070 $tmpDir = APP_HOME."/tmpfiles";
00071
00072 $this->cleanFiles($tmpDir, 100);
00073
00074 $tmpFile = tempnam($tmpDir,'tmp');
00075 $fileName = $tmpFile.'.pdf';
00076 $fileName = str_replace("\\","/",$fileName);
00077 unlink($tmpFile);
00078
00079 $pdfText = $domPdf->output();
00080 $fileHandle = fopen($fileName, 'w') or die("can't open pdf file to write");
00081 fwrite($fileHandle, $pdfText) or die("can't write to the pdf file");
00082 fclose($fileHandle);
00083
00084 $path_parts = pathinfo($fileName);
00085 $file_download = "tmpfiles/".$path_parts['basename'];
00086 echo "<HTML><BODY onload=\"window.location.href='../$file_download';\"</BODY></HTML>";
00087 }
00088
00095 public function cleanFiles($dir, $seconds)
00096 {
00097 $currentTime = time();
00098 $dirHandle=opendir($dir);
00099 while($file = readdir($dirHandle))
00100 {
00101 if(substr($file,0,3) == 'tmp' && substr($file,-4) == '.pdf')
00102 {
00103 $path = $dir.'/'.$file;
00104 if($currentTime - filemtime($path) > $seconds)
00105 unlink($path);
00106 }
00107 }
00108 closedir($dirHandle);
00109 }
00110 }
00111 ?>