楼主为了偷懒,使用了模板替换,但是在合并多个word的时候,内容很好合并,但是图片丢失了,看了一下源码,发现需要添加文件以及更改索引。
方法如下:
在phpword的 TemplateProcessor 类里添加方法:
public function addImage($partFileName, $rid, $imgPath, $imageMimeType)
{
// define templates
$typeTpl = '<Override PartName="/word/media/{IMG}" ContentType="image/{EXT}"/>';
$relationTpl = '<Relationship Id="{RID}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/{IMG}"/>';
$newRelationsTpl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>';
$newRelationsTypeTpl = '<Override PartName="/{RELS}" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
$extTransform = array(
'image/jpeg' => 'jpeg',
'image/png' => 'png',
'image/bmp' => 'bmp',
'image/gif' => 'gif',
);
// get image embed name
if (isset($this->tempDocumentNewImages[$imgPath])) {
$imgName = $this->tempDocumentNewImages[$imgPath];
} else {
// transform extension
if (isset($extTransform[$imageMimeType])) {
$imgExt = $extTransform[$imageMimeType];
} else {
throw new Exception("Unsupported image type $imageMimeType");
}
// add image to document
$imgName = 'image_' . $rid . '_' . pathinfo($partFileName, PATHINFO_FILENAME) . '.' . $imgExt;
$this->zipClass->pclzipAddFile($imgPath, 'word/media/' . $imgName);
$this->tempDocumentNewImages[$imgPath] = $imgName;
// setup type for image
$xmlImageType = str_replace(array('{IMG}', '{EXT}'), array($imgName, $imgExt), $typeTpl);
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlImageType, $this->tempDocumentContentTypes) . '</Types>';
}
$xmlImageRelation = str_replace(array('{RID}', '{IMG}'), array($rid, $imgName), $relationTpl);
if (!isset($this->tempDocumentRelations[$partFileName])) {
// create new relations file
$this->tempDocumentRelations[$partFileName] = $newRelationsTpl;
// and add it to content types
$xmlRelationsType = str_replace('{RELS}', $this->getRelationsName($partFileName), $newRelationsTypeTpl);
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlRelationsType, $this->tempDocumentContentTypes) . '</Types>';
}
// add image to relations
$this->tempDocumentRelations[$partFileName] = str_replace('</Relationships>', $xmlImageRelation, $this->tempDocumentRelations[$partFileName]) . '</Relationships>';
}
然后把合并的内容存到临时文件,然后重新加载,写入文件:
demo:
$temp = uniqid(sys_get_temp_dir().'/');
$zip->Flush(TBSZIP_FILE, $temp);
$zip->Close();
$dir = FCPATH . 'resources/upload/files/tempdoc/';
if (!is_dir($dir)) {
mkdir($dir, 0777);
}
$file = $student_info['name'].'_'.$student_info['student_num'].'_评阅意见表.docx';
$filename = $dir.$file;
$filename = iconv("UTF-8","GBK//IGNORE",$filename);
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$template = $PHPWord->loadTemplate($temp);
$imgAttrs = @getimagesize($this->user['signature'][1]);
$template->addImage('word/document.xml',$rid,$this->user['signature'][1],$imgAttrs['mime']);
$template->saveAs($filename);
原理是用zip打开word,然后在 word/media 里添加 图片,然后在 word/_rels/document.xml.rels和[Content_Types].xml 里添加索引即可
至此,大功告成。