-
Notifications
You must be signed in to change notification settings - Fork 1
/
phpMergeDocx.php
97 lines (85 loc) · 2.78 KB
/
phpMergeDocx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
include_once('tbszip.php');
/**
* phpMergeDocx version 1.0.0
* Based on : https://stackoverflow.com/a/7960164/7494347
* Date : 2020-11-01
* Author : JohannCOSTE
* Licence : LGPL (beacause use of LGPL tbszip library)
* Description : These functions are intended to merge docx files for LibreOffice in php.
*/
/**
* @param string $document1
* @param string $document2
* @param string $outputfile
* @return $outputfile if success, null otherwise
*/
function merge2Docx($document1, $document2, $outputfile)
{
$pageBreak ='<w:p><w:pPr><w:sectPr><w:type w:val="nextPage" /></w:sectPr></w:pPr></w:p>';
if (file_exists($document1) && file_exists($document2)) {
$zip = new clsTbsZip();
// Open the first document
$zip->Open($document1);
$content1 = $zip->FileRead('word'.DIRECTORY_SEPARATOR.'document.xml');
$zip->Close();
// Extract the content of the first document
$p = strpos($content1, '<w:body');
if ($p === false) {
// Tag <w:body> not found in document 1
return null;
}
$p = strpos($content1, '>', $p);
$content1 = substr($content1, $p + 1);
$p = strpos($content1, '</w:body>');
if ($p === false) {
// Tag </w:body> not found in document 1
return null;
}
$content1 = $pageBreak.substr($content1, 0, $p);
// Insert into the second document
$zip->Open($document2);
$content2 = $zip->FileRead('word'.DIRECTORY_SEPARATOR.'document.xml');
$p = strpos($content2, '</w:body>');
if ($p === false) {
// Tag </w:body> not found in document 2
return null;
}
$content2 = substr_replace($content2, $content1, $p, 0);
$zip->FileReplace('word'.DIRECTORY_SEPARATOR.'document.xml', $content2, 32);
// Save the merge into a third file
$zip->Flush(TBSZIP_FILE, $outputfile);
return $outputfile;
} else {
// $document1 or $document2 doesn't exist
return null;
}
}
/**
* @param string[] $documents
* @param string $outputfile
* @return 0 if success, null otherwise
*/
function mergeDocx($documents, $outputfile)
{
$documents = array_reverse($documents);
$nbDocx = count($documents);
if ($nbDocx >= 2) {
$previousMerge = merge2Docx($documents[0], $documents[1], $outputfile);
for ($i = 2; $i < $nbDocx; $i++) {
if ($previousMerge !== null) {
$previousMerge = merge2Docx($previousMerge, $documents[$i], $outputfile);
} else {
return null;
}
}
return 0;
}
elseif ($nbDocx == 1){
copy($documents[0], $outputfile);
return 0;
}
else{
return null;
}
}