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.
Warning: mPDF is not meant to receive HMTL/CSS from an outside user.
All user input passed to mPDF should be vetted and sanitized properly above the level
of standard browser-level sanitization (such is htmlspecialchars).
Note: Prior to mPDF 4.2 a fatal error was caused if
$html was passed as a null value,
false or an undefined variable.
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\HTMLParserModefor 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_MODEValues:
\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 = trueand acharset = ...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"ormedia ="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
trueorfalseto determine whether to initialise all buffers, starting all HTML elements from new. See example #2 for use. You must start with aWriteHTML()that calls$initialise = true.Default:
true - $close
-
Set
trueorfalseto specify whether all HTML elements are closed, and buffers cleared. See example #2 for use. You must end with aWriteHTML()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 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
- $allow_charset_conversion - attempts to read any charset declaration in the HTML code
- $ignore_invalid_utf8 - prevents mPDF from failing if text contains invalid UTF-8 characters
- $charset_in - specify the input text character set if not UTF-8
- $biDirectional - specify whether mPDF should test for RTL text
- $allow_html_optional_endtags - specify whether mPDF should try to accommodate optional HTML endtags
- $restoreBlockPagebreaks - keep current HTML tags/CSS styles active when forcing a page-break or formfeed