We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

HtmlConverter and ASP.NET

I'm trying to create a "hello world" ASP.NET program to convert an existing html page to a pdf. Based on the sample code I've been able to find, I have this so far:


Dim doc As PdfDocument = New PdfDocument()
Dim page As PdfPage = doc.Pages.Add()
Dim html As New HtmlConverter
Dim img As System.Drawing.Image = html.ConvertToImage("http://www.mysite.com", ImageType.Bitmap)
Dim Image As PdfImage = New PdfBitmap(img)
Dim fmt As New PdfLayoutFormat
fmt.Break = PdfLayoutBreakType.FitPage
fmt.Layout = PdfLayoutType.Paginate
Image.Draw(page, New RectangleF(0, 0, 700, 1000), fmt)

But, the HtmlConverter class seems to require a reference to Windows.Forms and I get the error:

"ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."

Can the HtmlConverter class be used with ASP.NET??

bert

5 Replies

PJ Pravin Joshua D Syncfusion Team March 11, 2010 05:55 AM UTC

Hi Bert,

Thank you for using Essential PDF.

Yes. The HTMLConverter class can be used with Asp.net.Please make the following change in your web project.

Required Modification:

Include the attribute AspCompat="true" in the @Page directive.

Here is the code snippet

[Aspx]
<%@ Page Language="C#" AutoEventWireup="true" AspCompat="true" %>


Please try this and let us know if this helps.

Regards,
Pravin Joshua D


BS Bert Sirkin March 11, 2010 01:43 PM UTC

Thanks - that worked.

Although I can get a public static website to convert, I can't get one of our secured web sites to convert. I'm getting the following error:

"Error HRESULT E_FAIL has been returned from a call to a COM component."

Our code requires a valid server session - does the conversion process take place in the same server session?

bert


BS Bert Sirkin March 11, 2010 03:50 PM UTC

It appears that the error:

"Error HRESULT E_FAIL has been returned from a call to a COM component."

occurs when I try to pass the CONVERT method of the HtmlConverter class HTML instead of a URL. Once the error occurs, I have to shut down Visual Studio, otherwise the CONVERT method won't ever work again, even when passing a URL.

Is there anything special I need to do when I pass a pages HTML to the method? For what it's worth, I picked a simple static public webpage: http://www.w3schools.com/ for testing.

bert


BS Bert Sirkin March 11, 2010 06:12 PM UTC

One last thing - I've tried rendering the HTML two different ways - both with the same error:

==================================================
Dim imgHTML As System.Drawing.Image = Nothing
imgHTML = html.FromString(sHTML, "http://mysite.com", ImageType.Metafile, 1000, -1, AspectRatio.KeepWidth)
==================================================
Dim result As HtmlToPdfResult = Nothing
result = html.Convert(sSource, "http://mysite.com", ImageType.Metafile, 1000, -1, AspectRatio.KeepWidth)
==================================================

bert


PJ Pravin Joshua D Syncfusion Team March 18, 2010 06:40 AM UTC

Hi Bert,

We are unable to reproduce the issue.Could you please report these issue with your sample through Direct Trac Developer Support Systemhttps://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents because you can take the advantage of the expertise of a dedicated support engineer and a guaranteed response time and we hope you will take advantage of this system as well. If you have already reported, please ignore this.

Please let us know if you have any other questions.

Regards,
Pravin.






Loader.
Live Chat Icon For mobile
Up arrow icon