mPDF Manual – mPDF functions

WriteHTML()

(mPDF ≥ 1.0)

WriteHTML — Write HTML code to the document

Description

void WriteHTML ( string $html [, int $mode [, boolean $initialise [, boolean $close ]]])

Write $html code to the document.

Parameters

$html

UTF-8 encoded HTML code to write to the document.

$mode

Controls what parts of the $html code is parsed. Use class constants from \Mpdf\HTMLParserMode for better readability and understanding.

Values

  • \Mpdf\HTMLParserMode::DEFAULT_MODE - Parses a whole $html document
  • \Mpdf\HTMLParserMode::HEADER_CSS - Parses the $html as styles and stylesheets only
  • \Mpdf\HTMLParserMode::HTML_BODY - Parses the $html as output elements only
  • \Mpdf\HTMLParserMode::HTML_PARSE_NO_WRITE - (For internal use only - parses the $html code without writing to document)
  • \Mpdf\HTMLParserMode::HTML_HEADER_BUFFER - (For internal use only - writes the $html code to a buffer)

Default: \Mpdf\HTMLParserMode::DEFAULT_MODE

Values:

\Mpdf\HTMLParserMode::DEFAULT_MODE:

Metadata:

  • title is read from <title>...</title> tags
  • subject, keywords and author are read from <meta ...

Charset:

  • if $allow_charset_conversion = true and a charset = ... statement is present, mPDF will attempt to convert all the following HTML input from the specified charset to UTF-8

CSS styles:

  • any CSS found between <style>...</style> tags
  • stylesheets specified by @import url(*.css)
  • stylesheets specified by <link rel="stylesheet" href=""

NB Stylesheets with media="all" or media ="screen" will always be parsed.

Anything between <style> tags is then discarded.

If <body> tags are found, all $html outside these tags are discarded, and the rest is parsed as content for the document.

If no <body> tags are found, all remaining $html is parsed as content.

\Mpdf\HTMLParserMode::HEADER_CSS:

The html input is only parsed as CSS style information only.

The code does not have to be surrounded by <style> tags, so you can pass the contents of a stylesheet directly - see Example #1.

\Mpdf\HTMLParserMode::HTML_BODY:

If <body> tags are found, all $html outside these tags are discarded, and the rest is parsed as content for the document.

If no <body> tags are found, all $html is parsed as content.

Prior to mPDF 4.2 the default CSS was not parsed when using $mode = \Mpdf\HTMLParserMode::HEADER_BODY.

$initialise

Set true or false to determine whether to initialise all buffers, starting all HTML elements from new. See example #2 for use. You must start with a WriteHTML() that calls $initialise = true.

Default: true

$close

Set true or false to specify whether all HTML elements are closed, and buffers cleared. See example #2 for use. You must end with a WriteHTML() that calls $close = true.

Default: true

Changelog

Version Description
2.0 Using WriteHTML without the $mode parameter no longer clears any CSS styles already imported.
2.1 Parameters $initialise and $close introduced.
4.2

Accepts null string as parameter without error.

Parses default CSS when using $mode as 2

Examples

Example #1

<?php
$mpdf = new \Mpdf\Mpdf();

$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet, \Mpdf\HTMLParserMode::HEADER_CSS);

$mpdf->WriteHTML('Hello World', \Mpdf\HTMLParserMode::HTML_BODY);

$mpdf->Output();

Example #2

<?php
// You can write parts of HTML elements by using the initialise and close parameters:

$mpdf->WriteHTML('This is the beginning...', \Mpdf\HTMLParserMode::HTML_BODY, true, false);
$mpdf->WriteHTML('...this is the middle...', \Mpdf\HTMLParserMode::HTML_BODY, false, false);
$mpdf->WriteHTML('...and this is the end', \Mpdf\HTMLParserMode::HTML_BODY, false, true);

See Also

Fork me on GitHub