Main Content

mlreportgen.utils.html2dom.prepHTMLString

Prepare HTML string for conversion to DOM

Description

example

preppedHTMLStr = mlreportgen.utils.html2dom.prepHTMLString(htmlStr) prepares the HTML in the string specified by htmlStr for conversion to the MATLAB® Report Generator™ internal document object model (DOM). The prepared HTML in preppedHTMLStr can be converted to a DOM API representation by using an mlreportgen.dom.HTML object. The mlreportgen.utils.html2dom.prepHTMLString function:

  • Corrects invalid markup by calling mlreportgen.utils.tidy with the settings for HTML output.

  • Uses the MATLAB web browser to convert the tidied markup to an HTML DOM document. See https://www.w3.org/TR/WD-DOM/introduction.html.

    The MATLAB web browser computes the CSS properties of the elements in the HTML input based on internal and external style sheets specified by the input HTML, and on the style attribute of an element. The CSS property computation supports all valid CSS style sheet selectors, including selectors not directly supported by mlreportgen.dom.HTML objects.

  • Converts the HTML DOM document to HTML markup that is supported by mlreportgen.dom.HTML objects. The style attribute for each element specifies the element CSS properties that the MATLAB web browser computed.

  • Returns the prepared HTML as a string scalar.

preppedHTMLStr = mlreportgen.utils.html2dom.prepHTMLString(htmlStr,"Tidy",false) prepares the input HTML without first tidying it. Use this syntax if you want to tidy the HTML markup yourself. For example, you might want to call mlreportgen.utils.tidy with different options than the ones used by mlreportgen.utils.html2dom.prepHTMLString, then pass the tidied HTML as the input to mlreportgen.utils.html2dom.prepHTMLString.

Examples

collapse all

Use mlreportgen.utils.html2dom.prepHTML to prepare an HTML string for conversion to a DOM object that you can append to a report.

Create a string named myHTMLStr that has this HTML content:

myHTMLStr = "<html><body><p>This is the first paragraph.<p>This is the second paragraph.<p></body></html>";

The paragraph is missing the / in the end tag </p>.

Try to convert the HTML to a DOM object and append the object to a report.

import mlreportgen.dom.*; 
rpt = Document("MyReport","docx"); 
htmlObj = mlreportgen.dom.HTML(myHTMLStr);
append(rpt,htmlObj); 
close(rpt); 
rptview(rpt);
Error using mlreportgen.dom.HTML
HTML error: expected end of tag 'p'

mlreportgen.dom.HTML ends with an error due to the missing end tag.

Prepare the HTML by using mlreportgen.utils.html2dom.prepHTMLString. Create an mlreportgen.dom.HTML object from the prepared HTML and append the object to the report.

import mlreportgen.dom.*; 
rpt = Document("MyReport","docx");
%prepare the HTML 
myPreppedHTML = mlreportgen.utils.html2dom.prepHTMLString(myHTMLStr);
htmlObj = mlreportgen.dom.HTML(myPreppedHTML);
append(rpt,htmlObj); 
close(rpt); 
rptview(rpt);

Input Arguments

collapse all

HTML content to be prepared for conversion to the DOM, specified as a character vector or string scalar.

Output Arguments

collapse all

Prepared HTML, returned as a string scalar.

Tips

  • MATLAB Report Generator mlreportgen.dom.HTML or mlreportgen.dom.HTMLFile objects typically cannot accept the raw HTML output of third-party applications, such as Microsoft® Word, that export native documents as HTML markup. In these cases, your Report API report generation program can use the mlreportgen.utils.html2dom.prepHTMLString and mlreportgen.utils.html2dom.prepHTMLFile functions to prepare the raw HTML for use with the mlreportgen.dom.HTML or mlreportgen.dom.HTMLFile objects. Typically, your program will have to further process the prepared HTML to remove valid but undesirable objects, such as line feeds that were in the raw content.

Introduced in R2020a