From f7d7aa952e1ae177ba2120ad66263b29998566bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B8=D0=B9?= Date: Thu, 9 Feb 2023 17:53:27 +0300 Subject: [PATCH 1/5] unlink tmp file Delete *.tmp file on windows if creating copy of this file is successful --- src/DocxMerge/Docx.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DocxMerge/Docx.php b/src/DocxMerge/Docx.php index 8b7f2fd..0af35a9 100644 --- a/src/DocxMerge/Docx.php +++ b/src/DocxMerge/Docx.php @@ -211,7 +211,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); From 9acc651b142ab13de46aff64074e61dadc667362 Mon Sep 17 00:00:00 2001 From: n-osennij Date: Fri, 16 Aug 2024 17:50:59 +0300 Subject: [PATCH 2/5] add page brake option --- src/DocxMerge.php | 4 ++-- src/DocxMerge/Docx.php | 10 ++++++---- src/libraries/TbsZip.php | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) 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 0af35a9..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); diff --git a/src/libraries/TbsZip.php b/src/libraries/TbsZip.php index ca3d1de..4c4b8d4 100644 --- a/src/libraries/TbsZip.php +++ b/src/libraries/TbsZip.php @@ -10,10 +10,10 @@ */ namespace DocxMerge\libraries; - define('TBSZIP_DOWNLOAD',1); // download (default) - define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent - define('TBSZIP_FILE',8); // output to file , or add from file - define('TBSZIP_STRING',32); // output to string, or add from string +// define('TBSZIP_DOWNLOAD',1); // download (default) +// define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent +// define('TBSZIP_FILE',8); // output to file , or add from file +// define('TBSZIP_STRING',32); // output to string, or add from string class TbsZip { From 03a9546fa5a07afbe7e38360a3e9548107b91298 Mon Sep 17 00:00:00 2001 From: n-osennij Date: Fri, 16 Aug 2024 18:17:19 +0300 Subject: [PATCH 3/5] update name and version --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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"], From 1e7c3b551e38aa05d9c16314b075aac15ab45f85 Mon Sep 17 00:00:00 2001 From: n-osennij Date: Fri, 16 Aug 2024 18:31:04 +0300 Subject: [PATCH 4/5] fix --- src/libraries/TbsZip.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/TbsZip.php b/src/libraries/TbsZip.php index 4c4b8d4..ca3d1de 100644 --- a/src/libraries/TbsZip.php +++ b/src/libraries/TbsZip.php @@ -10,10 +10,10 @@ */ namespace DocxMerge\libraries; -// define('TBSZIP_DOWNLOAD',1); // download (default) -// define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent -// define('TBSZIP_FILE',8); // output to file , or add from file -// define('TBSZIP_STRING',32); // output to string, or add from string + define('TBSZIP_DOWNLOAD',1); // download (default) + define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent + define('TBSZIP_FILE',8); // output to file , or add from file + define('TBSZIP_STRING',32); // output to string, or add from string class TbsZip { From f8747b00d6498991b856416c4547d77b5d00e051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9E=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B8=D0=B9?= Date: Fri, 16 Aug 2024 20:26:15 +0300 Subject: [PATCH 5/5] Compatibility PHP 8.2 --- src/libraries/TbsZip.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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 + }