Skip to main content

Converting HTML to PDF

Version: 3.0

HTML to PDF Conversion

Use the Converter.Convert static method to convert HTML from a URI or an HTML text string. HTML Converter supports converting HTML from a web URL, a local file URI, or directly from HTML markup provided as a string. Several overloads of the method are available, including overloads that save the converted PDF to a file or return the generated PDF as a byte array.

tip

Consider using Converter.ConvertAsync when converting a large number of HTML documents or when integrating HTML Converter into applications that use parallel, asynchronous, or non-blocking processing workflows.

Converter.Convert​

Convert HTML to PDF using one of the Converter.Convert method overloads.


* [public static void Convert(string inputHtml, string outputPath, [Uri basePath = null], [ConversionOptions conversionOptions = null])](cete.dynamicpdf.htmlconverter.converter#method-convert-String_String_Uri_ConversionOptions_)
* [public static Byte[] Convert(string inputHtml, [Uri basePath = null], [ConversionOptions conversionOptions = null])](cete.dynamicpdf.htmlconverter.converter#method-convert-String_Uri_ConversionOptions_)
* [public static Byte[] Convert(Uri uri, [ConversionOptions conversionOptions = null])](cete.dynamicpdf.htmlconverter.converter#method-convert-Uri_ConversionOptions_)
* [public static void Convert(Uri uri, string outputPath, [ConversionOptions conversionOptions = null])](cete.dynamicpdf.htmlconverter.converter#method-convert-Uri_String_ConversionOptions_)


* [Shared Sub Convert(inputHtml As String, outputPath As String, Optional basePath As Uri = null, Optional conversionOptions As ConversionOptions = null](cete.dynamicpdf.htmlconverter.converter#method-convert-String_String_Uri_ConversionOptions_)
* [Shared Function Convert(inputHtml As String, Optional basePath As Uri = null, Optional conversionOptions As ConversionOptions = null) As Byte()](cete.dynamicpdf.htmlconverter.converter#method-convert-String_Uri_ConversionOptions_)
* [Shared Function Convert(uri As Uri, Optional conversionOptions As ConversionOptions = null) As Byte()](cete.dynamicpdf.htmlconverter.converter#method-convert-Uri_ConversionOptions_)
* [Shared Sub Convert(uri As Uri, outputPath As String, Optional conversionOptions As ConversionOptions = null)](cete.dynamicpdf.htmlconverter.converter#method-convert-Uri_String_ConversionOptions_)

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

Simple Example​

The following example illustrates converting a web URL to a PDF document.

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

[SimpleConversion.cs](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-cs/SimpleConversion.cs){#gitlink-id target=_blank}


[SimpleConversion.vb](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-vb/SimpleConversion.vb){#gitlink-id target=_blank}

HTML String to PDF 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)

[HtmlConversionUsingString.cs](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-cs/HtmlConversionUsingString.cs){#gitlink-id target=_blank}


[HtmlConversionUsingString.vb](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-vb/HtmlConversionUsingString.vb){#gitlink-id target=_blank}

^^^ Figure 1. Creating PDF from HTML string. ^^^

URI to PDF Example​

The following example illustrates converting a web URL or a local HTML document using a URI.

Uri uri = new Uri(new Uri("file://"), inputPath);
Converter.Convert(uri, outputPath);
Dim uri As New Uri(New Uri("file://"),inputPath)
Converter.Convert(uri, outputPath & "/simple_one.pdf")

[SimpleConversion2.cs](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-cs/SimpleConversion2.cs){#gitlink-id target=_blank}


[SimpleConversion2.vb](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-vb/SimpleConversion2.vb){#gitlink-id target=_blank}

string html = "<html><body><h2>Hello DynamicPDF HTML Converter</h2></body></html>";
Converter.Convert(html, outputPath);
Dim html As String = "<html><body><h2>Hello DynamicPDF HTML Converter</h2></body></html>"
Converter.Convert(html, outputPath & "/simple_two.pdf")

[SimpleConversion2.cs](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-cs/SimpleConversion2.cs){#gitlink-id target=_blank}


[SimpleConversion2.vb](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-vb/SimpleConversion2.vb){#gitlink-id target=_blank}

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.

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)

[ConvertToByteArray.cs](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-cs/ConvertToByteArray.cs){#gitlink-id target=_blank}


[ConvertToByteArray.vb](https://github.com/DynamicPDF/html-converter-dotnet-core/blob/master/html-converter-dotnet-core-vb/ConvertToByteArray.vb){#gitlink-id target=_blank}