在TemplateProcessor中使用PHPWord元素
我有一个Word模板,其中包含几个宏(客户名称、地址等)以及一些生成的表,这些表运行良好。我需要填充的宏之一是web表单上TinyMCE字段中的数据,该字段生成HTML格式的数据 然而,我看到了我需要注入的HTML来自TinyMCE编辑器,可以包含表之类的内容 我能够将HTML写入磁盘并将其转换为.docx文件,然后使用PHPWord提取元素,但是我似乎无法将这些元素注入TemplateProcessor,因为它们的类型不对 任何帮助都将不胜感激在TemplateProcessor中使用PHPWord元素,php,html,word,phpword,Php,Html,Word,Phpword,我有一个Word模板,其中包含几个宏(客户名称、地址等)以及一些生成的表,这些表运行良好。我需要填充的宏之一是web表单上TinyMCE字段中的数据,该字段生成HTML格式的数据 然而,我看到了我需要注入的HTML来自TinyMCE编辑器,可以包含表之类的内容 我能够将HTML写入磁盘并将其转换为.docx文件,然后使用PHPWord提取元素,但是我似乎无法将这些元素注入TemplateProcessor,因为它们的类型不对 任何帮助都将不胜感激 /** * Converts HTML dat
/**
* Converts HTML data into XML elements for use in Word
*
* @param string $html
* @return \PhpOffice\PhpWord\Element\Section
*/
public function convertHTML($html)
{
global $config;
$format = "<html><head></head><body>%s</body></html>";
$temphtml = tempnam($config['tmp_dir'], 'PHPWord');
$tempdocx = tempnam($config['tmp_dir'], 'PHPWord');
$elements = [];
// Write the HTML to disk
$fp = fopen($temphtml, 'w');
fprintf($fp, $format, $html);
fclose($fp);
// Convert the HTML to Word
$unoconv = Unoconv::create();
$unoconv->transcode($temphtml, 'docx', $tempdocx);
// Parse the word doc
$word = IOFactory::load($tempdocx);
$sections = $word->getSections();
foreach ($sections as $section) {
$elements = array_merge($elements, $section->getElements());
}
unlink($temphtml);
unlink($tempdocx);
$section = $word->addSection();
foreach ($elements as $element) {
$section->addElement($element);
}
return $section;
}
$template->setComplexValue()
失败,因为结果是PhpOffice\PhpWord\Element\Section类型,setComplexValue只接受PhpOffice\PhpWord\Writer\Word2007\Element*,所以我想我已经走到了死胡同
// See if we're dealing with a TinyMCE field
$def = $bean->getFieldDefinition($varArray[0]);
if ($def['type'] === 'tinymce') {
// Convert the HTML from the TinyMCE field into Word elements
$section = $this->convertHTML($bean->{$varArray[0]});
$this->setComplexValue($var, $section);
} else {
// Just treat it like a normal text value
$this->setValue($var, $bean->{$varArray[0]});
}