diff --git a/DocxMerge.php b/DocxMerge.php deleted file mode 100644 index f312bd3..0000000 --- a/DocxMerge.php +++ /dev/null @@ -1,71 +0,0 @@ -=') and - !spl_autoload_functions() || !in_array('DocxMergeAutoload', spl_autoload_functions())) { - require dirname(__FILE__).DIRECTORY_SEPARATOR.'DocxMerge'.DIRECTORY_SEPARATOR.'DocxMergeAutoload.php'; - } - } - - /** - * Merge files in $docxFilesArray order and - * create new file $outDocxFilePath - * @param $docxFilesArray - * @param $outDocxFilePath - * @return int - */ - public function merge( $docxFilesArray, $outDocxFilePath ) { - if ( count($docxFilesArray) == 0 ) { - // No files to merge - return -1; - } - - if ( substr( $outDocxFilePath, -5 ) != ".docx" ) { - $outDocxFilePath .= ".docx"; - } - - if ( !copy( $docxFilesArray[0], $outDocxFilePath ) ) { - // Cannot create file - return -2; - } - - $docx = new Docx( $outDocxFilePath ); - for( $i=1; $iaddFile( $docxFilesArray[$i], "part".$i.".docx", "rId10".$i ); - } - - $docx->flush(); - - return 0; - } - - public function setValues( $templateFilePath, $outputFilePath, $data ) { - - if ( !file_exists( $templateFilePath ) ) { - return -1; - } - - if ( !copy( $templateFilePath, $outputFilePath ) ) { - // Cannot create output file - return -2; - } - - $docx = new Docx( $outputFilePath ); - $docx->loadHeadersAndFooters(); - foreach( $data as $key => $value ) { - $docx->findAndReplace( "\${".$key."}", $value ); - } - - $docx->flush(); - } - -} \ No newline at end of file diff --git a/DocxMerge/DocxMergeAutoload.php b/DocxMerge/DocxMergeAutoload.php deleted file mode 100644 index 998773e..0000000 --- a/DocxMerge/DocxMergeAutoload.php +++ /dev/null @@ -1,33 +0,0 @@ -=')) { - //SPL autoloading was introduced in PHP 5.1.2 - if (version_compare(PHP_VERSION, '5.3.0', '>=')) { - spl_autoload_register('DocxMergeAutoload', true, true); - } else { - spl_autoload_register('DocxMergeAutoload'); - } -} else { - /** - * Fall back to traditional autoload for old PHP versions - * @param string $classname The name of the class to load - */ - function __autoload($classname) - { - PHPMailerAutoload($classname); - } -} \ No newline at end of file diff --git a/README.md b/README.md index 33fbed2..8116916 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ Simple library for merging multiple MS Word ".docx" files into one Features -------- -+ Create valid docx for MS Office 2007 and above ++ Merge docx files generating valid docx for MS Office 2007 and above ++ Add a page break between merged documents Details ------- @@ -21,7 +22,7 @@ Merge Example $dm->merge( [ "templates/TplPage1.docx", "templates/TplPage2.docx" - ], "/tmp/result.docx" ); + ], "/tmp/result.docx", true ); setValues Example @@ -33,4 +34,4 @@ setValues Example $dm = new DocxMerge(); $dm->setValues( "templates/template.docx", "templates/result.docx", - array( "NAME" => "Sterling", "SURNAME" => "Archer" ) ); \ No newline at end of file + array( "NAME" => "Sterling", "SURNAME" => "Archer" ) ); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..772763a --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name" : "jupitern/docxmerge", + "description": "php docx file merge", + "keywords" : ["docx", "php"], + "homepage" : "https://github.com/jupitern/docxmerge", + "license" : "MIT", + "authors" : [ + { + "name" : "Nuno Chaves", + "email" : "nunochaves@sapo.pt", + "role" : "Developer" + } + ], + "support": { + "source": "https://github.com/jupitern/docxmerge", + "issues": "https://github.com/jupitern/docxmerge/issues" + }, + "require" :{ + "php":">=5.4" + }, + "autoload": { + "psr-4": { + "Jupitern\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + + } + } +} \ No newline at end of file diff --git a/DocxMerge/Docx.php b/src/Docx.php similarity index 90% rename from DocxMerge/Docx.php rename to src/Docx.php index 9c83cca..ba89025 100644 --- a/DocxMerge/Docx.php +++ b/src/Docx.php @@ -5,6 +5,8 @@ * Date: 05.02.14 * Time: 11:59 */ +namespace Jupitern\DocxMerge; + class Docx { @@ -51,12 +53,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 ); } @@ -67,8 +69,9 @@ 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); @@ -82,7 +85,7 @@ private function addContentType( $zipName ) { } public function loadHeadersAndFooters() { - $relsXML = new SimpleXMLElement( $this->docxRels ); + $relsXML = new \SimpleXMLElement( $this->docxRels ); foreach( $relsXML as $rel ) { if ( $rel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" || $rel["Type"] == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" ) { diff --git a/src/DocxMerge.php b/src/DocxMerge.php new file mode 100644 index 0000000..cbef4f5 --- /dev/null +++ b/src/DocxMerge.php @@ -0,0 +1,67 @@ +addFile($docxFilesArray[$i], "part" . $i . ".docx", "rId10" . $i, $addPageBreak); + } + + $docx->flush(); + + return 0; + } + + public function setValues($templateFilePath, $outputFilePath, $data) + { + + if (!file_exists($templateFilePath)) { + return -1; + } + + if (!copy($templateFilePath, $outputFilePath)) { + // Cannot create output file + return -2; + } + + $docx = new Docx($outputFilePath); + $docx->loadHeadersAndFooters(); + foreach ($data as $key => $value) { + $docx->findAndReplace("\${" . $key . "}", $value); + } + + $docx->flush(); + } + +} diff --git a/DocxMerge/TbsZip.php b/src/TbsZip.php similarity index 99% rename from DocxMerge/TbsZip.php rename to src/TbsZip.php index 76a1c32..eb0c27e 100644 --- a/DocxMerge/TbsZip.php +++ b/src/TbsZip.php @@ -9,6 +9,8 @@ Visit http://www.tinybutstrong.com */ +namespace Jupitern\DocxMerge; + 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