xmlxsltphpdocx

docx file corruption with image


I am using an xslt to write to xml, which replaces the word/document.xml file in the docx file.

Once generated, the docx file is created but upon opening, I get the "file is corrupt and cannot be opened" error. I'll click OK and get the "unreadable content" error and the document does open with all the proper data.

I ran the finished docx through a validator (http://ucd.eeonline.org/validator/index.php) and it tells me that "All images must have alternate text defined."

My image is coming into the document, and I am using an alt tag in the generated XML.

Here is the code from my xslt file as it pertains to the image:

<w:bookmarkStart w:id="0" w:name="_GoBack"/>
          <w:r>
            <w:rPr>
              <w:rFonts w:ascii="Calibri" w:cs="Segoe UI" w:hAnsi="Calibri"/>
              <w:noProof/>
            </w:rPr>
            <w:drawing>
              <wp:inline distB="0" distL="0" distR="0" distT="0">
                <wp:extent cx="3093810" cy="2320356"/>
                <wp:effectExtent b="3810" l="0" r="0" t="0"/>
                <wp:docPr id="1" name="Picture 1"/>
                <wp:cNvGraphicFramePr>
                  <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
                </wp:cNvGraphicFramePr>
                <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                  <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                    <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                      <pic:nvPicPr>
                        <pic:cNvPr id="1" name="Tulips.jpg"/>
                        <pic:cNvPicPr/>
                      </pic:nvPicPr>
                      <pic:blipFill>
                        <a:blip cstate="print" r:embed="rId5">
                          <a:extLst>
                            <a:ext uri="">
                              <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
                            </a:ext>
                          </a:extLst>
                        </a:blip>
                        <a:stretch>
                          <a:fillRect/>
                        </a:stretch>
                      </pic:blipFill>
                      <pic:spPr>
                        <a:xfrm>
                          <a:off x="0" y="0"/>
                          <a:ext cx="3105628" cy="2329220"/>
                        </a:xfrm>
                        <a:prstGeom prst="rect">
                          <a:avLst/>
                        </a:prstGeom>
                      </pic:spPr>
                    </pic:pic>
                  </a:graphicData>
                </a:graphic>
              </wp:inline>
            </w:drawing>
          </w:r>
          <w:bookmarkEnd w:id="0"/>

This is the generated word/document.xml:

 <w:bookmarkStart w:id="0" w:name="_GoBack"/>
        <w:r>
          <w:rPr>
            <w:rFonts w:ascii="Calibri" w:cs="Segoe UI" w:hAnsi="Calibri"/>
            <w:noProof/>
          </w:rPr>
          <w:drawing>
            <wp:inline distB="0" distL="0" distR="0" distT="0">
              <wp:extent cx="3093810" cy="2320356"/>
              <wp:effectExtent b="3810" l="0" r="0" t="0"/>
              <wp:docPr id="1" name="Picture 1"/>
              <wp:cNvGraphicFramePr>
                <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
              </wp:cNvGraphicFramePr>
              <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                  <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                    <pic:nvPicPr>
                      <pic:cNvPr id="0" name="11786.jpeg"/>
                      <pic:cNvPicPr/>
                    </pic:nvPicPr>
                    <pic:blipFill>
                      <a:blip cstate="print" r:embed="rId5">
                        <a:extLst>
                          <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                            <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
                          </a:ext>
                        </a:extLst>
                      </a:blip>
                      <a:stretch>
                        <a:fillRect/>
                      </a:stretch>
                    </pic:blipFill>
                    <pic:spPr>
                      <a:xfrm>
                        <a:off x="0" y="0"/>
                        <a:ext cx="3105628" cy="2329220"/>
                      </a:xfrm>
                      <a:prstGeom prst="rect">
                        <a:avLst/>
                      </a:prstGeom>
                    </pic:spPr>
                  </pic:pic>
                </a:graphicData>
              </a:graphic>
            </wp:inline>
          </w:drawing>
        </w:r>
        <w:bookmarkEnd w:id="0"/>

My xml has both and attributes.

Here is the function that adds the images:

protected function addImages()
{
    // Check which image type(s) are registered in [Content_Types].xml
    $this->checkDefaultContentTypes();
    // Prepare word/_rels/document.xml.rels to add details of the images
    $this->initRelationships();
    // Get the merged text content ready for the image details
    $doc = new \DOMDocument();
    $doc->loadXML($this->mergedContent);
    $docPr = $doc->getElementsByTagNameNS(self::WP_NS, 'docPr');
    $cNvPr = $doc->getElementsByTagNameNS(self::PIC_NS, 'cNvPr');
    $blip = $doc->getElementsByTagNameNS(self::A_NS, 'blip');
    // Get the image filenames from the XML source
    $images = $this->getImageFilenames();
    $i = 0;
    $imgNum = 1;
    // Add the details of each image to the merged content
    foreach ($images as $image) {
        $pr = $docPr->item($i);
        $pr->setAttribute('id', $imgNum);
        $pr->setAttribute('name', 'Picture ' . $imgNum);
        $cNvPr->item($i)->setAttribute('id', 0);
        $cNvPr->item($i)->setAttribute('name', $image);
        $blip->item($i)->setAttributeNS(self::R_NS, 'r:embed', 'rId' . $this->imageStart);
        $ext = $blip->item($i)->getElementsByTagNameNS(self::A_NS, 'ext');
        $ext->item(0)->setAttribute('uri', '{28A0092B-C50C-407E-A947-70E740481C1C}');
        $extension = $this->checkType($image);
        $this->zip->addFile($this->imageSource . $image, 'word/media/image' . $i . ".$extension");
        $this->generateNewRelationship(self::IMAGE_NS, 'media/image' . $i . ".$extension");
        $i++;
        $imgNum++;
        $this->imageStart++;
    }
    // Add the Relationship elements that need to follow the images
    foreach ($this->relsToAdd as $rel) {
        $this->generateNewRelationship($rel['Type'], $rel['Target']);
        $this->imageStart++;
    }
    // Save the updated version of document.xml.rels, and add to the download file
    $relations = $this->docRels->saveXML();
    $this->zip->addFromString('word/_rels/document.xml.rels', $relations);

    // Save the updated version of [Content_Types].xml and add it to the download file
    $types = $this->types->saveXML();
    $this->zip->addFromString('[Content_Types].xml', $types);

    // Save the updated merged content
    $this->mergedContent = $doc->saveXML();

    // Add the header and footer Relationship IDs to the merged content if necessary
    if ($this->header) {
        $this->fixHeadersFooters($relations, 'header');
    }
    if ($this->footer) {
        $this->fixHeadersFooters($relations, 'footer');
    }
}

Any idea how to resolve this?


Solution

  • This was the result of a NULL value