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. (Last updated on : November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Converting HTML to PDF in a class Module

Thread ID:

Created:

Updated:

Platform:

Replies:

97194 Nov 5,2010 07:32 PM UTC Nov 8,2010 09:00 AM UTC ASP.NET Web Forms (Classic) 1
loading
Tags: PDF
Graham Field
Asked On November 5, 2010 07:32 PM UTC

Hi
I'm new to Syncfusion, but working through your example to convert a HTML page to PDF.

I'm testing the process with nunit prior to incorporating the functionality into my DLL. The DLL runs as part of workflow app, so only calls it as a class module.

When I try to add HtmlConverter it asks for the Windows.forms to be included, but when I try to run it errors with:-
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 System.Windows.Forms.AxHost..ctor(String clsid)
at Syncfusion.HtmlConverter.Natives.AxWebBrowser..ctor()
at Syncfusion.HtmlConverter.HtmlConverter..ctor()
at FCP.CorrespondenceTest.TestSyncFusion.TestSyncFusionPdf() in TestSyncFusion.cs: line 38


My simple class is
[Test]
public void TestSyncFusionPdf()
{

string htmlFile =
@"D:\Development\Correspondence\src\CorrespondenceTest\bin\Debug\0000000000000000000000000024016.html";

PdfDocument doc = new PdfDocument();

doc.PageSettings.SetMargins(20,20,25,35);

PdfPage page = doc.Pages.Add();
SizeF pageSize = page.GetClientSize();

PdfUnitConvertor convertor = new PdfUnitConvertor();
float height = -1;
float width = convertor.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);

using (Syncfusion.HtmlConverter.HtmlConverter html = new Syncfusion.HtmlConverter.HtmlConverter())
{
html.EnableJavaScript = false;
html.AutoDetectPageBreak = true;
html.EnableHyperlinks = false;

using (HtmlToPdfResult result = html.Convert(htmlFile,ImageType.Metafile,(int)width,(int)height,AspectRatio.KeepWidth) )
{
if (result.RenderedImage == null)
return;

PdfMetafile mf = new PdfMetafile(result.RenderedImage as Metafile);
mf.Quality = 100;

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

result.Render(page, format);
}
}

doc.Save("Sample1.doc");
doc.Close();
}

Thanks in anticipation
Graham Field


Geetha M [Syncfusion]
Replied On November 8, 2010 09:00 AM UTC

Hi Graham,

Thank you for your interest in Syncfusion products.

HTMLConverter internally makes use of a windows component which runs only in STA mode. This issue could be resolved by invoking the instance of HTMLConverter separately, under STA by using the following code snippet.

Thread t = new Thread(new ThreadStart(Convert));
t.SetApartmentState(ApartmentState.STA);
t.Start();

Please refer to the following sample and let me know if you have any questions.
I73948.zip

Regards,
Geetha


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.

Warning Icon 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.Close Icon

;