noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
Document_Export Class Reference

Export DOCUMENT from Analytic accountancy, can transform into PDF and add a stamp on each pages. More...

+ Collaboration diagram for Document_Export:

Public Member Functions

 __construct ()
 create 2 temporary folders, store_pdf and store_convert, initialize an array feedback containing messages
 
 check_file ()
 check that the files are installed throw a exception if one is missing
 
 clean_folder ()
 remove folder and its content
 
 concatenate_pdf ()
 concatenate all PDF into a single one and save it into the store_pdf folder.
 
 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.
 
 export_receipt ($p_jrn_id, Progress_Bar $progress, $step)
 export a file (
 
 make_zip ()
 Make a zip file.
 
 move_file ($p_source, $p_target)
 copy the file
 
 reorder_array ($p_array)
 Order the array with the date.
 
 send_pdf ()
 send the resulting PDF to the browser
 
 send_zip ()
 send the resulting PDF to the browser
 

Data Fields

 $feedback
 
 $progress
 
 $store_convert
 
 $store_pdf
 

Detailed Description

Export DOCUMENT from Analytic accountancy, can transform into PDF and add a stamp on each pages.

It depends on PDFTK and CONVERT_GIF_PDF

Definition at line 27 of file document_export.class.php.

Constructor & Destructor Documentation

◆ __construct()

Document_Export::__construct ( )

create 2 temporary folders, store_pdf and store_convert, initialize an array feedback containing messages

Definition at line 38 of file document_export.class.php.

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 }

Member Function Documentation

◆ check_file()

Document_Export::check_file ( )

check that the files are installed throw a exception if one is missing

Definition at line 308 of file document_export.class.php.

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 }
_("actif, passif,charge,...")
$ex
const PDF2PS
Definition constant.php:291
const PS2PDF
Definition constant.php:300

References $ex, _, PDF2PS, and PS2PDF.

Referenced by concatenate_pdf(), export_all(), and move_file().

◆ clean_folder()

Document_Export::clean_folder ( )

remove folder and its content

Definition at line 153 of file document_export.class.php.

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 }

References $i.

◆ concatenate_pdf()

Document_Export::concatenate_pdf ( )

concatenate all PDF into a single one and save it into the store_pdf folder.

If an error occurs then it is added to feedback

Definition at line 59 of file document_export.class.php.

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 }
check_file()
check that the files are installed throw a exception if one is missing

References $status, and check_file().

Referenced by export_all().

+ Here is the call graph for this function:

◆ export_all()

Document_Export::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.

If an error occurs then $this->feedback won't be empty

Parameters
$p_arraycontents all the jr_id
Progress_Bar$progressis the progress bar
int$p_separate1 everything in a single PDF or a ZIP with all PDF
int$reconcilied_operation1 with receipt of reconcilied operation 2 without them

Definition at line 179 of file document_export.class.php.

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 }
$op
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
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.

References $cn, $op, $order, $output, $p_array, $progress, $reconcilied_document, $status, $step, $value, _, check_file(), concatenate_pdf(), export_receipt(), make_zip(), move_file(), reorder_array(), send_pdf(), send_zip(), and Progress_Bar\set_value().

+ Here is the call graph for this function:

◆ export_receipt()

Document_Export::export_receipt ( $p_jrn_id,
Progress_Bar $progress,
$step )

export a file (

Parameters
type$p_jrn_id
$progress
Returns
string

Definition at line 330 of file document_export.class.php.

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 }
noalyss_str_replace($search, $replace, $string)
clean_filename($p_filename)
sanitize the filename remove character which could be a problem,

References $cn, $ext, $file, $img, $output, $progress, $receipt, $status, $step, _, clean_filename(), noalyss_str_replace(), PDF2PS, and PS2PDF.

Referenced by export_all().

+ Here is the call graph for this function:

◆ make_zip()

Document_Export::make_zip ( )

Make a zip file.

Definition at line 89 of file document_export.class.php.

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 }
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...

References $res, and record_log().

Referenced by export_all().

+ Here is the call graph for this function:

◆ move_file()

Document_Export::move_file ( $p_source,
$p_target )

copy the file

Parameters
$p_source
$target
Exceptions
Exception

Definition at line 116 of file document_export.class.php.

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 }

References $i, $target, and check_file().

Referenced by export_all().

+ Here is the call graph for this function:

◆ reorder_array()

Document_Export::reorder_array ( $p_array)

Order the array with the date.

Parameters
array$p_arrayarray of jrn.jr_id

Definition at line 465 of file document_export.class.php.

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 }

References $array, $cn, and $p_array.

Referenced by export_all().

◆ send_pdf()

Document_Export::send_pdf ( )

send the resulting PDF to the browser

Definition at line 131 of file document_export.class.php.

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 }

Referenced by export_all().

◆ send_zip()

Document_Export::send_zip ( )

send the resulting PDF to the browser

Definition at line 142 of file document_export.class.php.

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 }

Referenced by export_all().

Field Documentation

◆ $feedback

Document_Export::$feedback

contains feedback

Definition at line 29 of file document_export.class.php.

◆ $progress

Document_Export::$progress

contain value for progressbar

Definition at line 32 of file document_export.class.php.

Referenced by export_all(), and export_receipt().

◆ $store_convert

Document_Export::$store_convert

temporary folder for files

Definition at line 30 of file document_export.class.php.

◆ $store_pdf

Document_Export::$store_pdf

temporary folder for PDF

Definition at line 31 of file document_export.class.php.


The documentation for this class was generated from the following file: