Converting HTML to PDFs

Converting HTML to PDF is straightforward using the DynamicPDF Converter for .NET.

HtmlConverter Class

HTML conversion is accomplished using the HtmlConverter class. This class can convert HTML strings, files, and URIs to PDF documents (see the API for a list of constructors).

The following two examples illustrate using a URI followed by using an HTML string.

HtmlConverter htmlConverter = new HtmlConverter(new Uri("http://www.google.com"));
htmlConverter.Convert(outputPdf);
Dim htmlConverter As New HtmlConverter(New Uri("http://www.google.com"))
htmlConverter.Convert(outputPdf)
string html = "<html><body><p>This is a very simple HTML" 
	+ "string including a table.</p>"
	+ "<h4>Two rows and three columns:</h4>"
	+ "<table border=\"1\"><tr><td>100</td>"
	+ "<td>200</td><td>300</td></tr><tr><td>"
	+ "400</td><td>500</td><td>600</td></tr>"
	+ "</table></body></html>";
htmlConverter htmlConverter = new(html);
htmlConverter.Convert(outputPdf);
Dim html As String = "<html><body><p>This is a very simple HTML" & "string including a table.</p>" 
	& "<h4>Two rows and three columns:</h4>" 
	& "<table border=""1""><tr><td>100</td>" 
	& "<td>200</td><td>300</td></tr><tr><td>" 
	& "400</td><td>500</td><td>600</td></tr>" 
	& "</table></body></html>"
Dim htmlConverter As New HtmlConverter(html)
htmlConverter.Convert(outputPdf)

HtmlConversionOptions Class

The HtmlConversionOptions class can also take conversion options to specify how to format the created PDF. Conversion option properties include formatting the created PDF's dimensions, margins, and metadata.

string html = "<html><body><p>Very simple HTML."
        + "</p></body></html>";
HtmlConversionOptions options = 
    new(PageSize.A4, PageOrientation.Landscape, 36);
HtmlConverter converter = new(html, options);
converter.Convert(outputPdf);
Dim html As String = "<html><body><p>Very simple HTML" 
	& "</p></body></html>"

Dim conversionOptions As New HtmlConversionOptions(PageSize.A4, PageOrientation.Landscape, 36)

Dim htmlConverter As New HtmlConverter(html, conversionOptions)
htmlConverter.Convert(outputPdf)

In this topic