Categories > OpenTBS with DOCX >

Facing merge problem in more than 2 .docx files.

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Irfan
Date: 2015-01-16
Time: 00:55

Facing merge problem in more than 2 .docx files.

I found the solutions in stackoverflow for the 2 .docx merging file but when I used it then it is destroying the Microsoft word document's format. But my problem is I need to merge at least 5 to 10 MS word documents and when I tried to modify the code its not allow to merge multiple documents. Would anybody help me out to provide me the code or details how can I merge multiple MS word documents.

Thank you in advance

Following is the code which I'm using:

<?php

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the first document
$zip->Open('doc1.docx');
$content1 = $zip->FileRead('word/document.xml');
$zip->Close();

// Extract the content of the first document
$p = strpos($content1, '<w:body');
if ($p===false) exit("Tag <w:body> not found in document 1.");
$p = strpos($content1, '>', $p);
$content1 = substr($content1, $p+1);
$p = strpos($content1, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document 1.");
$content1 = substr($content1, 0, $p);

// Insert into the second document
$zip->Open('doc2.docx');
$content2 = $zip->FileRead('word/document.xml');
$p = strpos($content2, '</w:body>');
if ($p===false) exit("Tag </w:body> not found in document 2.");
$content2 = substr_replace($content2, $content1, $p, 0);
$zip->FileReplace('word/document.xml', $content2, TBSZIP_STRING);

// Save the merge into a third file
$zip->Flush(TBSZIP_FILE, 'merge.docx');

By: Skrol29
Date: 2015-01-17
Time: 23:51

Re: Facing merge problem in more than 2 .docx files.

Hi,

What do you mean by
>  it is destroying the Microsoft word document's format.
and
> its not allow to merge multiple documents

The solution given in Stackoverflow and on this forum does warn that the definitions of the internal formats must be the same between the tow documents otherwise they may be format bugs.

The code shows the trick for 2 DOCX, if you understand what is is done, you can adapt it for several DOCX.
By: Irfan
Date: 2015-01-18
Time: 12:42

Re: Facing merge problem in more than 2 .docx files.

>  it is destroying the Microsoft word document's format.
When I use above code it doesn't merge properly let say if word document has 2 pages and when I merged using above code its added few spaces and removed the table's border after merging its show in 2.5 pages.

> its not allow to merge multiple documents
This above code is working perfectly for 2 docx. But when I try to 3 or more docx merge and it is not working. So I need sample code for multiple docx merging.

>>>The code shows the trick for 2 DOCX, if you understand what is is done, you can adapt it for several DOCX.
TBS does not allow merge docx more than 2 files?

Please help me in this regards. I really appreciate your help and quick response.

Thanks
By: Skrol29
Date: 2015-01-19
Time: 00:52

Re: Facing merge problem in more than 2 .docx files.

The problem you have when merging docx files is normal.
That's the limitation of this technical, and I don't know any other technical that can combine two DOCX into a single one with PHP.
It can work only if internal definitions such as formats, images, charts, ... are identical between the two DOCX, which is quite hard to be done.

See
http://www.tinybutstrong.com/forum.php?thr=2757

For merging more than 2 files, it seems quite easy when reading the snippet. Is is more a PHP question that a TBS question.
One part of the script is getting the body content a the first DOCX, and the second part of the scrip is inserting the content at the end of the body of the second DOCX.