00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Document_Export
00029 {
00030
00031
00032
00033
00034
00035 function __construct()
00036 {
00037
00038
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
00050
00051
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
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
00100 }
00101
00102
00103
00104
00105
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
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
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
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
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
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
00171 $output = $this->store_convert . '/stamp_' . $file_pdf;
00172
00173
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
00189 $this->move_file($output, 'stamp_' . $file_pdf);
00190 }
00191 $this->concatenate_pdf();
00192 ob_clean();
00193 $this->send_pdf();
00194
00195
00196 $this->clean_folder();
00197 var_dump($this->feedback);
00198
00199 }
00200
00201
00202
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 }