PHPWord(很少人会用到吧!) [ 2.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文档,戳这里吧!
平头哥 山沟沟
注册时间:2016-04-25
最后登录:2020-07-23
在线时长:59小时17分
最后登录:2020-07-23
在线时长:59小时17分
- 粉丝9
- 金钱2000
- 威望30
- 积分2890
共 3 条评论
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'));
老哥,你那个第二步自动加载的代码是添加到哪个文件里面的
有没有用过merge 的 方法