Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----- PHPWord With HTML Text Tanpa Template -----
- $pw = new \PhpOffice\PhpWord\PhpWord();
- $section = $pw->addSection();
- // Add HTML
- $html = "<h1>Hello World</h1>";
- $html .= "<p>Paragraph 1.</p>";
- $html .= "<p><b>Paragraph 2</b></p>";
- \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
- // ALT
- header("Content-Type: application/octet-stream");
- header("Content-Disposition: attachment; filename=test.docx");
- $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "Word2007");
- $objWriter->save("php://output");
- // ----- PHPWord With HTML Text Dengan Template -----
- $phpWord = new \PhpOffice\PhpWord\PhpWord();
- $pw = $phpWord->loadTemplate('assets/files/template_soal_answer.docx');
- // Add HTML
- $html = "<h1>Hello World</h1>";
- $html .= "<p>Paragraph 1.</p>";
- $html .= "<p><b>Paragraph 2</b></p>";
- $parser = new \HTMLtoOpenXML\Parser();
- \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false);
- $ooXml = $parser->fromHTML($html); // Parse Text To HTML
- $pw->setValue('variable', $ooXml); // Set Value
- \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
- // Break Line
- $pw->setValue('ganti_baris', '${newline}');
- $new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t> </w:t><w:br/><w:t>');
- $pw->setComplexValue('newline', $new_line);
- // Process Export
- header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
- header("Content-Disposition: attachment; filename=Test.docx");
- $pw->saveAs('php://output');
Advertisement
Add Comment
Please, Sign In to add comment