HTML to PDF Conversion

Use the Converter.Convert static method to convert HTML from a URI or a text string. DynamicPDF HTML Converter supports converting HTML from a URI (URL or a local file) or from a text string. This method also supports returning a byte array rather than specifying an output path.

URI to PDF

Converting HTML to PDF using a URL or local file requires passing a URI and an output path to save the converted PDF document. The method also allows passing optional conversion options.

public static void Convert(Uri uri, string outputPath, [ConversionOptions conversionOptions = null])
Public Shared Sub Convert(uri As Uri, outputPath As String, Optional conversionOptions As ConversionOptions = Nothing)

Refer to the ConversionOptions documentation for more information on providing conversion options.

Converting a URI to a PDF is straightforward, as the following C# and VB .NET examples illustrate.

Example

The following example illustrates converting a URL to a PDF document using HTML Converter.

Converter.Convert(new Uri("https://www.google.com"), "SimpleConversion.pdf");
Converter.Convert(New Uri("https://www.google.com"), "SimpleConversion.pdf")

HTML String to PDF

Converting an HTML document to PDF using HTML from a string is also straightforward. The method takes an input string of HTML, an output path, an optional base path, and optional conversion options.

public static void Convert(string inputHtml, string
outputPath, [Uri basePath = null], 
[ConversionOptions conversionOptions = null])
Public Shared Sub Convert(inputHtml As String, outputPath As String, Optional basePath As Uri = Nothing, Optional conversionOptions As ConversionOptions = Nothing)

Example

The following example illustrate converting an HTML string to a PDF.

string sampleHtml = "<html><body><p>This is a very simple HTML string including a Table below.</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>";

Converter.Convert(sampleHtml, outputPath);
Dim sampleHtml As String = "<html><body><p>This is a very simple HTML string including a Table below.</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>"
                        
Converter.Convert(htmlString, outputPath)

Figure 1. Creating PDF from HTML string.

Converting to a Byte Array

Using the Converter.Convert static method to return a byte array rather than specifying an output file is straightforward. The input can either be a URI or a string.

Example

The following example inputs a URI and returns a byte array.

byte[] pdfByteArray = Converter.Convert(new Uri("https://www.google.com"));
File.WriteAllBytes(outputPath), pdfByteArray);
Dim document As Uri = New Uri("https://google.com")
Dim output As Byte() = Converter.Convert(document)
File.WriteAllBytes(outputPath, PdfByteArray)

In this topic