-1

I am trying to make a website witch people submits a form. One part of this form is using ckeditor to get input from user. Becasuse of data that is coming from ckeditor is html you need to turn html to openxml to use tamplatingprocessor of phpword but whenever i run my script i am geting blank section on the output file.

PHP Code:

$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";
}

Output of HTMLtoOpenXML:

<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>

i dont thing problem is about HTMLtoOpenXML i thing problem is about

All sections working except section2 on output file. The place where data of section2 supposed to be is just empty.

  • From the PHPWord docs: "Only single-line values can be replaced." Are you sure you are only replacing simple strings? – Maria Mar 27 '18 at 11:07
  • @Maria Thank you for yor commend. I didn't know that i cannot use taplating engine withe multi-line values. – functionator Mar 27 '18 at 14:00
  • I suggest you unpack your output-file (with 7zip or something like that) and check out the document.xml. This contains the XML of your result document. Look if yout HTMLtoOpenXML output is really not there, or if it's just not shown in the docx-file. I'm guessing the Template-Processor is trying to write a Paragraph () into a text node. Word probably can't handle that. If you want the HTMLtoOpenXML output in the word-file, try escaping it. – Maria Mar 28 '18 at 13:35
  • Yes you are right. Template Processor only replaces the inside of paragraph and i am repalceing inside of paragraph with another paragraph and that gives error. If i just delete first paragraph opening and last paragraph closing it sholud work. I am gona custimize HtmltoOpenXML editor for this problem. Thank you for you help @Maria – functionator Mar 28 '18 at 18:48
  • I postet it as an answer, so the thread doesn't just sit here seemlingly unanswered ;) Hope it works! :) – Maria Apr 03 '18 at 07:59

1 Answers1

0

The TemplateProcessor is only able to replace single line strings. For this, it replaces the content of a Word paragraph.

You want to replace this with another paragraph (<w:p>) from your HTMLtoOpenXML result. Escape this result or delete the paragraph tag and it should work.

Maria
  • 151
  • 2
  • 9