为什么我在使用htmltopenxml时在PHPword上得到空页面?

为什么我在使用htmltopenxml时在PHPword上得到空页面?,php,openxml,phpword,phpoffice,Php,Openxml,Phpword,Phpoffice,我正在尝试制作一个网站,让人们提交表单。此表单的一部分是使用ckeditor从用户处获取输入。由于来自ckeditor的数据是html,您需要将html转换为openxml以使用phpword的tamplatingprocessor,但每当我运行脚本时,我都会在输出文件上获得空白部分 PHP代码: $toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML($data[1]); $templateProcessor = new \PhpOffic

我正在尝试制作一个网站,让人们提交表单。此表单的一部分是使用ckeditor从用户处获取输入。由于来自ckeditor的数据是html,您需要将html转换为openxml以使用phpword的tamplatingprocessor,但每当我运行脚本时,我都会在输出文件上获得空白部分

PHP代码:

$toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML($data[1]);
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');

$templateProcessor->setValue('section1', $data[0]);
$templateProcessor->setValue('section2', $toOpenXML);
$templateProcessor->setValue('section3', $specialdata);
$templateProcessor->setValue('section4', $data[2]);
$templateProcessor->setValue('section5', $data[3]);

$file_name1 = $file_name.'.docx';
$file_name2 = $file_name.rand().'.docx';

if(file_exists($file_name1)== false){
    $templateProcessor->saveAs($file_name1);  
}elseif(file_exists($file_name2)== false){
    $templateProcessor->saveAs($file_name2);
}else{
    $info['er'] = "3";
}
<w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'>foo</w:t>
  </w:r>
  </w:p>
  <w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'>bar</w:t>
  </w:r>
  </w:p>
  <w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'></w:t>
  </w:r>
</w:p>
HTMLtoOpenXML的输出:

$toOpenXML = HTMLtoOpenXML::getInstance()->fromHTML($data[1]);
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');

$templateProcessor->setValue('section1', $data[0]);
$templateProcessor->setValue('section2', $toOpenXML);
$templateProcessor->setValue('section3', $specialdata);
$templateProcessor->setValue('section4', $data[2]);
$templateProcessor->setValue('section5', $data[3]);

$file_name1 = $file_name.'.docx';
$file_name2 = $file_name.rand().'.docx';

if(file_exists($file_name1)== false){
    $templateProcessor->saveAs($file_name1);  
}elseif(file_exists($file_name2)== false){
    $templateProcessor->saveAs($file_name2);
}else{
    $info['er'] = "3";
}
<w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'>foo</w:t>
  </w:r>
  </w:p>
  <w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'>bar</w:t>
  </w:r>
  </w:p>
  <w:p>
  <w:pPr>
    <w:pStyle w:val='OurStyle2'/>
  </w:pPr>
  <w:r>
    <w:t xml:space='preserve'></w:t>
  </w:r>
</w:p>

福
酒吧
我不认为问题在于HTMLTOpenXML,我认为问题在于


除输出文件上的第2部分外,所有部分均正常工作。第2节的数据应该是空的。

TemplateProcessor只能替换单行字符串。为此,它将替换单词段落的内容


您想用HTMLOpenXML结果中的另一个段落(
)替换此段落。转义此结果或删除段落标记,它应该会起作用。

来自PHPWord文档:“只能替换单行值。”您确定只替换简单字符串吗?@Maria感谢您的推荐。我不知道我不能使用带多行值的Tapplating引擎。我建议您解压缩输出文件(使用7zip或类似的东西),然后查看document.xml。它包含结果文档的XML。看看您的htmltopenxml输出是否真的不在那里,或者它是否只是没有显示在docx文件中。我猜模板处理器正在尝试将段落()写入文本节点。Word可能无法处理这个问题。如果您希望在word文件中输出HTMLTOpenXML,请尝试转义它。是的,您是对的。模板处理器只替换段落的内部,我正在用另一个段落替换段落的内部,这会产生错误。如果我只是删除开头的第一段和结尾的最后一段,那就太糟糕了。我将为这个问题定制HTMLTOpenXML编辑器。感谢您的帮助@MariaI将其作为一个答案发布,这样线程就不会像坐在这里一样没有回答;)希望它能起作用!:)