1

I am trying to load binary data as images into Word documents (Opem XML) using PHP for later usage with XSLT.

After opening the Word document as a PHP ZipArchive, I am able to load images into the word/media folder succesfully and also update the word/document.xml file. But I am unable to update the <Relationships/> in the word/rels/document.xml.rels file.

I have already cross-checked the xml is in the correct format.

The following is the code snippet I am trying to use,

        $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();

Could anyone suggest what I might be doing wrong?

Todd Main
  • 28,951
  • 11
  • 82
  • 146
Sai Rahul
  • 11
  • 2
  • You're not saying what's happening (or not happening) exactly. Is the file unchanged? – Pekka Jun 24 '10 at 08:03
  • If I comment out the part trying to add xml nodes to the docmuents.xml.rels file, the images get saved into the word document. But, if I try to add these relationships to the .rels file, even the images are not saved. I am using the PHP code mentioned in the below link, http://msdn.microsoft.com/en-us/library/ee840137(office.12).aspx and trying to add images to the $outputDocument before applying $newContent to word/document.xml – Sai Rahul Jun 24 '10 at 11:26

1 Answers1

0

You're adding a new content type as well when you add the PNG, so you need to set that in [Content_Types].xml. See Is it possible to add some data to a Word document? for more details.

Community
  • 1
  • 1
Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • Thanks for the post, but there is already the PNG content type in [Content-Types].xml. As I said before, I am able to add images to the word document. I am unable to add relationships to the _rels/document.xml.rels file. The code is not working only when I try to add relationships, if I comment out that part, I am able to add images to the document. – Sai Rahul Jul 05 '10 at 10:37