平头哥 2016-05-30 08:56:00 14889次浏览 3条评论 3 0 0

1.通过composer安装,添加到composer.json中,然后composer update ,在vendor里会有phpoffice的文件夹

 {
     "require": {
        "phpoffice/phpword": "dev-master"
     }
 }

2.(根据自己的路径正确引入)

require_once (__DIR__ . '/../vendor/phpoffice/phpword/src/PhpWord/Autoloader.php');

3.

$phpWord = new \PhpOffice\PhpWord\PhpWord();

4.这个13装不下去了,不会写教程,哈哈,直接贴PHPWord文档,戳这里吧!

  • 评论于 2017-10-13 11:32 举报

    1.最好用xml的格式做模板。

    2.图片也可以替换,改类库
    public function setImg( $strKey, $img){

        $strKey = '${'.$strKey.'}';
        
        $relationTmpl = '<Relationship Id="RID" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/IMG"/>';
        $imgTmpl = '<w:pict><v:shape type="#_x0000_t75" style="position:absolute;left:0;text-align:left;margin-left:MLpt;margin-top:MTpt;width:WIDpx;height:HEIpx"><v:imagedata r:id="RID" o:title=""/></v:shape></w:pict>';
    
        $toAdd = $toAddImg = $toAddType = '';
        $aSearch = array('RID', 'IMG');
        $aSearchType = array('IMG', 'EXT');
        $countrels=$this->_countRels++;
        //I'm work for jpg files, if you are working with other images types -> Write conditions here
        $imgExt = 'jpg';
        $imgName = 'img' . $countrels . '.' . $imgExt;
    
        $this->zipClass->deleteName('word/media/' . $imgName);
        $this->zipClass->addFile($img['src'], 'word/media/' . $imgName);
    
        $typeTmpl = '<Override PartName="/word/media/'.$imgName.'" ContentType="image/EXT"/>';
    
        $rid = 'rId' . $countrels;
        $countrels++;
        list($w,$h) = getimagesize($img['src']);
    
        if(isset($img['swh'])) //Image proportionally larger side
        {
            if($w<=$h)
            {
               $ht=(int)$img['swh'];
               $ot=$w/$h;
               $wh=(int)$img['swh']*$ot;
               $wh=round($wh);
            }
            if($w>=$h)
            {
               $wh=(int)$img['swh'];
               $ot=$h/$w;
               $ht=(int)$img['swh']*$ot;
               $ht=round($ht);
            }
            $w=$wh;
            $h=$ht;
        }
    
        if(isset($img['size']))
        {
            $w = $img['size'][0];
            $h = $img['size'][1];           
        }
        if (isset($img['mt'])) {
            $mt = $img['mt'];
        }
        if (isset($img['ml'])) {
            $ml = $img['ml'];
        }
        $toAddImg .= str_replace(array('RID', 'WID', 'HEI','ML', 'MT'), array($rid, $w, $h,$ml, $mt), $imgTmpl) ;
        if(isset($img['dataImg']))
        {
            $toAddImg.='<w:br/><w:t>'.$this->limpiarString($img['dataImg']).'</w:t><w:br/>';
        }
    
        $aReplace = array($imgName, $imgExt);
        $toAddType .= str_replace($aSearchType, $aReplace, $typeTmpl) ;
    
        $aReplace = array($rid, $imgName);
        $toAdd .= str_replace($aSearch, $aReplace, $relationTmpl);
    
        $this->tempDocumentMainPart=str_replace('<w:t>' . $strKey . '</w:t>', $toAddImg, $this->tempDocumentMainPart);
    
        if($this->_rels=="")
        {
            $this->_rels=$this->zipClass->getFromName('word/_rels/document.xml.rels');
            $this->_types=$this->zipClass->getFromName('[Content_Types].xml');
        }
    
        $this->_types = str_replace('</Types>', $toAddType, $this->_types) . '</Types>';
        $this->_rels  = str_replace('</Relationships>', $toAdd, $this->_rels) . '</Relationships>';
    }
    
    
    

    $templateProcessor->setImg('IMG', array('src' => $model->img, 'swh' => '180', 'ml' => '45', 'mt' => '160'));

  • 评论于 2017-11-14 16:43 举报

    老哥,你那个第二步自动加载的代码是添加到哪个文件里面的

  • 评论于 2018-03-07 10:51 举报

    有没有用过merge 的 方法

您需要登录后才可以评论。登录 | 立即注册