M
M
marq2020-04-19 13:30:08
PHP
marq, 2020-04-19 13:30:08

How to save text to document using PHPWord from ckeditor?

how to save text written in ckeditor to a document using the PHPWord library, with formatting preserved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2020-04-19
@marq

Accept a POST request with the text that the user entered into the editor and save it to a file.

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

Since we are inserting not just text into the file, but HTML, the formatting will remain the same as in the js editor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question