HtmlLayout
The HtmlLayout class renders HTML to a PDF document. The class supports HTML 4.0.1 and CSS 2.1 (with a few limitations - see below).
Do not confuse HtmlLayout with the HtmlArea class. The HtmlArea class is intended for adding areas of HTML to a page. The HtmlLayout class adds HTML to an entire document.
An HtmlLayout class has six constructors that allow inputting HTML from a steam, text, or URI.
public HtmlLayout(Stream stream, PageInfo pageInfo)
public HtmlLayout(Stream stream, Uri baseRef, PageInfo pageInfo)
public HtmlLayout(string text, PageInfo pageInfo)
public HtmlLayout(string text, Uri baseRef, PageInfo pageInfo)
public HtmlLayout(Uri uri, PageInfo pageInfo)
public HtmlLayout(Uri uri, Uri baseRef, PageInfo pageInfo)
The class also has numerous properties for customizing the document's appearance, including adding headers and footers and modifying margins.
If working with complex HTML documents, then consider using either DynamicPDF HTML Converter for .NET or DynamicPDF Converter for .NET
PageInfo
All overloaded constructors for the HtmlLayout class takes a PageInfo instance as a parameter. This class has four overloaded constructors for initializing an html layout.
public PageInfo(PageSize size, PageOrientation orientation)
public PageInfo(PdfPage inputPage)
public PageInfo(float width, float height)
public PageInfo(string filePath, int pageNumber)
BaseRef
If the HTML contains relative paths, then the HtmlLayout must set a base url. This property specifies the base URL for all relative URLs in the HTML text. For example, the HTML:
<p>
<img href="../images/logo.png"
</p>
would require setting the BaseRef
to the following: htmLayoutInstance.BaseRef = "https://www.dynamicpdf.com";
.
Example
The following example demonstrates using the HtmlLayout class with simple header and footer text displayed on the created PDF.
PageInfo layoutPage = new PageInfo(PageSize.A4, PageOrientation.Portrait);
string txt = File.ReadAllText("example.html");
HtmlLayout html = new HtmlLayout(txt, layoutPage);
html.Header.Center.Text = "%%PR%%%%SP%% of %%ST%%";
html.Header.Center.HasPageNumbers = true;
html.Header.Center.Width = 200;
html.Footer.Center.Text = "%%PR%%%%SP(A)%% of %%ST(B)%%";
html.Footer.Center.HasPageNumbers = true;
html.Footer.Center.Width = 200;
Document document = html.Layout();
document.Draw(outputPath);
'Create Html layout page info
Dim layoutPage As PageInfo = New PageInfo(PageSize.A4, PageOrientation.Portrait)
'Create Uri
Dim uri As Uri = New Uri("example.html")
'Create a Html Layout
Dim html As HtmlLayout = New HtmlLayout(uri, layoutPage)
'Create a header
html.Header.Center.Text = "%%PR%%%%SP%% of %%ST%%"
html.Header.Center.HasPageNumbers = True
html.Header.Center.Width = 200
'Create a footer
html.Footer.Center.Text = "%%PR%%%%SP(A)%% of %%ST(B)%%"
html.Footer.Center.HasPageNumbers = True
html.Footer.Center.Width = 200
'Create a PDF Document
Dim document As Document = html.Layout
'Save the PDF
document.Draw(outputPath)
HTML & CSS Support
DynamicPDF Core Suite supports the following HTML tags and CSS properties.
HTML Tags | CSS |
---|---|
a address blockquote center div img br hr h1 h2 h3 h4 h5 h6 p pre dd dl dt li ol ul table b i q u em tt big del dfn ins kbd sub sup var abbr cite code font samp span small s strike strong input textarea label button fieldset legend select acronym |
background color text-decoration letter-spacing text-align text-indent text-transform word spacing font border margin padding height min-height max-height width min-width max-width line-height position clip top right bottom left z-index list-style border-collapse border-spacing caption-side empty-cells table-layout clear display float visibility page-break-after page-break-before |
Character Encoding
The HTML passed to an HtmlLayout uses the following character encodings.
- The encoding specified in the HTML file header is used when the HTML source is an HTML file. If that is not present, the HTML meta charset is used.
- The character encoding specified in the HTML Meta charset converts an HTML string.
- UTF-8 is the default encoding used by the HTML Area when there is no meta charset or file encoding specified.
Limitations
Supported tags and properties are based on HTML 4.0.1 and CSS 2.1 specifications. Refer to the Product Limitations documentation for a complete listing of limitations.