Prepare HTML for Conversion to DOM Objects
This example shows how to prepare HTML content before converting
it to the MATLAB®
Report Generator™ internal document object model (DOM). The example creates a CSS style
sheet and an HTML file that uses the style sheet. The example uses the mlreportgen.utils.html2dom.prepHTMLFile
function to prepare the HTML
file content so that it can be converted to an mlreportgen.dom.HTMLFile
object. The function tidies the HTML and
converts the HTML to markup that is supported by an
mlreportgen.dom.HTMLFile
object. See Prepare HTML Before Conversion. To prepare
HTML content that is in a string, use mlreportgen.utils.html2dom.prepHTMLString
.
Create a CSS style sheet, myCSS.css
, to specify that the
text in a paragraph is red.
p {
color: red;
}
Create a file, myHTML.html
, that contains this HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="myCSS.css" >
</head>
<body>
<p> Hello World</p>
</body>
</html>
The HTML cannot be used by a MATLAB
Report Generator DOM HTMLFile
object because the
link
element is not properly closed. The slash
(/
) before the closing angle bracket
(>
) is missing.
Try to convert the HTML to an mlreportgen.dom.HTMLFile
object.
import mlreportgen.dom.* d = Document('test','pdf'); htmlObj = HTMLFile('myHTML.html'); append(d,htmlObj); close(d); rptview(d);
Error using mlreportgen.dom.HTMLFile HTML error: expected end of tag 'link'
Tidy the HTML by using mlreportgen.utils.tidy
. This function does not format the HTML
elements using the formatting in the style
sheet.
import mlreportgen.dom.* import mlreportgen.utils.* d = Document('test','pdf'); tidy('myHTML.html'); htmlObj = HTMLFile('myHTML-tidied.html'); append(d,htmlObj); close(d); rptview(d);
In the generated report, the text is black, not red.
Use mlreportgen.utils.html2dom.prepHTMLFile
to tidy the HTML and
format the HTML elements according to the style sheet.
import mlreportgen.dom.* import mlreportgen.utils.html2dom.* d = Document('test','pdf'); preppedHTMLFile = prepHTMLFile('myHTML.html','mypreppedHTML.html'); htmlObj = HTMLFile(preppedHTMLFile); append(d,htmlObj); close(d); rptview(d);
In the generated report, the text is red.
Note
In the process of preparing HTML, the
mlreportgen.utils.html2dom.prepHTMLFile
and
mlreportgen.utils.html2dom.prepHTMLString
functions load the
HTML in a MATLAB web browser. See Prepare HTML Before Conversion.
Formatting that is not specified in the input HTML or style sheet is
determined by the browser. The input HTML in this example does not specify
the font family. Therefore, the font family is determined by the browser. To
render the text with a different font, you can specify the font in the input
style sheet or HTML before preparing the HTML conversion or use the report
generation formatting capabilities. See Report Formatting Approaches.
See Also
mlreportgen.utils.html2dom.prepHTMLFile
| mlreportgen.utils.html2dom.prepHTMLString
| mlreportgen.utils.tidy
| mlreportgen.dom.HTMLFile
| mlreportgen.dom.HTML