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.

Values

  • 0 - Parses a whole $html document
  • 1 - Parses the $html as styles and stylesheets only
  • 2 - Parses the $html as output elements only
  • 3 - (For internal use only - parses the $html code without writing to document)
  • 4 - (For internal use only - writes the $html code to a buffer)

Default: 0

Mode #0 (Default)

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.

Mode #1

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.

Mode #2

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 = 2.

$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, 1);

$mpdf->WriteHTML('Hello World', 2);

$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...', 2, true, false);
$mpdf->WriteHTML('...this is the middle...', 2, false, false);
$mpdf->WriteHTML('...and this is the end', 2, false, true);

See Also

Fork me on GitHub