Practical phpdocx

HTML and CSS

Introduction

One of the most remarkable features of phpdocx is the possibility of working directly with HTML and CSS to create contents.

Just define the HMTL code and its styles and you will get a Word format conversion with great quality and similarity with the original code.

In order to use this functionality it is necessary to have Tidy installed and enabled.

HTML and CSS to WordML

This is the available method to add HTML to a document:

In the $html parameter, write the HTML and CSS you want to transform to WordML; either the styles in the head or inline in the tags. The second parameter, $options, is an array of options. The most important ones options are:

  • isFile: It's a boolean to set if the HTML is in a file. By default, it is a string.
  • downloadImages: boolean to downlaod the images.
  • wordStyles: array to associate to a class or id Word styles. It must be used along with the strictWordStyles option of this same method.

For example, to add a HTML code with inline styles:

Replacing variables in templates

When working with a template, the following method allows to replace a placeholder with HTML:

This HTML code is transformed into a WordML before replacing the placeholder.

The available parameters are:

  • $var: Name of the placeholder.
  • $type: Block or inline.
  • $html: HTML with inline styles.
  • $options: It has the same available options as the embedHtml method.

After loading the template, an easy replacement would be:

This code changes the placeholder VAR by a paragraph with the text “phpdocx”.

Tips and tricks

If the CSS styles are shown in the document instead of being transformed, that's because Tidy must be installed or enabled.

When adding HTML with images, it is advisable to enable the parameter donwloadImages. It is important too to make sure that the paths are defined as absolute instead of relative.

Next - Sections and properties