Ejemplo n.º 1
0
// ReSharper restore NotAccessedField.Local

        /// <summary>
        /// Initializes wrapped library. This is done automatically when you need it.
        /// </summary>
        /// <param name="useGraphics">use X11 graphics, <code>false</code> in most cases.</param>
        public static void InitLib(bool useGraphics)
        {
            if (_inited)
            {
                return;
            }
            _inited = true;

            if (Log.IsTraceEnabled)
            {
                Log.Trace("T:" + Thread.CurrentThread.Name + " Initializing library (wkhtmltopdf_init)");
            }

            PechkinBindings.wkhtmltopdf_init(useGraphics ? 1 : 0);

            if (_useHack)
            {
                _hackObj = new SimplePechkin(new GlobalConfig());
            }

            if (LibInit != null)
            {
                LibInit();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates and initializes a private AppDomain and therein loads and initializes the
        /// wkhtmltopdf library. Attaches to the current AppDomain's DomainUnload event in IIS environments
        /// to ensure that on re-deploy, the library is freed so the new AppDomain will be able to use it.
        /// </summary>
        private static void SetupAppDomain()
        {
            if (Factory.useSync)
            {
                Factory.synchronizer = new SynchronizedDispatcherThread();
            }

            String binPath = String.Empty;

            if (!String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
            {
                String[] paths = AppDomain.CurrentDomain.RelativeSearchPath.Split(';');

                for (var i = 0; i < paths.Length; i++)
                {
                    paths[i].Remove(0, AppDomain.CurrentDomain.BaseDirectory.Length);
                }

                binPath = String.Join(";", paths);
            }

            Factory.operatingDomain = AppDomain.CreateDomain("pechkin_internal_domain", null,
                                                             new AppDomainSetup
            {
                ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
                // Sometimes, like in a web app, your bin folder is not the same
                // as the base dir.
                PrivateBinPath = binPath
            });

            if (binPath != String.Empty)
            {
                Factory.operatingDomain.SetData("assemblyLocation", Assembly.GetExecutingAssembly().Location);

                Factory.operatingDomain.DoCallBack(() =>
                {
                    String location     = AppDomain.CurrentDomain.GetData("assemblyLocation").ToString();
                    String filename     = System.IO.Path.GetFileName(location);
                    List <String> paths = AppDomain.CurrentDomain.RelativeSearchPath.Split(';').ToList();

                    foreach (String path in paths.ToArray())
                    {
                        paths.Remove(path);
                        paths.AddRange(System.IO.Directory.GetFiles(path, filename));
                    }

                    Assembly.LoadFrom(paths[0]);

                    AppDomain.CurrentDomain.SetData("assemblyLocation", paths[0]);
                });

                Factory.realAssemblyLocation = Factory.operatingDomain.GetData("assemblyLocation").ToString();
            }
            else
            {
                Factory.operatingDomain.Load(Assembly.GetExecutingAssembly().FullName);
            }

            Func <object> del = () =>
            {
                Factory.operatingDomain.SetData("useX11Graphics", Factory.useX11Graphics);

                Factory.operatingDomain.DoCallBack(() =>
                {
                    PechkinBindings.wkhtmltopdf_init((bool)AppDomain.CurrentDomain.GetData("useX11Graphics") ? 1 : 0);
                });

                return(null);
            };

            Factory.invocationDelegate.DynamicInvoke(del);

            if (AppDomain.CurrentDomain.IsDefaultAppDomain() == false)
            {
                AppDomain.CurrentDomain.DomainUnload += Factory.TearDownAppDomain;
            }
        }