docx文件损坏图像

时间:2017-12-19 22:04:04

标签: xml xslt phpdocx

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

一旦生成,docx文件就会被创建,但是在打开时,我得到的#34;文件已损坏,无法打开"错误。我点击“确定”即可获得“不可读的内容”#34;错误,文档打开时包含所有正确的数据。

我通过验证器(http://ucd.eeonline.org/validator/index.php)运行完成的docx,它告诉我 "所有图像必须定义备用文字。"

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

以下是我的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');
    }
}

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

1 个答案:

答案 0 :(得分:1)

这是NULL值的结果