合并2文件docx使用tbszip

I' using OpenTBS to merge 2 file docx.

include_once('tbszip.php');

$zip = new clsTbsZip();

// Open the first document

$zip->Open('file-1.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('file-2.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_DOWNLOAD, 'merge1.docx');

content in file-1.docx include image+text, file-2: only text. But when gen file merge1.docx, can not gen image from file-1.docx Please for me a solution, thanks. P/s: sorry for my english.


when I reversed the order to open the file, file merge1.docx full content. why?

// Open the first document

$zip->Open('file-2.docx');
$content1 = $zip->FileRead('word/document.xml');
$zip->Close();


..........
// Insert into the second document
$zip->Open('file-1.docx');

It is quite difficult to merge two DOCX because of internal elements such as pictures, charts, ...

  • In the archive, pictures must be saved in the word/media/ directory.
  • They must be declared in the file /[Content_Types].xml
  • They also must be assigned to a unique Id in the file /word/_rels/document.xml.rels.
  • And then the unique Id must be used in a XML element corresponding to the picture in the word/document.xml file.

So in order to merge two DCX files you have to apply your snippet, then get the pictures from DOCX to the other, and then perform the operation above.

You're using TbsZip, which is used by OpenTBS but it is not the same tool. OpenTBS won't help your to merge two DOCX together.

你好  请问图片的问题是怎么解决的