diff --git a/composer.json b/composer.json index 86a14b6..1b758c4 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { - "name": "krustnic/docx-merge", + "name": "n-osennij/docx-merge", + "version": "1.1", "type": "library", "description": "Simple library for merging multiple MS Word \".docx\" files into one", "keywords": ["docx","merge","placeholder"], diff --git a/src/DocxMerge.php b/src/DocxMerge.php index edf213b..69f77e1 100644 --- a/src/DocxMerge.php +++ b/src/DocxMerge.php @@ -17,7 +17,7 @@ class DocxMerge { * @param $outDocxFilePath * @return int */ - public function merge( $docxFilesArray, $outDocxFilePath ) { + public function merge( $docxFilesArray, $outDocxFilePath, $addPageBreak = false) { if ( count($docxFilesArray) == 0 ) { // No files to merge return -1; @@ -34,7 +34,7 @@ public function merge( $docxFilesArray, $outDocxFilePath ) { $docx = new Docx( $outDocxFilePath ); for( $i=1; $iaddFile( $docxFilesArray[$i], "part".$i.".docx", "rId10".$i ); + $docx->addFile( $docxFilesArray[$i], "part".$i.".docx", "rId10".$i, $addPageBreak); } $docx->flush(); diff --git a/src/DocxMerge/Docx.php b/src/DocxMerge/Docx.php index 8b7f2fd..0ad307a 100644 --- a/src/DocxMerge/Docx.php +++ b/src/DocxMerge/Docx.php @@ -55,12 +55,12 @@ private function writeContent( $content, $zipPath ) { return 0; } - public function addFile( $filePath, $zipName, $refID ) { + public function addFile( $filePath, $zipName, $refID, $addPageBreak = false ) { $content = file_get_contents( $filePath ); $this->docxZip->FileAdd( $zipName, $content ); $this->addReference( $zipName, $refID ); - $this->addAltChunk( $refID ); + $this->addAltChunk( $refID, $addPageBreak ); $this->addContentType( $zipName ); } @@ -71,8 +71,10 @@ private function addReference( $zipName, $refID ) { $this->docxRels = substr_replace($this->docxRels, $relXmlString, $p, 0); } - private function addAltChunk( $refID ) { - $xmlItem = ''; + private function addAltChunk( $refID, $addPageBreak ) { + $pagebreak = $addPageBreak ? '' : ''; + + $xmlItem = $pagebreak.''; $p = strpos($this->docxDocument, ''); $this->docxDocument = substr_replace($this->docxDocument, $xmlItem, $p, 0); @@ -211,7 +213,10 @@ public function flush() { // Replace current file with tempFile content if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - copy( $tempFile, $this->docxPath ); + $status = copy( $tempFile, $this->docxPath ); + if ($status) { + unlink($tempFile); + } } else { rename($tempFile, $this->docxPath); diff --git a/src/libraries/TbsZip.php b/src/libraries/TbsZip.php index ca3d1de..9b48a3f 100644 --- a/src/libraries/TbsZip.php +++ b/src/libraries/TbsZip.php @@ -17,6 +17,31 @@ class TbsZip { + public $Meth8Ok; + public $DisplayError; + public $ArchFile; + public $Error; + + // Compatibility PHP 8.2 + public $ArchHnd; + public $ArchIsNew; + public $CdEndPos; + public $CdPos; + public $CdInfo; + public $ArchIsStream; + public $CdFileLst; + public $CdFileNbr; + public $CdFileByName; + public $VisFileLst; + public $LastReadComp; + public $LastReadIdx; + public $ReplInfo; + public $ReplByPos; + public $AddInfo; + public $OutputMode; + public $OutputHandle; + public $OutputSrc; + function __construct() { $this->Meth8Ok = extension_loaded('zlib'); // check if Zlib extension is available. This is need for compress and uncompress with method 8. $this->DisplayError = true; @@ -1000,4 +1025,4 @@ function _EstimateNewArchSize($Optim=true) { } - } \ No newline at end of file + }