FebimaHermawan

PHPWord HTML Text in Codeigniter

Nov 9th, 2020
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1. // ----- PHPWord With HTML Text Tanpa Template -----
  2.  
  3. $pw         = new \PhpOffice\PhpWord\PhpWord();
  4. $section        = $pw->addSection();
  5.  
  6. // Add HTML
  7. $html       = "<h1>Hello World</h1>";
  8. $html       .= "<p>Paragraph 1.</p>";
  9. $html       .= "<p><b>Paragraph 2</b></p>";
  10. \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
  11.  
  12. // ALT
  13. header("Content-Type: application/octet-stream");
  14. header("Content-Disposition: attachment; filename=test.docx");
  15. $objWriter  = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "Word2007");
  16. $objWriter->save("php://output");
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. // ----- PHPWord With HTML Text Dengan Template -----
  24.  
  25. $phpWord    = new \PhpOffice\PhpWord\PhpWord();
  26. $pw         = $phpWord->loadTemplate('assets/files/template_soal_answer.docx');
  27.  
  28. // Add HTML
  29. $html       = "<h1>Hello World</h1>";
  30. $html       .= "<p>Paragraph 1.</p>";
  31. $html       .= "<p><b>Paragraph 2</b></p>";
  32.  
  33. $parser = new \HTMLtoOpenXML\Parser();
  34. \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false);
  35. $ooXml = $parser->fromHTML($html); // Parse Text To HTML
  36. $pw->setValue('variable', $ooXml); // Set Value
  37.  \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
  38.  
  39.  
  40. // Break Line
  41. $pw->setValue('ganti_baris', '${newline}');
  42. $new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t> </w:t><w:br/><w:t>');
  43. $pw->setComplexValue('newline', $new_line);
  44.  
  45.  
  46. // Process Export
  47. header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
  48. header("Content-Disposition: attachment; filename=Test.docx");
  49. $pw->saveAs('php://output');
Advertisement
Add Comment
Please, Sign In to add comment