$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Добавим секцию (в Word весь текст разделен на секции)
$section = $phpWord->addSection();
// Добавим наш HTML в секцию
$html = $_POST['content'] ?? '';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Сохраняем файл
$phpWord->save("html-to-doc.docx", "Word2007");
$html = $_POST['content'] ?? '';
$html = !empty($_POST['content']) ? $_POST['content'] : '';
<?php
include (__DIR__ . '/vendor/autoload.php');
if (!empty($_POST)) {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Добавим секцию (в Word весь текст разделен на секции)
$section = $phpWord->addSection();
// Добавим наш HTML в секцию
$html = $_POST['content'] ?? '';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Сохраняем файл
$phpWord->save("html-to-doc.docx", "Word2007");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CKEditor</title>
<script src="https://cdn.ckeditor.com/4.14.0/standard/ckeditor.js"></script>
</head>
<body>
<form method="post">
<textarea name="content"></textarea>
<input type="submit" value="OK" />
</form>
<script>
CKEDITOR.replace( 'content' );
</script>
</body>
</html>
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$source = "2.docx";
$phpWord = $objReader->load($source);
$body = '';
foreach($phpWord->getSections() as $section) {
$arrays = $section->getElements();
foreach($arrays as $e) {
if(get_class($e) === 'PhpOffice\PhpWord\Element\TextRun') {
foreach($e->getElements() as $text) {
$font = $text->getFontStyle();
$size = $font->getSize()/10;
$bold = $font->isBold() ? 'font-weight:700;' :'';
$color = $font->getColor();
$fontFamily = $font->getName();
$body .= '<span style="font-size:' . $size . 'em;font-family:' . $fontFamily . '; '.$bold.'; color:#'.$color.'">';
$body .= $text->getText().'</span>';
//print_r($font);
//break;
//$text->getText();
}
}
else if(get_class($e) === 'PhpOffice\PhpWord\Element\TextBreak') {
$body .= '</ >';
}
else if(get_class($e) === 'PhpOffice\PhpWord\Element\Table') {
$body .= '<table border="2px">';
$rows = $e->getRows();
foreach($rows as $row) {
$body .= '<tr>';
$cells = $row->getCells();
foreach($cells as $cell) {
$body .= '<td style="width:'.$cell->getWidth().'">';
$celements = $cell->getElements();
foreach($celements as $celem) {
if(get_class($celem) === 'PhpOffice\PhpWord\Element\Text') {
$body .= $celem->getText();
}
else if(get_class($celem) === 'PhpOffice\PhpWord\Element\TextRun') {
foreach($celem->getElements() as $text) {
$body .= $text->getText();
}
}
else {
//$body .= get_class($celem);
}
}
$body .= '</td>';
}
$body .= '</tr>';
}
$body .= '</table>';
}
else {
$body .= $e->getText();
}
}
break;
}
print_r($body);
$source = __DIR__. "/html-to-doc.docx";
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
/** @var \PhpOffice\PhpWord\PhpWord $phpWord */
$phpWord = $objReader->load($source);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('test.html');
$source = __DIR__. "/html-to-doc.docx";
$objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
/** @var \PhpOffice\PhpWord\PhpWord $phpWord */
$phpWord = $objReader->load($source);
foreach($phpWord->getSections() as $section) {
foreach($section->getElements() as $element) {
if(method_exists($element,'getText')) {
echo($element->getText() . "<br>");
}
}
}