Welcome Guest Search | Active Topics | Sign In | Register

Converting html to pdf with reflection Options
Eitan
Posted: Sunday, November 20, 2011 12:01:50 PM
Rank: Newbie
Groups: Member

Joined: 10/31/2011
Posts: 1
Hi Guys,
Due to several constraints i need to run my eo.pdf plugin with reflection.
I'm trying to generate pdf file and i get all the settings but i get an exception when i try to call the "ConvertHTML" method
the Message is: "Exception has been thrown by the target of an invocation."

At first i generated my pdf file without reflection and i got no exceptions so i'm guessing the reflection thing causing an inner exception in "ConvertHtml" method

any ideas?

Thanks.

Here is the code i'm using:
Code: C#
byte[] asseblyBytes = stream.ToArray();

            Assembly assembly = Assembly.Load(asseblyBytes);
            Type runtime = assembly.GetType("EO.Pdf.Runtime");
            MethodInfo runtimeMethod = runtime.GetMethod("AddLicense");
            runtimeMethod.Invoke(null, new object[] { "" });



            Type HtmlToPdfOptionsType = assembly.GetType("EO.Pdf.HtmlToPdfOptions");
            object HtmlToPdfOptionsIns = System.Activator.CreateInstance(HtmlToPdfOptionsType);
            FieldInfo runtimeFieldPage = HtmlToPdfOptionsType.GetField("PageSize");
            runtimeFieldPage.SetValue(HtmlToPdfOptionsIns, new System.Drawing.SizeF(8.5f, 12f));
            FieldInfo runtimeFieldHeader = HtmlToPdfOptionsType.GetField("HeaderHtmlFormat");
            runtimeFieldHeader.SetValue(HtmlToPdfOptionsIns, "header");
            FieldInfo runtimeFieldFooter = HtmlToPdfOptionsType.GetField("FooterHtmlFormat");
            runtimeFieldFooter.SetValue(HtmlToPdfOptionsIns, "tooer");
            FieldInfo runtimeFieldFooterPos = HtmlToPdfOptionsType.GetField("FooterHtmlPosition");
            runtimeFieldFooterPos.SetValue(HtmlToPdfOptionsIns, 11f);


            // PdfDocument
            Type PdfDocument = assembly.GetType("EO.Pdf.PdfDocument");
            object PdfDocumentInstance = System.Activator.CreateInstance(PdfDocument);

            Type HtmlToPdf = assembly.GetType("EO.Pdf.HtmlToPdf");
            object HtmlToPdfInstance = System.Activator.CreateInstance(HtmlToPdf);


            HtmlToPdf.InvokeMember("ConvertHtml", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, HtmlToPdfInstance, new object[] { "<html><head></head><body>Hello World</body></html>", PdfDocumentInstance, HtmlToPdfOptionsIns });

            MethodInfo PdfDocumentSaveMethod = PdfDocumentInstance.GetType().GetMethod("Save");
            PdfDocumentSaveMethod.Invoke(PdfDocumentInstance, new object[] { pdfStream });

            File.WriteAllBytes(@"C:\test.pdf", pdfStream.GetBuffer());
eo_support
Posted: Sunday, November 20, 2011 12:11:25 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

There are two things:

1. You can NOT load EO.Pdf.dll from a byte array. EO.Pdf.dll MUST be a physical file on disk. Otherwise it won't work. This means you can not hide EO.Pdf.dll;

2. You do not need to create an instance of EO.Pdf.HtmlToPdf because ConvertHtml is a static function;

Because ConvertHtml is overloaded, you may also want to get a MethodInfo object first and then use MethodInfo.Invoke instead of Type.InvokeMember.

If you continue to have problem, check your Exception object's InnerException. That should tell you exactly what's wrong.

Hope this helps. Let us know if you still have any questions.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.