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.
Unfortunately, activation email could not send to your email. Please try again.

HTML to PDF on web

Thread ID:

Created:

Updated:

Platform:

Replies:

105488 Oct 30,2012 10:06 AM Nov 2,2012 12:58 AM ASP.NET Web Forms (Classic) 2
loading
Tags: PDF
Bashkim Deliu
Asked On October 30, 2012 10:06 AM

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.


Martin White
Replied On October 30, 2012 03:22 PM

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

Praveenkumar H [Syncfusion]
Replied On November 2, 2012 12:58 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;