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());
|
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!
|