76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
|
|
<?php
|
||
|
|
/*
|
||
|
|
* add mz 2014-12-30
|
||
|
|
* Class to create one pdf file from many documents
|
||
|
|
*/
|
||
|
|
class EcmMultiPdf {
|
||
|
|
const TEMP_DIR = "temp/EcmMultiPdf/";
|
||
|
|
const MY_PATH = "include/ECM/EcmMultiPdf/";
|
||
|
|
private $outputfile;
|
||
|
|
private $documents; // is two-dimensions array, $documents[i] = array('module'=>'moduleName', 'record'=>'record_id')
|
||
|
|
private $status;
|
||
|
|
private $count;
|
||
|
|
private $temp_files; // array with temponary file names
|
||
|
|
private $s; // class strings
|
||
|
|
function __construct($documents, $outputfile = 'multi.pdf') {
|
||
|
|
$this->documents = $documents;
|
||
|
|
$this->outputfile = strtolower ( substr ( $outputfile, - 4 ) ) == '.pdf' ? strtolower ( substr ( $outputfile, 0, - 4 ) ) .'_'. create_guid () . '.pdf' : $outputfile .'_'. create_guid () . '.pdf';
|
||
|
|
$this->count = sizeof ( $this->documents );
|
||
|
|
$this->temp_files = array ();
|
||
|
|
// create temp dir if not exists
|
||
|
|
if (! is_dir ( self::TEMP_DIR ))
|
||
|
|
mkdir ( self::TEMP_DIR );
|
||
|
|
}
|
||
|
|
/*
|
||
|
|
* Function creates pdf
|
||
|
|
* return:
|
||
|
|
* file path on succes
|
||
|
|
* -1: prepareDosc error
|
||
|
|
* -2: mergeFiles error
|
||
|
|
*/
|
||
|
|
public function process() {
|
||
|
|
if (! $this->prepareDocs ())
|
||
|
|
return - 1;
|
||
|
|
if (! $this->mergeFiles ())
|
||
|
|
return - 2;
|
||
|
|
$this->deleteTempFiles ();
|
||
|
|
return self::TEMP_DIR . $this->outputfile;
|
||
|
|
}
|
||
|
|
private function prepareDocs() {
|
||
|
|
if (! is_array ( $this->documents ) || $this->count == 0)
|
||
|
|
return false;
|
||
|
|
|
||
|
|
for($i = 0; $i < $this->count; $i ++) {
|
||
|
|
$doc = ( array ) $this->documents [$i]; // cast from object to array when it's axaj call
|
||
|
|
if (! is_array ( $doc ) || sizeof ( $doc ) != 2 || ! $doc ['module'] || ! $doc ['record'])
|
||
|
|
return false;
|
||
|
|
if (! file_exists ( 'modules/' . $doc ['module'] . '/createPDF.php' ))
|
||
|
|
return false;
|
||
|
|
include_once 'modules/' . $doc ['module'] . '/createPDF.php';
|
||
|
|
$generator = 'create' . substr ( $doc ['module'], 0, - 1 ) . 'Pdf';
|
||
|
|
$filename = basename ( $generator ( $doc ['record'], 'MULTIPDF' ) );
|
||
|
|
$this->temp_files [$i] = $filename;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
private function mergeFiles() {
|
||
|
|
include_once self::MY_PATH . 'PDFMerger.php';
|
||
|
|
$merger = new PDFMerger ();
|
||
|
|
$errors = array ();
|
||
|
|
for($i = 0; $i < $this->count; $i ++)
|
||
|
|
if (file_exists ( self::TEMP_DIR . $this->temp_files [$i] ))
|
||
|
|
$merger->addPDF ( self::TEMP_DIR . $this->temp_files [$i], 'all' );
|
||
|
|
else
|
||
|
|
return -2;
|
||
|
|
if (!file_put_contents ( self::TEMP_DIR . $this->outputfile, $merger->merge ( 'string', $this->outputfile ) ));
|
||
|
|
return -2;
|
||
|
|
return true; // file is ready
|
||
|
|
}
|
||
|
|
private function deleteTempFiles() {
|
||
|
|
if (sizeof ( $this->temp_files ) == 0)
|
||
|
|
return;
|
||
|
|
foreach ( $this->temp_files as $f )
|
||
|
|
unlink ( self::TEMP_DIR . $f );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|