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?