Conversion using IE Rendering
6 Dec 20247 minutes to read
Essential® PDF makes use of the Microsoft MSHTML library to convert HTML pages to PDF. The output would like how it is viewed in the Internet Explorer browser.
NOTE
Starting with v20.1.0.x, if you reference Syncfusion® HTML converter assemblies from trial setup or from the NuGet feed, you also have to include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Prerequisites
To use the IE rendering engine in the application, the following assemblies or NuGet packages needs to be added as reference to the project.
Assemblies
- Syncfusion.Compression.Base.dll
- Syncfusion.Pdf.Base.dll
- Syncfusion.HtmlConverter.Base.dll
- Microsoft.mshtml.dll
NuGet
Platform(s) | NuGet Package |
---|---|
Windows Forms | |
WPF | |
ASP.NET | |
ASP.NET MVC4 | |
ASP.NET MVC5 |
NOTE
The above mentioned NuGet packages are available in nuget.org
Converting the URL to a PDF document
To convert the HTTP or HTTPS website to PDF, use the following the code snippet.
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert HTML to PDF document
PdfDocument document = htmlConverter.Convert("https://www.google.com");
//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'Convert HTML to PDF document
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Converting the HTML string to PDF document
To convert the HTML string to PDF, use the following the code snippet.
baseURL: path of the resources (images, style sheets, scripts.,) used in the input HTML string.
For the below HTML string, the baseURL will be the path of the syncfusion_logo.gif image.
For example, if the above image is in “C:\Temp\HTMLFiles\syncfusion_logo.gif” location then the baseURL will be as below,
baseURL: C:\Temp\HTMLFiles\
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//HTML string and base URL
string htmlText = "<html><body><img src=\"syncfusion_logo.gif\" alt=\"Syncfusion_logo\" width=\"200\" height=\"70\"><p> Hello World</p></body></html>";
string baseUrl = @"C:/Temp/HTMLFiles/";
//Convert HTML to PDF document
PdfDocument document = htmlConverter.Convert(htmlText, baseUrl);
//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'HTML string and base URL
Dim htmlText As String = "<html><body><img src=""syncfusion_logo.gif"" alt=""Syncfusion_logo"" width=""200"" height=""70""><p> Hello World</p></body></html>"
Dim baseUrl As String = "C:/Temp/HTMLFiles/"
'Convert HTML to PDF document
Dim document As PdfDocument = htmlConverter.Convert(htmlText, baseUrl)
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<body>
<img src="syncfusion_logo.gif" alt="Syncfusion_logo" width="200" height="70">
<p> Hello World</p>
</body>
</html>
You can download a complete working sample from GitHub.
Converting Windows Authenticated web page to PDF document
The webpage you want to convert may protected with windows authentication. IE rendering engine provides support for converting the Windows Authenticated webpage to PDF document by providing the username and password. Refer to the following code snippet,
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//IE Converter settings
IEConverterSettings converterSettings = new IEConverterSettings();
converterSettings.Username = "username";
converterSettings.Password = "password";
htmlConverter.ConverterSettings = converterSettings;
//Convert HTML to PDF document
PdfDocument document = htmlConverter.Convert("https://www.google.com");
//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'IE Converter settings
Dim converterSettings As New IEConverterSettings()
converterSettings.Username = "username"
converterSettings.Password = "password"
htmlConverter.ConverterSettings = converterSettings
'Convert HTML to PDF document
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Converting with PDFA conformance
IE HTML to PDF converter provides support for converting the web pages to PDF with PDFA1B conformance, which embeds all the fonts into the PDF document. The following code snippet illustrates how to convert HTML pages to PDF document with PDFA1B conformance.
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//IE Converter settings
IEConverterSettings converterSettings = new IEConverterSettings();
//PDFA1B conformance
converterSettings.IsPDFA1B = true;
htmlConverter.ConverterSettings = converterSettings;
//Convert HTML to PDF document
PdfDocument document = htmlConverter.Convert("https://www.google.com");
//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As New HtmlToPdfConverter()
'IE Converter settings
Dim converterSettings As New IEConverterSettings()
'PDFA1B conformance
converterSettings.IsPDFA1B = True
htmlConverter.ConverterSettings = converterSettings
'Convert HTML to PDF document
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
You can download a complete working sample from GitHub.
Troubleshooting
Issue |
The following conditions may occur while converting HTML to PDF by using the IE rendering engine.
|
||||||
---|---|---|---|---|---|---|---|
Solution |
The above issues may occur in the machines with IE9 or later versions installed. As the Internet Explorer version 9 and above supports hardware acceleration, the rendered content would be in the form of Bitmap, where many features are not supported. To overcome this issue, the key FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI should be updated in the registry as explained in the link below. http://msdn.microsoft.com/en-us/library/ee330732(v=vs.85).aspx#iviewobject_draw
|
Issue | Images or other contents in the HTML are missing in the resultant PDF document |
---|---|
Solution | The issue may be due to the slow Internet connection or due to the behavior that the conversion completed before the URL is loaded completely. To overcome this issue, add suitable delay to the conversion using AdditionalDelay property of the HTMLConverter. |
HTML conversion support in Azure | |
---|---|
Azure App Service | No |
Azure Functions | No |
Azure Cloud Service | Yes |