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

Converting HTML to PDF in a class Module

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


1 Reply

GM Geetha M Syncfusion Team 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


Loader.
Live Chat Icon For mobile
Up arrow icon