Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unlink tmp file #20

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
4 changes: 2 additions & 2 deletions src/DocxMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +34,7 @@ public function merge( $docxFilesArray, $outDocxFilePath ) {

$docx = new Docx( $outDocxFilePath );
for( $i=1; $i<count( $docxFilesArray ); $i++ ) {
$docx->addFile( $docxFilesArray[$i], "part".$i.".docx", "rId10".$i );
$docx->addFile( $docxFilesArray[$i], "part".$i.".docx", "rId10".$i, $addPageBreak);
}

$docx->flush();
Expand Down
15 changes: 10 additions & 5 deletions src/DocxMerge/Docx.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand All @@ -71,8 +71,10 @@ private function addReference( $zipName, $refID ) {
$this->docxRels = substr_replace($this->docxRels, $relXmlString, $p, 0);
}

private function addAltChunk( $refID ) {
$xmlItem = '<w:altChunk r:id="'.$refID.'"/>';
private function addAltChunk( $refID, $addPageBreak ) {
$pagebreak = $addPageBreak ? '<w:p><w:r><w:br w:type="page" /></w:r></w:p>' : '';

$xmlItem = $pagebreak.'<w:altChunk r:id="'.$refID.'"/>';

$p = strpos($this->docxDocument, '</w:body>');
$this->docxDocument = substr_replace($this->docxDocument, $xmlItem, $p, 0);
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 26 additions & 1 deletion src/libraries/TbsZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1000,4 +1025,4 @@ function _EstimateNewArchSize($Optim=true) {

}

}
}