embedHTML

Inserts HTML content into the Word document.

PRO, PRO+ and CORPORATE PHPDocX >= 2.5

Description

public embedHTML (string $html [, array $options])

This method allows for the insertion of HTML into the current Word document.

Practically all HTML tags and CSS styles are supported.

This method transforms HTML directly into WordML and it is compatible with OpenOffice and PDF conversion.

You may find a more detailed explanation of this useful method in the HTML to Word section of the API documentation.

Parameters

html

The html code to be translated into WordML: it could be a string or the path to a file.

options

The posible keys and values are:

keyTypeDescription
isFile bool True for files and false for strings.
baseURL string The base URL used to complete the relative paths of links and images.
downloadImages bool If true inserts the images into the docx document, otherwise just links them as an external source.
filter string Only renders the firltered contents. It could be an string denoting an id (’#myId’), a class (’.myClass’), a HTML tag (’‘) or a valid XPath expression (’//expression’).
parseAnchors bool If true parses the anchors included in the HTML code.
parseDivsAsPs bool If true parses the div elements as paragraphs.
parseFloats bool If true preserves the floating properties of images and tables.
strictWordStyles bool If true ignores all CSS styles and uses the styles set via the wordStyles option (see next).
wordStyles array One may associate different Word styles to HTML classes, ids or tags. For example: array(’.myClass’ => ‘LightGridPHPDOCX’, ‘#myId’ => ‘LightListAccent6PHPDOCX’, ‘‘ => ‘PHPDOCXHeading1′).
target string Document (default value), defaultHeader, firstHeader, evenHeader, defaultFooter, firstFooter, evenFooter.
rawWordML bool Allows to save the WordML output for its later insertion, for example, within a table or a header/footer.

Return values

If the option rawWordML is set to true (default value is false) it returns a string with the raw WordML code, else returns nothing.

Code samples

Example #1: inserts a table written as HTML code


require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();
$html= '

And now we draw a table with border and rawspans and colspans:

'; $html.= ''; $html.=''; $html.=''; $html.=''; $html.= '
1_11_2
Some random text.
  • One
  • Two and a half
3_23_33_3
'; $docx->embedHTML($html); $docx->createDocx(example_embedSimpleHTML);

The resulting Word document looks like (download .docx file):

Example #2: generates a Word document with the contents of a Wikipedia article


require_once '../../classes/CreateDocx.inc';

$docx = new CreateDocx();

$docx->embedHTML('

The Wikipedia Article on the Periodic Table

'); $html = file_get_contents('http://en.wikipedia.org/wiki/Periodic_table'); $docx->embedHTML($html, array('parseDivsAsPs' => true, 'baseURL' => 'http://en.wikipedia.org/', 'downloadImages' => true, 'filter' => '#bodyContent')); $docx->modifyPageLayout('A3'); $docx->createDocx(example_ExternalHTML)

The resulting Word document looks like (download .docx file):

Change log

  • Available since 2.5
  • rawWordML option included in 3.0