HTML and CSS
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.
embedHTML is the 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 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 download 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:
When working with a template, replaceVariableByHTML 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/CSS
- $options: Extra options
After loading the template, an easy replacement would be:
This code changes the placeholder VAR by a paragraph with the text “phpdocx”.
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 downloadImages. It is important too to make sure that the paths are defined as absolute instead of relative.
Premium licenses include HTML Extended and CSS Extended features to invoke phpdocx methods with custom HTML tags and apply custom styles not supported by standard HTML tags and CSS styles. Thus, it is possible to insert headers, footers, comments, table of contents, cross-references, sections and many other contents. All integrated with the supported HTML tags and CSS styles.
E.g.: you can insert a TOC with headings generated with HTML tags (H1, H1 and H3) or through a custom tag:
Headers and footers:
And just almost any other content:
The flexibility of this feature also allows to customize the tags for each supported content, e.g., to define "header" and "footer" as tags to create headers and footers, instead of the default tags phpdocx_header and phpdocx_footer:
HTML Extended and CSS Extended also have an extra set of methods and options designed to make conversions more flexible and efficient:
- addBaseCSS: defines a base CSS for all HTML conversions.
- stylesReplacementType: available for the replaceVariableByHTML method, allows using the original placeholder styles (pPr y rPr) instead of the ones defined by the imported HTML and CSS or mix template placeholder and HTML/CSS styles.
- support overwriting styles in level lists: create lists with different styles in the same levels.
- extraHTMLExtendedOptions: transform additional contents just like tabs.
- embed TTF fonts using @font-face rules.