HTML Extended and CSS Extended

HTML Extended and CSS Extended

Introduction

Thanks to HTML Extended and CSS Extended functionality, it is possible to invoke phpdocx methods, styles and options not available through standard HTML tags or CSS styles. Thus, you can add contents such as headers, footers, comments, table of contents, cross-references and many other contents types, insert new sections, access properties and styles not available in CSS and even add WordFragments.

This feature, available for Premium licenses, integrates with the embedHTML and replaceVariableByHTML methods with a set of custom tags that can be combined even with other HTML and CSS contents.

HTML Extended and CSS Extended 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, it allows using the original placeholder styles (pPr y rPr) instead of the ones defined by the imported HTML and CSS, and mix HTML and placeholder styles.
  • embedFonts: embeds TTF fonts from @font-face rules.
  • support overwriting styles in level lists: create lists with different styles in the same levels.
  • extraHTMLExtendedOptions: transform additional contents just like tabs.

The HTML API documentation includes an exhaustive list of tags and styles included in HTML Extended and CSS Extended.

Supported contents

Contents

This is the complete list of supported contents and their default tags:

  • addBookmark: phpdocx_bookmark
  • addBreak: phpdocx_break
  • addComment: phpdocx_comment
  • addCrossReference: phpdocx_crossreference
  • addDateAndHour: phpdocx_dateandhour
  • addEndnote: phpdocx_endnote
  • addFooter: phpdocx_footer
  • addFootnote: phpdocx_footnote
  • addFormElement: phpdocx_formelement
  • addHeader: phpdocx_header
  • addHeading: phpdocx_heading
  • addImage: phpdocx_image
  • addLink: phpdocx_link
  • addMathEquation: phpdocx_mathequation
  • addMergeField: phpdocx_mergefield
  • addOle: phpdocx_ole
  • addOnlineVideo: phpdocx_onlinevideo
  • addPageNumber: phpdocx_pagenumber
  • addSection: phpdocx_section
  • addShape: phpdocx_shape
  • addSimpleField: phpdocx_simplefield
  • addStructuredDocumentTag: phpdocx_structureddocumenttag
  • addTableContents: phpdocx_tablecontents
  • addTableFigures: phpdocx_tablefigures
  • addText: phpdocx_text
  • addTextBox: phpdocx_textbox
  • addWordFragment: phpdocx_wordfragment
  • addWordML: phpdocx_wordml
  • math: MathML equation
  • meta: document properties
  • modifyPageLayout: phpdocx_modifypagelayout
  • title: document title

As explained further on, almost all tags can be customized.

Tag types

Tags are divided in two types: inline and block.

The inline ones do not admit children elements. They are the following:

  • meta
  • phpdocx_bookmark
  • phpdocx_break
  • phpdocx_crossreference
  • phpdocx_dateandhour
  • phpdocx_formelement
  • phpdocx_heading
  • phpdocx_image
  • phpdocx_link
  • phpdocx_mathequation
  • phpdocx_mergefield
  • phpdocx_ole
  • phpdocx_onlinevideo
  • phpdocx_pagenumber
  • phpdocx_section
  • phpdocx_shape
  • phpdocx_simplefield
  • phpdocx_structureddocumenttag
  • phpdocx_tablecontents
  • phpdocx_text
  • phpdocx_textbox
  • phpdocx_wordfragment
  • phpdocx_wordml
  • title

The block ones allow children, either of the HTML or the HTML Extended tags. They are the following:

  • math
  • phpdocx_comment
  • phpdocx_endnote
  • phpdocx_footer
  • phpdocx_footnote
  • phpdocx_header

Options for each method are added with custom data attributes. You just need to write the option name after the prefix data-. E.g.: data-bold="true", data-type="start" o data-type="default".

Supported custom attributes and styles

Custom styles and custom attributes

In addition to the previous methods and styles, CSS Extended supports custom styles and attributes in HTML standard tags:

  • font-face: embed a TTF font
  • border-spacing: paragraph border spacing
  • data-depth: force a level in a li tag
  • data-font-size: "initial" avoids applying a font size
  • data-em: emphasis mark type
  • data-heading-level: set outline level to contents without using heading tags
  • data-highlight: highlight style
  • data-label: set a custom label to apply to captions
  • data-lang: set a custom language: language-region (en-US)
  • data-listppr: paragraph style in numberings
  • data-listrpr: run-of-text style in numberings
  • data-ppr: paragraph style
  • data-rpr: run-of-text style
  • data-style: apply a custom character/paragraph/table style
  • data-tblpr: table style
  • data-trpr: row style
  • data-tcpr: cell style

Insert 	 to add tabs.

Examples

HTML Extended is as easy to use as standard HTML tags. It is just necessary to enable the useHTMLExtended option to start working with the custom tags and styles.

Insert two bookmarks and a paragraph:

Add a TOC based on headings created with HTML and phpdocx tags:

Place headers and footers:

Add comments, endnotes and footnotes:

As shown in this example, comments, endnotes and footnotes require a specific structure in order to correctly add all their content.

Apply custom styles:

How to customize the tags

Tags related to each method can be customized with two static variables: HTMLExtended::$tagsBlock and HTMLExtended::$tagsInline.

For example, to define "header" and "footer" as tags associated to addHeader and addFooter:

Set a base CSS

The addBaseCSS method inserts a group of CSS styles to be applied for every HTML import. This allows to convert HTML contents that share a common set of styles without the need of adding them after each embedHTML call. These base styles can be overwritten with new styles when importing HTML.

For example, the next code adds a base CSS to be applied to all paragraphs imported from HTML, then overwrites the style with embedHTML:

Replace placeholders keeping styles

The replaceVariableByHTML method replaces a placeholder by HTML. This procedure leads to the application of the styles imported from HTML and the removal of the original placeholder ones. Using the stylesReplacementType option, the applied styles can be changed:

  • usePlaceholderStyles: styles from the imported HTML are ignored.
  • mixPlaceholderStyles: mix placeholder styles, placeholder styles overwrite HTML styles with the same name. Used with stylesReplacementTypeIgnore, this option allows choosing the styles to be applied. The stylesReplacementTypeOverwrite option allows HTML styles to overwrite placeholder styles.

For example, the code that follows replaces VAR for HTML, preserving the styles defined in the placeholder template:

The following code mixes HTML and placeholder styles, ignoring the font size from the HTML:

List level override

With custom list styles, you may import HTML while applying different styles for each level.

The following code creates a list with three items and defines different list styles for the secondary sublevels:

Embed fonts

Enabling the embedFonts option, TTF fonts included in CSS styles using @font-face rules will be added to the DOCX as embedded fonts.

The following code embeds Lato-Regular.ttf in the DOCX and sets it as font family of the paragraph:

CSS Extended: inheritance

In addition to the set of styles that CSS Extended expands with regard to the standard styles, it also allows to define new styles for the descendant elements to inherit from their parent ones.

The static variable HTMLExtended::$cssExtendedStylesInherited allows to define and add new inherited styles.

Additional information
  • Section tags can be added only once for each embedHTML call.