问题描述
我从tinymce获取html正文,其中包含图像和文本(段落)。但是当我尝试将其插入模板时会出现空白图像。
有我的代码:
$PHPWord = new \PHPOffice\PHPWord\PHPWord();
$templateProcessor = new \PHPOffice\PHPWord\TemplateProcessor('./template.docx');
$section = $PHPWord->addSection();
$content = '<p>Test</p> <img src="https://www.sciencemag.org/sites/default/files/styles/inline__450w__no_aspect/public/sparrow_16x9_0.jpg" />';
Html::addHtml($section,$content);
$xml = getXMLContent($section);
\PHPOffice\PHPWord\Settings::setoutputEscapingEnabled(false);
$templateProcessor->setValue('test',$xml);
\PHPOffice\PHPWord\Settings::setoutputEscapingEnabled(true);
$templateProcessor->saveAs('./result.docx');
function getXMLContent($section) {
$xmlWriter = new XMLWriter(XMLWRITER::STORAGE_MEMORY,'./',Settings::hasCompatibility());
$containerWriter = new Container($xmlWriter,$section);
$containerWriter->write();
return $xmlWriter->getData();
}
我做错什么了吗?还是有另一种方法?
解决方法
首先,您正在使用哪个版本的PHPWord? 如果您使用的是最新版本(截至2020年10月),则可以使用此功能:
https://phpword.readthedocs.io/en/latest/templates-processing.html#setimagevalue
将图像添加到Word模板非常简单:
setImageValue(<tag>,<path to file>);
我目前正在尝试固定图像的高度和宽度,但发现它有问题,图像宽度固定为3.04cm。
用于插入图像并设置其高度和宽度的API:
setImageValue(<tag>,array('path'=><path to file>,'height'=><height>,'width'=><width>);