noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_document_export.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /**
00023  * @brief Export DOCUMENT from Analytic accountancy, can transform into PDF
00024  * and add a stamp on each pages
00025  * 
00026  * It depends on PDFTK and CONVERT_GIF_PDF
00027  */
00028 class Document_Export
00029 {
00030     /**
00031      *@brief create 2 temporary folders, store_pdf and store_convert, initialize
00032      * an array feedback containing messages
00033      * 
00034      */
00035     function __construct()
00036     {
00037         // Create 2 temporary folders   1. convert to PDF + stamp
00038         //                              2. store result
00039         $this->feedback = array();
00040         $this->store_convert = tempnam($_ENV['TMP'], 'convert_');
00041         $this->store_pdf = tempnam($_ENV['TMP'], 'pdf_');
00042         unlink($this->store_convert);
00043         unlink($this->store_pdf);
00044         umask(0);
00045         mkdir($this->store_convert);
00046         mkdir($this->store_pdf);
00047     }
00048     /**
00049      * @brief concatenate all PDF into a single one and save it into the
00050      * store_pdf folder.
00051      * If an error occurs then it is added to feedback
00052      */
00053     function concatenate_pdf()
00054     {
00055         try
00056         {
00057             $this->check_file();
00058             $stmt=PDFTK." ".$this->store_pdf.'/stamp_*pdf  output '.$this->store_pdf.'/result.pdf';
00059             $status=0;
00060             echo $stmt;
00061             passthru($stmt, $status);
00062 
00063             if ($status<>0)
00064             {
00065                 $cnt_feedback=count($this->feedback);
00066                 $this->feedback[$cnt_feedback]['file']='result.pdf';
00067                 $this->feedback[$cnt_feedback]['message']=' cannot concatenate PDF';
00068                 $this->feedback[$cnt_feedback]['error']=$status;
00069             }
00070         }
00071         catch (Exception $exc)
00072         {
00073             $cnt_feedback=count($this->feedback);
00074             $this->feedback[$cnt_feedback]['file']=' ';
00075             $this->feedback[$cnt_feedback]['message']=$exc->getMessage();
00076             $this->feedback[$cnt_feedback]['error']=0;
00077         }
00078     }
00079 
00080     function move_file($p_source, $target)
00081     {
00082         $this->check_file();
00083         copy($p_source, $this->store_pdf . '/' . $target);
00084     }
00085     /**
00086      * @brief send the resulting PDF to the browser
00087      */
00088     function send_pdf()
00089     {
00090         header('Content-Type: application/x-download');
00091         header('Content-Disposition: attachment; filename="result.pdf"');
00092         header('Cache-Control: private, max-age=0, must-revalidate');
00093         header('Pragma: public');
00094         echo file_get_contents($this->store_pdf . '/result.pdf');
00095     }
00096 
00097     function clean_folder()
00098     {
00099         //unlink($this->store_convert . '/*.pdf');
00100     }
00101 
00102     /**
00103      * @brief export all the pieces in PDF and transform them into a PDF with
00104      * a stamp. If an error occurs then $this->feedback won't be empty
00105      * @param $p_array contents all the jr_id
00106      */
00107     function export_all($p_array)
00108     {
00109         $this->check_file();
00110         ob_start();
00111         var_dump($p_array);
00112         $cnt_feedback=0;
00113         global $cn;
00114 
00115         $cn->start();
00116         foreach ($p_array as $value)
00117         {
00118             // For each file save it into the temp folder,
00119             $file = $cn->get_array('select jr_pj,jr_pj_name,jr_pj_number,jr_pj_type from jrn '
00120                     . ' where jr_id=$1', array($value));
00121             if ($file[0]['jr_pj'] == '')
00122                 continue;
00123 
00124             $cn->lo_export($file[0]['jr_pj'], $this->store_convert . '/' . $file[0]['jr_pj_name']);
00125 
00126             // Convert this file into PDF 
00127             if ($file[0]['jr_pj_type'] != 'application/pdf')
00128             {
00129                 $status = 0;
00130                 $arg=" ".escapeshellarg($this->store_convert.DIRECTORY_SEPARATOR.$file[0]['jr_pj_name']);
00131                 echo "arg = [".$arg."]";
00132                 passthru(OFFICE . " " . $arg , $status);
00133                 if ($status <> 0)
00134                 {
00135                     $this->feedback[$cnt_feedback]['file'] = $file[0]['jr_pj_name'];
00136                     $this->feedback[$cnt_feedback]['message'] = ' cannot convert to PDF';
00137                     $this->feedback[$cnt_feedback]['error'] = $status;
00138                     $cnt_feedback++;
00139                     continue;
00140                 }
00141             }
00142 
00143             // Create a image with the stamp + formula
00144             $img = imagecreatefromgif(__DIR__ . '/template/template.gif');
00145             $font = imagecolorallocatealpha($img, 100, 100, 100, 110);
00146             imagettftext($img, 40, 25, 500, 1000, $font, __DIR__ . '/tfpdf/font/unifont/DejaVuSans.ttf', _("Copie certifiée conforme à l'original"));
00147             imagettftext($img, 40, 25, 550, 1100, $font, __DIR__ . '/tfpdf/font/unifont/DejaVuSans.ttf', $file[0]['jr_pj_number']);
00148             imagettftext($img, 40, 25, 600, 1200, $font, __DIR__ . '/tfpdf/font/unifont/DejaVuSans.ttf', $file[0]['jr_pj_name']);
00149             imagegif($img, $this->store_convert . '/' . 'stamp.gif');
00150 
00151             // transform gif file to pdf with convert tool
00152             $stmt = CONVERT_GIF_PDF . " " . escapeshellarg($this->store_convert . '/' . 'stamp.gif') . " " . escapeshellarg($this->store_convert . '/stamp.pdf');
00153             passthru($stmt, $status);
00154 
00155             if ($status <> 0)
00156             {
00157                 $this->feedback[$cnt_feedback]['file'] = 'stamp.pdf';
00158                 $this->feedback[$cnt_feedback]['message'] = ' cannot convert to PDF';
00159                 $this->feedback[$cnt_feedback]['error'] = $status;
00160                 $cnt_feedback++;
00161                 continue;
00162             }
00163 
00164             // 
00165             // remove extension
00166             $ext = strrpos($file[0]['jr_pj_name'], ".");
00167             $file_pdf = substr($file[0]['jr_pj_name'], 0, $ext);
00168             $file_pdf .=".pdf";
00169 
00170             // output
00171             $output = $this->store_convert . '/stamp_' . $file_pdf;
00172 
00173             // Concatenate stamp + file
00174             $stmt = PDFTK . " " . escapeshellarg($this->store_convert . '/' . $file_pdf) . ' stamp ' . $this->store_convert .
00175                     '/stamp.pdf output ' . $output;
00176             
00177             passthru($stmt, $status);
00178             echo $stmt;
00179             if ($status <> 0)
00180             {
00181 
00182                 $this->feedback[$cnt_feedback]['file'] = $file_pdf;
00183                 $this->feedback[$cnt_feedback]['message'] = _(' ne peut pas convertir en PDF');
00184                 $this->feedback[$cnt_feedback]['error'] = $status;
00185                 $cnt_feedback++;
00186                 continue;
00187             }
00188             // Move the PDF into another temp directory 
00189             $this->move_file($output, 'stamp_' . $file_pdf);
00190         }
00191         $this->concatenate_pdf();
00192         ob_clean();
00193         $this->send_pdf();
00194 
00195         // remove files from "conversion folder"
00196         $this->clean_folder();
00197         var_dump($this->feedback);
00198         // concatenate all pdf into one
00199     }
00200    /**
00201     * @brief check that the files are installed
00202     * throw a exception if one is missing
00203     */
00204     function check_file()
00205     {
00206         try 
00207         {
00208             if (CONVERT_GIF_PDF == 'NO')    throw new Exception(_("CONVERT_GIF_PDF n'est pas installé"));
00209             if (PDFTK          == 'NO')     throw new Exception(_("TKPDF n'est pas installé"));
00210         } catch (Exception $ex) 
00211         {
00212             throw ($ex);
00213         }
00214     }
00215 }
 All Data Structures Namespaces Files Functions Variables Enumerations