How to fix the ThreadStateException (ActiveX control cannot be instantiated) exception when converting HTML to PDF?
In the HTML to PDF conversion process, the HTMLConverter throws an exception with the message "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."
The HTMLConverter internally makes use of the MSHTML.dll that can run only in the STA mode. Running the application in the MTA mode leads to this exception.
This can be fixed by any of the following ways:
- In the ASP.NET applications, add 'AspCompat=true' in the @page directive.
- In ASP.NET MVC, windows or web services and console applications make use of the following code snippet:
[C#] Thread t = new Thread(ConvertToPDF); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); private void ConvertToPDF() { // Convert PDF here }
[C#] private void ConvertToPDF() { // Convert PDF here using IE based converter }
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about How to fix the ThreadStateException (ActiveX control cannot be instantiated) exception when converting HTML to PDF.
You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!