When does COM Exception arise while converting HTML to PDF in Azure?
The HTML to PDF converter makes use of mshtml.dll (IE Rendering Engine) to convert HTML to Vector Images and then the Vector Images are converted to PDF document. So, while hosting in Azure, when the mshtml.dll is not present, then COM exception occurs. To avoid that, refer to this assembly manually and set the CopyLocal to true by using the following code in csproj file.
.csproj
<Reference Include="Microsoft.mshtml, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <Private>True</Private> </Reference> |
As IE rendering engine needs elevated permission, it can be hosted only as Cloud service and not as website since Azure websites only have limited permissions.
The COM error may occur when the instances are not properly disposed. The following changes are needed:
- Proceed with the HTMLConversion within using statement.
- Dispose the PdfDocument instance after conversion.
C#
//using statement while using HtmlConverter. using (HtmlConverter html = new HtmlConverter()) { //Conversion goes here. } // Save PDF document. doc.Save("Sample.pdf", Response, HttpReadType.Open); //Dispose the object completely. doc.Close(true);