-2

I have Multi Files (doc and docx ) maybe one maybe more This is defined by User . I will get it all and print it in one file So i need to merge it in one file .

 $filePath   =  '../part_folder/' ;
 $filesName  = [
            '1_en.docx' ,
            '2_en.docx' ,
            '3_en.docx' ,
            '3_en.docx' ,
            '5_en.docx' ,
        ];
//******** in result i want to make one file from it
$finalFile = mergeDocumentParts($filePath , $filesName);
  • 1
    Please add your code which you have done till now, I suggest get all files data and make array and merge all array and write a new doc file. – HP371 Dec 09 '19 at 09:00

1 Answers1

1
 $zip = new clsTbsZip();
        $content = [] ;
        $r = '';
        for ($i = 1  ; $i <  count($filesName)    ; $i++){
        // Open the all document - 1
            $zip -> Open($filePath . $filesName[$i]);
            $content[$i] = $zip->FileRead('word/document.xml');
            $zip->Close();
            // Extract the content of  document
            $p = strpos($content[$i], '<w:body');
            if ($p===false)
                echo ("Tag <w:body> not found in document .".$filesName[$i] );
            $p = strpos($content[$i], '>', $p);
            $content[$i] = substr($content[$i], $p+1);
            $p = strpos($content[$i], '</w:body>');
            if ($p===false)
                echo ("Tag <w:body> not found in document .".$filesName[$i] );
            $content[$i] = substr($content[$i], 0, $p);
            $r .= $content[$i]  ;
        }
        // Insert after first document
        $zip->Open($filePath . $filesName[0]);
        $content2 = $zip->FileRead('word/document.xml');
        $p = strpos($content2, '</w:body>');
        if ($p===false)
            echo ("Tag <w:body> not found in document .".$filesName[0] );
        $content2 = substr_replace($content2, $r, $p, 0);
        $zip->FileReplace('word/document.xml', $content2, TBSZIP_STRING);
        $zip->Flush(TBSZIP_FILE, 'merge.docx');