2010-06-24 33 views
0

我试图加载二进制数据作为图像转换成使用PHP供以后使用XSLT使用Word文档(Opem XML)来更新字/ RELS/document.xml.rels。无法使用PHP ZipArchive

打开Word文档作为一个PHP ZipArchive后,我能图像加载到字/媒体文件夹,成功地与同时更新字/ document.xml中文件。但我无法更新在字/ RELS/document.xml.rels的<Relationships/>文件。

我已经交叉检查的XML是正确的格式。

以下是代码片段,我尝试使用,

 $zipArchive=new ZipArchive(); 
    $zipArchive->open($pathToDoc); 
    $imagePre="image"; 
    $relIdPre="rId"; 
    $index=100; 

    $nodeList = $reportDOM->getElementsByTagName("Node"); 
    $i=0; 

    foreach($nodeList as $node) { 
     $divList = $node->getElementsByTagName("*"); 

     foreach ($divList as $divNode) { 
      if (strncasecmp($divNode->nodeName, "wizChart", 8) == 0) { 
       $imgData=$divNode->getAttribute("src"); 
       $imgData=base64_decode(substr($imgData,22)); 

        $zipArchive-> 
addFromString("word/media/".$imagePre."".$index.".png",$imgData); 
       $fp=$zipArchive->getStream("word/_rels/document.xml.rels"); 

       $contents=''; 
       while (!feof($fp)) { 
        $contents .= fread($fp, 2); 
       } 
       $serviceOutput=new DOMDocument(); 
       $serviceOutput->loadXML($contents); 
       $serviceList=$serviceOutput->getElementsByTagName("Relationships"); 

       $element=$serviceOutput->createElement("Relationship"); 
       $element->setAttribute("Id",$relIdPre."".$index); 
       $element->setAttribute("Type","http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"); 
       $element->setAttribute("Target","word/media/".$imagePre."".$index.".png"); 

       foreach ($serviceList as $serviceNode) { 
       $serviceNode->appendChild($element); 
       } 

       $zipArchive->addEmptyDir("word/_rels/"); 
       $zipArchive->addFromString("word/_rels/document.xml.rels", $serviceOutput->saveXML()); 
       $index++; 
      }  
     } 
    } 
    $zipArchive->close(); 

可能有人建议我可能是做错了什么?

+0

你不是在说什么发生(或没有发生)。该文件是否保持不变? – 2010-06-24 08:03:48

+0

如果我注释掉试图将xml节点添加到docmuents.xml.rels文件的部分,图像将保存到word文档中。但是,如果我尝试将这些关系添加到.rels文件,即使图像也不会保存。 我正在使用下面的链接中提到的PHP代码, http://msdn.microsoft.com/en-us/library/ee840137(office.12).aspx 并试图图像添加到$ outputDocument,然后将$ newContent应用到word/document.xml – 2010-06-24 11:26:32

回答

0

你添加新的内容类型,以及当您添加PNG,所以你需要设置,在[CONTENT_TYPES] .XML。有关更多详细信息,请参阅Is it possible to add some data to a Word document?

+0

感谢这篇文章,但[Content-Types] .xml中已经有PNG内容类型。正如我之前所说,我可以将图像添加到单词文档。我无法将关系添加到_rels/document.xml.rels文件。 当我尝试添加的关系,如果我注释掉部分的代码不仅是工作,我能够将图像添加到文档中。 – 2010-07-05 10:37:47