带有图像的 docx 文件损坏

docx file corruption with image

我正在使用 xslt 写入 xml,它替换了 docx 文件中的 word/document.xml 文件。

生成后,docx 文件已创建,但在打开时,出现 "file is corrupt and cannot be opened" 错误。我将单击“确定”并收到 "unreadable content" 错误,并且文档会打开并包含所有正确的数据。

我 运行 通过验证器 (http://ucd.eeonline.org/validator/index.php) 完成的 docx,它告诉我 "All images must have alternate text defined."

我的图片进入文档,我在生成的文件中使用了 alt 标签 XML。

这是我的 xslt 文件中与图像相关的代码:

<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"/>

这是生成的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"/>

我的 xml 有 和 属性。

这是添加图片的函数:

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');
    }
}

知道如何解决这个问题吗?

这是 NULL 值的结果