Skip to content
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
71 changes: 0 additions & 71 deletions DocxMerge.php

This file was deleted.

33 changes: 0 additions & 33 deletions DocxMerge/DocxMergeAutoload.php

This file was deleted.

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand All @@ -21,7 +22,7 @@ Merge Example
$dm->merge( [
"templates/TplPage1.docx",
"templates/TplPage2.docx"
], "/tmp/result.docx" );
], "/tmp/result.docx", true );


setValues Example
Expand All @@ -33,4 +34,4 @@ setValues Example
$dm = new DocxMerge();
$dm->setValues( "templates/template.docx",
"templates/result.docx",
array( "NAME" => "Sterling", "SURNAME" => "Archer" ) );
array( "NAME" => "Sterling", "SURNAME" => "Archer" ) );
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {

}
}
}
13 changes: 8 additions & 5 deletions DocxMerge/Docx.php → src/Docx.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Date: 05.02.14
* Time: 11:59
*/
namespace Jupitern\DocxMerge;


class Docx {

Expand Down Expand Up @@ -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 );
}

Expand All @@ -67,8 +69,9 @@ 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 All @@ -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" ) {
Expand Down
67 changes: 67 additions & 0 deletions src/DocxMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* User: krustnic
* Date: 04.02.14
* Time: 11:17
*/

namespace Jupitern\DocxMerge;

class DocxMerge
{

/**
* Merge files in $docxFilesArray order and
* create new file $outDocxFilePath
* @param $docxFilesArray
* @param $outDocxFilePath
* @return int
*/
public function merge($docxFilesArray, $outDocxFilePath, $addPageBreak = false)
{
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; $i < count($docxFilesArray); $i++) {
$docx->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();
}

}
2 changes: 2 additions & 0 deletions DocxMerge/TbsZip.php → src/TbsZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down