Cookbook

WYSIWYG DOCX editor

phpdocx Advanced and Premium licenses allow to create a WYSIWYG editor for DOCX documents. It is possible to transform DOCX to HTML to see it or modify it in a WYSIWYG editor and then turn it to a DOCX again.

To perform these transformations it is necessary to call two methods:

Both methods use only PHP native classes, so there's no need to install additional tools.

Supported contents and styles of each method are detailed on the documentation pages.

As a practical example, let's work with a DOCX that contains texts with styles, tables, lists and images.

1- Transform the DOCX to HTML

2- Show the HTML obtained in the previous step with any modern WYSIWYG editor like CKEditor

For the sake of the example, CKEditor works here with the standard settings. Anyway, it is possible to include more features like font sizes, preset styles, math equations… This editor and many others allow customization to add just the desired options.

phpdocx transforms DOCX to HTML, generating non inline CSS styles by using style tags. Some WYSIWYG editors require using inline styles. In these cases, it is advisable to convert the HTML obtained with phpdocx to an HTML with inline styles. There are many tools and libraries fit for this purpose, including native PHP solutions: https://stackoverflow.com/questions/3990706/php-class-to-inline-css-styles.

3- Modify freely the contents with the WYSIWYG editor using the supported options. Then generate the DOCX with the embedHTML method

This document can be exported to PDF, DOC, ODT, RTF and other formats with the conversion plugin, or back to HTML with the aforementioned steps.

The methods reviewed here count with multiple options, detailed in its own documentation pages. Supported tags and styles are also explained there.