noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
document_export.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21/**
22 * @brief Export DOCUMENT from Analytic accountancy, can transform into PDF
23 * and add a stamp on each pages
24 *
25 * It depends on PDFTK and CONVERT_GIF_PDF
26 */
28{
29 var $feedback; /*!< contains feedback */
30 var $store_convert; /*!< temporary folder for files*/
31 var $store_pdf; /*!< temporary folder for PDF */
32 var $progress; /*!< contain value for progressbar*/
33 /**
34 *@brief create 2 temporary folders, store_pdf and store_convert, initialize
35 * an array feedback containing messages
36 *
37 */
38 function __construct()
39 {
40 // Create 2 temporary folders 1. convert to PDF + stamp
41 // 2. store result
42 $this->feedback = array();
43 $this->store_convert = tempnam($_ENV['TMP'], 'convert_');
44 $this->store_pdf = tempnam($_ENV['TMP'], 'pdf_');
45 unlink($this->store_convert);
46 unlink($this->store_pdf);
47 $this->progress=NULL;
48 umask(0);
49 if ( mkdir($this->store_convert) == FALSE )
50 throw new Exception(sprintf("ERR DE50-Create %s failed",$this->store_onvert));
51 if ( mkdir($this->store_pdf)== FALSE )
52 throw new Exception(sprintf("ERR DE52 Create %s failed",$this->store_pdf));
53 }
54 /**
55 * @brief concatenate all PDF into a single one and save it into the
56 * store_pdf folder.
57 * If an error occurs then it is added to feedback
58 */
59 function concatenate_pdf()
60 {
61 try
62 {
63 $this->check_file();
64 $stmt=PDFTK." ".$this->store_pdf.'/*pdf output '.$this->store_pdf.'/result.pdf';
65 $status=0;
66 echo $stmt;
67 passthru($stmt, $status);
68
69 if ($status<>0)
70 {
71 $cnt_feedback=count($this->feedback);
72 $this->feedback[$cnt_feedback]['file']='result.pdf';
73 $this->feedback[$cnt_feedback]['message']='DE73 cannot concatenate PDF';
74 $this->feedback[$cnt_feedback]['error']=$status;
75 }
76 }
77 catch (Exception $exc)
78 {
79 $cnt_feedback=count($this->feedback);
80 $this->feedback[$cnt_feedback]['file']=' ';
81 $this->feedback[$cnt_feedback]['message']=$exc->getMessage();
82 $this->feedback[$cnt_feedback]['error']=0;
83 }
84 }
85
86 /**
87 * Make a zip file
88 */
89 function make_zip()
90 {
91 $zip=new Zip_Extended();
92 $res=$zip->open("{$this->store_pdf}/result.zip",ZipArchive::CREATE);
93 if ($res !== true) {
94 error_log("ERR-DE89 cannot create zip file");
95 record_log($this);
96 throw new Exception ("ERR-DE89 cannot recreate zip");
97 }
98 chdir($this->store_pdf);
99 // addGmpn
100 $res=$zip->add_file_pattern($this->store_pdf,"/.*pdf/");
101 if ($res == 0) {
102 error_log("ERR-DE96 aucun fichier trouvé");
103 record_log($this);
104 throw new Exception ( "ERR-DE96 cannot recreate zip");
105 }
106 $zip->close();
107
108 }
109 /**
110 * copy the file
111 * @param $p_source
112 * @param $target
113 * @throws Exception
114 */
115
116 function move_file($p_source, $p_target)
117 {
118 $this->check_file();
119 $i=1;
120 $target=$p_target;
121 // do not overwrite a document already present
122 while (file_exists($target)) {
123 $target = sprintf("%d-%s",$i,$p_target);
124 $i++;
125 }
126 copy($p_source, $this->store_pdf . '/' . $target);
127 }
128 /**
129 * @brief send the resulting PDF to the browser
130 */
131 function send_pdf()
132 {
133 header('Content-Type: application/x-download');
134 header('Content-Disposition: attachment; filename="result.pdf"');
135 header('Cache-Control: private, max-age=0, must-revalidate');
136 header('Pragma: public');
137 echo file_get_contents($this->store_pdf . '/result.pdf');
138 }
139 /**
140 * @brief send the resulting PDF to the browser
141 */
142 function send_zip()
143 {
144 header('Content-Type: application/zip');
145 header('Content-Disposition: attachment; filename="result.zip"');
146 header('Cache-Control: private, max-age=0, must-revalidate');
147 header('Pragma: public');
148 echo file_get_contents($this->store_pdf . '/result.zip');
149 }
150 /**
151 * @brief remove folder and its content
152 */
153 function clean_folder()
154 {
155 $files= scandir($this->store_convert);
156 $nb_file=count($files);
157 for ($i=0;$i < $nb_file;$i++) {
158 if (is_file($this->store_convert."/".$files[$i])) unlink($this->store_convert."/".$files[$i]);
159 }
160 rmdir($this->store_convert);
161 $files= scandir($this->store_pdf);
162 $nb_file=count($files);
163 for ($i=0;$i < $nb_file;$i++) {
164 if (is_file($this->store_pdf."/".$files[$i])) unlink($this->store_pdf."/".$files[$i]);
165 }
166 rmdir($this->store_pdf);
167
168 }
169
170 /**
171 * @brief export all the pieces in PDF and transform them into a PDF with
172 * a stamp. If an error occurs then $this->feedback won't be empty
173 * @param $p_array contents all the jr_id
174 * @param Progress_Bar $progress is the progress bar
175 * @param int $p_separate 1 everything in a single PDF or a ZIP with all PDF
176 * @param int $reconcilied_operation 1 with receipt of reconcilied operation 2 without them
177 *
178 */
180 {
181 $this->progress=$progress;
182
183 $this->check_file();
184 if (count($p_array)==0)
185 return;
186 ob_start();
187 $cnt_feedback=0;
188 global $cn;
189
191 $order=0;
192 // follow progress
193 $step=round(16/count($p_array), 2);
194
195 foreach ($p_array as $value)
196 {
197 $progress->increment($step);
198
199 $output_receipt=$this->export_receipt($value, $progress, $step);
200
201 if ($output_receipt==NULL)
202 {
203 continue;
204 }
205 $output=$output_receipt['output'];
206 $file_pdf=$output_receipt['filepdf'];
207
208 // export also the receipt of reconcilied operation
209 $a_reconcilied_operation=[];
210 if ( $reconcilied_document == 1 ) {
211 $a_reconcilied_operation=$cn->get_array("select jr_id,jra_concerned
212 from jrn_rapt where jra_concerned=$1 or jr_id=$1", [$value]);
213 }
214
215 // for each reconcilied operation , export the receipt and concantenate
216 foreach ($a_reconcilied_operation as $reconcilied_operation)
217 {
218 $op=($reconcilied_operation['jr_id']==$value)?$reconcilied_operation['jra_concerned']:$reconcilied_operation['jr_id'];
219
220 $output_rec=$this->export_receipt($op, $progress, $step);
221 if ($output_rec==NULL)
222 {
223 continue;
224 }
225 // concatenate detail operation with the output
226 $output3=$this->store_convert.'/tmp_operation_'.$file_pdf;
227
228 $stmt=PDFTK." ".$output." ".$output_rec['output'].
229 ' output '.$output3;
230
231 passthru($stmt, $status);
232 if ($status<>0)
233 {
234 $cnt_feedback=count($this->feedback);
235 $this->feedback[$cnt_feedback]['file']=$output3;
236 $this->feedback[$cnt_feedback]['message']=_('Echec ');
237 $this->feedback[$cnt_feedback]['error']=$status;
238 $cnt_feedback++;
239 continue;
240 }
241 unlink($output_rec['output']);
242 rename($output3, $output);
243 }
244 $progress->increment($step);
245
246 // create the pdf with the detail of operation
247 $detail_operation=new PDF_Operation($cn, $value);
248 $detail_operation->export_pdf(array("acc", "anc"));
249
250 // output 2
251 $output2=$this->store_convert.'/operation_'.$file_pdf;
252
253 // concatenate detail operation with the output
254 $stmt=PDFTK." ".$detail_operation->get_pdf_filename()." ".$output.
255 ' output '.$output2;
256
257 $progress->increment($step);
258 passthru($stmt, $status);
259 if ($status<>0)
260 {
261 $cnt_feedback=count($this->feedback);
262 $this->feedback[$cnt_feedback]['file']=$output2;
263 $this->feedback[$cnt_feedback]['message']=_('Echec Ajout detail ');
264 $this->feedback[$cnt_feedback]['error']=$status;
265 $cnt_feedback++;
266 continue;
267 }
268 // remove doc with detail
269 $detail_operation->unlink();
270
271 // overwrite old with new PDF
272 rename($output2, $output);
273
274 // Move the PDF into another temp directory
275 $this->move_file($output, $file_pdf);
276 $order++;
277 }
278
279 $progress->set_value(93);
280 if (DEBUGNOALYSS>1) {
281 tracedebug(date('y-m-d')."-debug.log",$this->feedback,'feedback');
282 }
283 if ($p_separate==1)
284 {
285 // concatenate all pdf into one
286 $this->concatenate_pdf();
287
288 ob_clean();
289 $this->send_pdf();
290 }
291 else
292 {
293 // Put all PDF In a zip file
294 $this->make_zip();
295 ob_clean();
296 $this->send_zip();
297 }
298
299 $progress->set_value(100);
300 // remove files from "conversion folder"
301 // $this->clean_folder();
302 }
303
304 /**
305 * @brief check that the files are installed
306 * throw a exception if one is missing
307 */
308 function check_file()
309 {
310 try
311 {
312 if (CONVERT_GIF_PDF == 'NOT') throw new Exception(_("CONVERT_GIF_PDF n'est pas installé"));
313 if (PDFTK == 'NOT') throw new Exception(_("TKPDF n'est pas installé"));
314 if ( FIX_BROKEN_PDF == 'YES') {
315 if (PS2PDF == 'NOT') throw new Exception(_('PS2PDF non installé'));
316 if (PDF2PS == 'NOT') throw new Exception(_('PDF2PS non installé'));
317 }
318 } catch (Exception $ex)
319 {
320 throw ($ex);
321 }
322 }
323
324 /**
325 * @brief export a file (
326 * @param type $p_jrn_id
327 * @param $progress
328 * @return string
329 */
331 {
332 global $cn;
333 $cnt_feedback=count($this->feedback);
334
335 // For each file save it into the temp folder,
336 $file=$cn->get_array('select jr_pj,jr_pj_name,jr_pj_number,jr_pj_type from jrn '
337 .' where jr_id=$1', array($p_jrn_id));
338
339
340 if ($file[0]['jr_pj']=='')
341 {
342 return null;
343 }
344
345
346 $filename=clean_filename($file[0]['jr_pj_name']);
347 $receipt=clean_filename($file[0]['jr_pj_number']);
349 $filename=$receipt.'-'.$filename;
350
351 $cn->start();
352 $cn->lo_export($file[0]['jr_pj'], $this->store_convert.'/'.$filename);
353 $cn->commit();
354
355 if ( ! file_exists( $this->store_convert.'/'.$filename) ){
356 throw new \Exception("ERR:DE342 Ne peut pas exporter le fichier $filename");
357 }
358
359 // Convert this file into PDF
360 if ($file[0]['jr_pj_type']!='application/pdf')
361 {
362 $status=0;
363 $arg=" ".escapeshellarg($this->store_convert.DIRECTORY_SEPARATOR.$filename);
364 echo "arg = [".$arg."]";
365 passthru(OFFICE." ".$arg, $status);
366 if ($status<>0)
367 {
368 $this->feedback[$cnt_feedback]['file']=$filename;
369 $this->feedback[$cnt_feedback]['message']=' OFFICE cannot convert to PDF';
370 $this->feedback[$cnt_feedback]['error']=$status;
371 return null;
372 }
373 }
374 // Create a image with the stamp + formula
375 $img=imagecreatefromgif(NOALYSS_INCLUDE.'/template/template.gif');
376 $font=imagecolorallocatealpha($img, 100, 100, 100, 110);
377 imagettftext($img, 40, 25, 500, 1000, $font,
378 NOALYSS_INCLUDE.'/tfpdf/font/unifont/DejaVuSans.ttf'
379 , _("Copie certifiee conforme a l'original"));
380 imagettftext($img, 40, 25, 550, 1100, $font,
381 NOALYSS_INCLUDE.'/tfpdf/font/unifont/DejaVuSans.ttf'
382 , $file[0]['jr_pj_number']);
383 imagettftext($img, 40, 25, 600, 1200, $font,
384 NOALYSS_INCLUDE.'/tfpdf/font/unifont/DejaVuSans.ttf'
385 ,$filename);
386 imagegif($img, $this->store_convert.'/'.'stamp.gif');
387
388 // transform gif file to pdf with convert tool
389 $stmt=CONVERT_GIF_PDF." ".escapeshellarg($this->store_convert.'/'.'stamp.gif')." "
390 .escapeshellarg($this->store_convert.'/stamp.pdf');
391 passthru($stmt, $status);
392 if ($status<>0)
393 {
394 $this->feedback[$cnt_feedback]['file']='stamp.pdf';
395 $this->feedback[$cnt_feedback]['message']=' CONVERT_GIF_PDF cannot convert to PDF';
396 $this->feedback[$cnt_feedback]['error']=$status;
397 return null;
398 }
399
400
401 $progress->increment($step);
402 //
403 // remove extension
404 $ext=strrpos($filename, ".");
405 $file_pdf=substr($filename, 0, $ext);
406 $file_pdf.=".pdf";
407
408 //-----------------------------------
409 // Fix broken PDF , actually pdftk can not handle all the PDF
410 if (FIX_BROKEN_PDF=='YES'&&PDF2PS!='NOT'&&PS2PDF!='NOT')
411 {
412
413 $stmpt=PDF2PS." ".escapeshellarg($this->store_convert.'/'.$file_pdf).
414 " ".escapeshellarg($this->store_convert.'/'.$file_pdf.'.ps');
415
416 passthru($stmpt, $status);
417
418 if ($status<>0)
419 {
420 $this->feedback[$cnt_feedback]['file']=$this->store_convert.'/'.$file_pdf;
421 $this->feedback[$cnt_feedback]['message']=' cannot force to PDF';
422 $this->feedback[$cnt_feedback]['error']=$status;
423 $cnt_feedback++;
424 return null;
425 }
426 $stmpt=PS2PDF." ".escapeshellarg($this->store_convert.'/'.$file_pdf.'.ps').
427 " ".escapeshellarg($this->store_convert.'/'.$file_pdf.'.2');
428
429 passthru($stmpt, $status);
430
431 if ($status<>0)
432 {
433 $this->feedback[$cnt_feedback]['file']=$this->store_convert.'/'.$file_pdf;
434 $this->feedback[$cnt_feedback]['message']=' cannot force to PDF';
435 $this->feedback[$cnt_feedback]['error']=$status;
436 $cnt_feedback++;
437 return null;
438 }
439 rename($this->store_convert.'/'.$file_pdf.'.2', $this->store_convert.'/'.$file_pdf);
440 }
441 $progress->increment($step);
442 // output
443 $output=$this->store_convert.'/stamp_'.$file_pdf;
444
445 // Concatenate stamp + file
446 $stmt=PDFTK." ".escapeshellarg($this->store_convert.'/'.$file_pdf)
447 .' stamp '.$this->store_convert.
448 '/stamp.pdf output '.$output;
449
450 passthru($stmt, $status);
451 if ($status<>0)
452 {
453
454 $this->feedback[$cnt_feedback]['file']=$file_pdf;
455 $this->feedback[$cnt_feedback]['message']=_('PDFTK ne peut pas convertir en PDF')." cmd =[$stmt] status [$status]";
456 $this->feedback[$cnt_feedback]['error']=$status;
457 return null;
458 }
459 return array("output"=>$output,"filepdf"=>$file_pdf);
460 }
461 /**
462 * @brief Order the array with the date
463 * @param array $p_array array of jrn.jr_id
464 */
466 {
467 global $cn;
468 if (empty($p_array)) {return array();}
469
470 $list_jrn_id=join(',', $p_array);
471
472 $array=$cn->get_array("select jr_id ,jr_date from jrn where jr_id in ($list_jrn_id) order by jr_date");
473 $array=array_column($array, 'jr_id');
474 return $array;
475 }
476}
noalyss_str_replace($search, $replace, $string)
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
$op
_("actif, passif,charge,...")
$ex
Export DOCUMENT from Analytic accountancy, can transform into PDF and add a stamp on each pages.
send_pdf()
send the resulting PDF to the browser
concatenate_pdf()
concatenate all PDF into a single one and save it into the store_pdf folder.
move_file($p_source, $p_target)
copy the file
__construct()
create 2 temporary folders, store_pdf and store_convert, initialize an array feedback containing mess...
export_receipt($p_jrn_id, Progress_Bar $progress, $step)
export a file (
make_zip()
Make a zip file.
send_zip()
send the resulting PDF to the browser
reorder_array($p_array)
Order the array with the date.
export_all($p_array, Progress_Bar $progress, $p_separate=1, $reconcilied_document=2)
export all the pieces in PDF and transform them into a PDF with a stamp.
clean_folder()
remove folder and its content
check_file()
check that the files are installed throw a exception if one is missing
Detail Operation ACC + ANC , it will use Acc_Operation and Anc_Operation.
Use one db for tracking progress bar value, the task id must be unique and let you follow the progres...
set_value($p_value)
Store the progress value into the db.
extends the Zip object
const PDF2PS
Definition constant.php:291
const PS2PDF
Definition constant.php:300
clean_filename($p_filename)
sanitize the filename remove character which could be a problem,