HTML to PDF on web

Hi,

I keep getting an error what ever I do, while trying to convert an html page.

The code I am using on my web project is:

 PdfDocument pdf = new PdfDocument();
            pdf.PageSettings.SetMargins(0);
            pdf.PageSettings.Orientation = PdfPageOrientation.Portrait;
            pdf.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle0;
            PdfPage page = pdf.Pages.Add();
            PdfUnitConvertor converter = new PdfUnitConvertor();
            float width = converter.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);
            try
            {
                using (HtmlConverter html = new HtmlConverter())
                {
                    html.EnableJavaScript = true;
                    html.AutoDetectPageBreak = true;
                    html.EnableHyperlinks = true;
                    HtmlToPdfResult result = html.Convert("http://www.syncfusion.com", ImageType.Metafile, (int)width, -1, AspectRatio.KeepWidth);
                    if (result != null)
                    {
                        PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
                        mf.Quality = 100;

                        PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
                        format.Break = PdfLayoutBreakType.FitPage;
                        format.Layout = PdfLayoutType.Paginate;
                        pdf.PageSettings.Height = result.RenderedImage.Size.Height;
                        format.SplitTextLines = false;
                        format.SplitImages = true;

                        result.Render(page, format);
                    }
                    else
                    {
                        Response.Write("Warning! Please check the HTML link");
                    }

                    pdf.Save("Sample.pdf", Response, HttpReadType.Save);
                }
            }
            catch (Exception exeption)
            {
                string message = exeption.Message;
                Response.Write(exeption);
            }

I did reference Syncfusion.HTML.Convert.Base and Syncfusion.HTML.Pdf.Base 

 And this an error I am getting.

System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. at System.Windows.Forms.AxHost..ctor(String clsid, Int32 flags) at Syncfusion.HtmlConverter.Natives.AxWebBrowser..ctor() at Syncfusion.HtmlConverter.HtmlConverter.InitializeControl() at Syncfusion.HtmlConverter.HtmlConverter..ctor()

Am I missing anything?

Thank you in advance for your attention.


2 Replies

MW Martin White October 30, 2012 07:22 PM UTC

I got this same error. I solved it by running the code that create the pdf in a new thread.

You need to make sure you set the ApartmenetState of the new Thread to STA


PH Praveenkumar H Syncfusion Team November 2, 2012 04:58 AM UTC

Hi Bashkim Deliu

 

You need set  the ApartmenetState of the new Thread to STA . for more information please visit following link

 

http://www.syncfusion.com/support/kb/2309/

 

with regards,

Praveen


Loader.
Up arrow icon