Example #1
0
        /// <summary>
        /// Returns an instance of a PDF converter that implements the IPechkin interface.
        /// </summary>
        /// <param name="config">A GlobalSettings object for the converter to apply.</param>
        /// <returns>IPechkin</returns>
        public static IPechkin Create()
        {
            if (Factory.operatingDomain == null)
            {
                lock (setupLock)
                {
                    if (Factory.operatingDomain == null)
                    {
                        Factory.SetupAppDomain();
                    }
                }
            }

            ObjectHandle handle = Activator.CreateInstanceFrom(
                Factory.operatingDomain,
                Assembly.GetExecutingAssembly().Location,
                typeof(SimplePechkin).FullName,
                false,
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                null,
                null,
                null,
                null,
                null);

            IPechkin instance = handle.Unwrap() as IPechkin;

            return(new Proxy(instance));
        }
Example #2
0
        public void ResultIsPdf()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            using (IPechkin c = ProduceTestObject(new GlobalConfig()))
            {
                byte[] ret = c.Convert(html);

                Assert.NotNull(ret);

                byte[] right = Encoding.UTF8.GetBytes("%PDF");

                Assert.True(right.Length <= ret.Length);

                byte[] test = new byte[right.Length];
                Array.Copy(ret, 0, test, 0, right.Length);

                for (int i = 0; i < right.Length; i++)
                {
                    Assert.Equal(right[i], test[i]);
                }
            }

            TestEnd();
        }
Example #3
0
        /// <summary>
        /// Returns an instance of a PDF converter that implements the IPechkin interface.
        /// </summary>
        /// <param name="config">A GlobalConfig object for the converter to apply.</param>
        /// <returns>IPechkin</returns>
        public static IPechkin Create(GlobalConfig config)
        {
            if (Factory.operatingDomain == null)
            {
                Factory.SetupAppDomain();
            }

            String location = Factory.realAssemblyLocation ?? Assembly.GetExecutingAssembly().Location;

            ObjectHandle handle = Activator.CreateInstanceFrom(
                Factory.operatingDomain,
                location,
                typeof(SimplePechkin).FullName,
                false,
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                null,
                new object[] { config },
                null,
                null);

            IPechkin instance = handle.Unwrap() as IPechkin;

            Proxy proxy = new Proxy(instance, Factory.invocationDelegate);

            Factory.proxies.Add(proxy);

            proxy.Disposed += Factory.OnInstanceDisposed;

            return(proxy);
        }
Example #4
0
        internal Proxy(IPechkin remote, Delegate invoker)
        {
            this.remoteInstance = remote;
            this.invoker = invoker;
            this.IsDisposed = remote.IsDisposed;

            // For all these event handlers, making sure to re-signal
            // using the PROXY as arg A, not the Remote, otherwise
            // synchronization could break

            remote.Begin += (a, b) =>
            {
                if (this.Begin != null)
                {
                    this.Begin(this, b);
                }
            };

            remote.Error += (a, b) =>
            {
                if (this.Error != null)
                {
                    this.Error(this, b);
                }
            };

            remote.Finished += (a, b) =>
            {
                if (this.Finished != null)
                {
                    this.Finished(this, b);
                }
            };

            remote.PhaseChanged += (a, b, c) =>
            {
                if (this.PhaseChanged != null)
                {
                    this.PhaseChanged(this, b, c);
                }
            };

            remote.ProgressChanged += (a, b, c) =>
            {
                if (this.ProgressChanged != null)
                {
                    this.ProgressChanged(this, b, c);
                }
            };

            remote.Warning += (a, b) =>
            {
                if (this.Warning != null)
                {
                    this.Warning(this, b);
                }
            };
        }
Example #5
0
        internal Proxy(IPechkin remote, Delegate invoker)
        {
            this.remoteInstance = remote;
            this.invoker        = invoker;
            this.IsDisposed     = remote.IsDisposed;

            // For all these event handlers, making sure to re-signal
            // using the PROXY as arg A, not the Remote, otherwise
            // synchronization could break

            remote.Begin += (a, b) =>
            {
                if (this.Begin != null)
                {
                    this.Begin(this, b);
                }
            };

            remote.Error += (a, b) =>
            {
                if (this.Error != null)
                {
                    this.Error(this, b);
                }
            };

            remote.Finished += (a, b) =>
            {
                if (this.Finished != null)
                {
                    this.Finished(this, b);
                }
            };

            remote.PhaseChanged += (a, b, c) =>
            {
                if (this.PhaseChanged != null)
                {
                    this.PhaseChanged(this, b, c);
                }
            };

            remote.ProgressChanged += (a, b, c) =>
            {
                if (this.ProgressChanged != null)
                {
                    this.ProgressChanged(this, b, c);
                }
            };

            remote.Warning += (a, b) =>
            {
                if (this.Warning != null)
                {
                    this.Warning(this, b);
                }
            };
        }
Example #6
0
        /// <summary>
        /// Event handler for proxies that get disposed.
        /// </summary>
        /// <param name="disposed"></param>
        private static void OnInstanceDisposed(IPechkin disposed)
        {
            Factory.proxies.Remove(disposed);

            if (Factory.proxies.Count == 0 && Factory.useDynamicLoading)
            {
                Factory.TearDownAppDomain(null, EventArgs.Empty);
            }
        }
Example #7
0
 public static void StringToPDF(string html, string fileName)
 {
     using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
     {
         ObjectConfig oConfig = new ObjectConfig();
         oConfig.SetMinFontSize(14);
         byte[] pdf = pechkin.Convert(oConfig, html);
         File.WriteAllBytes(fileName, pdf);
     }
 }
Example #8
0
        public void ReturnsResultFromString()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            IPechkin c = Factory.Create();

            byte[] ret = c.Convert(html);

            Assert.NotNull(ret);
        }
Example #9
0
 private void OnScWarning(IPechkin converter, string warningtext)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Warning: " + warningtext); }));
     }
     else
     {
         MessageBox.Show("Warning: " + warningtext);
     }
 }
Example #10
0
 private void OnScBegin(IPechkin converter, int expectedphasecount)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("Begin, PhaseCount: {0}", expectedphasecount); }));
     }
     else
     {
         this.Text = string.Format("Begin, PhaseCount: {0}", expectedphasecount);
     }
 }
Example #11
0
 private void OnScFinished(IPechkin converter, bool success)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("Finished, Success: " + success); }));
     }
     else
     {
         Text = ("Finished, Success: " + success);
     }
 }
Example #12
0
 private void OnScPhase(IPechkin converter, int phasenumber, string phasedescription)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("New Phase " + phasenumber + ": " + phasedescription); }));
     }
     else
     {
         Text = ("New Phase " + phasenumber + ": " + phasedescription);
     }
 }
Example #13
0
 private void OnScPhase(IPechkin converter, int phasenumber, string phasedescription)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("New Phase {0}: {1}", phasenumber, phasedescription); }));
     }
     else
     {
         this.Text = string.Format("New Phase {0}: {1}", phasenumber, phasedescription);
     }
 }
Example #14
0
 private void OnScWarning(IPechkin converter, string warningtext)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { MessageBox.Show(string.Format("Warning: {0}", warningtext)); }));
     }
     else
     {
         MessageBox.Show(string.Format("Warning: {0}", warningtext));
     }
 }
Example #15
0
 private void OnScFinished(IPechkin converter, bool success)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("Finished, Success: {0}", success); }));
     }
     else
     {
         this.Text = string.Format("Finished, Success: {0}", success);
     }
 }
Example #16
0
 private void OnScError(IPechkin converter, string errorText)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { MessageBox.Show(string.Format("Error: {0}", errorText)); }));
     }
     else
     {
         MessageBox.Show(string.Format("Error: {0}", errorText));
     }
 }
Example #17
0
 private void OnScBegin(IPechkin converter, int expectedphasecount)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("Begin, PhaseCount: " + expectedphasecount); }));
     }
     else
     {
         Text = ("Begin, PhaseCount: " + expectedphasecount);
     }
 }
Example #18
0
 private void OnScError(IPechkin converter, string errorText)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Error: " + errorText); }));
     }
     else
     {
         MessageBox.Show("Error: " + errorText);
     }
 }
Example #19
0
 private void OnScProgress(IPechkin converter, int progress, string progressdescription)
 {
     if (InvokeRequired)
     {
         // simple Invoke WILL NEVER SUCCEDE, because we're in the button click handler. Invoke will simply deadlock everything
         BeginInvoke((Action <string>)SetText, "Progress " + progress + ": " + progressdescription);
     }
     else
     {
         Text = ("Progress " + progress + ": " + progressdescription);
     }
 }
Example #20
0
 private void OnScProgress(IPechkin converter, int progress, string progressdescription)
 {
     if (this.InvokeRequired)
     {
         // simple Invoke WILL NEVER SUCCEDE, because we're in the button click handler. Invoke will simply deadlock everything
         this.BeginInvoke((Action <string>)SetText, string.Format("Progress {0}: {1}", progress, progressdescription));
     }
     else
     {
         this.Text = string.Format("Progress {0}: {1}", progress, progressdescription);
     }
 }
Example #21
0
        public void ReturnsResultFromString()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            using (IPechkin c = ProduceTestObject(new GlobalConfig()))
            {
                byte[] ret = c.Convert(html);

                Assert.NotNull(ret);
            }

            TestEnd();
        }
Example #22
0
        public void OneObjectPerformsTwoConversionSequentially()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            IPechkin c = Factory.Create();

            byte[] ret = c.Convert(html);

            Assert.NotNull(ret);

            ret = c.Convert(html);

            Assert.NotNull(ret);
        }
Example #23
0
        public ActionResult DownloadPdf2()
        {
            string url = "http://www.baidu.com/";

            using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
            {
                ObjectConfig oc = new ObjectConfig();
                oc.SetPrintBackground(true);
                oc.SetLoadImages(true);
                oc.SetScreenMediaType(true);
                oc.SetPageUri(url);
                byte[] pdf = pechkin.Convert(oc);
                return(File(pdf, "application/pdf", "ExportPdf.pdf"));
            }
        }
Example #24
0
        public void ObjectIsHappilyGarbageCollected()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            IPechkin c = Factory.Create();

            byte[] ret = c.Convert(html);

            Assert.NotNull(ret);

            c   = Factory.Create();
            ret = c.Convert(html);

            Assert.NotNull(ret);

            GC.Collect();
        }
Example #25
0
        public void OneObjectPerformsTwoConversionSequentially()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            using (IPechkin c = ProduceTestObject(new GlobalConfig()))
            {
                byte[] ret = c.Convert(html);

                Assert.NotNull(ret);

                ret = c.Convert(html);

                Assert.NotNull(ret);
            }

            TestEnd();
        }
Example #26
0
        protected void ImgPDF_Click(object sender, ImageClickEventArgs e)
        {
            string ProjectName = WebModel.PidName(Session["DatabaseName"].ToString(), "ProjectM0", Session["ProjectCode"].ToString());
            string l_str2      = "";

            l_str2 += @"<table class=""Table100"" border=""1"">         
            <tr><td class=""Table0C"" colspan=""6"">分包預算工料</td>";
            l_str2 += DateListString.Text;
            l_str2 += @"</tr> <tr>
                <td class=""Table0C"">項次</td>
                <td class=""Table0C"">工項/工料名稱</td>
                <td class=""Table0C"">單位</td>
                <td class=""Table0C"">分包數量</td>
                <td class=""Table0C"">預算單價</td>
                <td class=""Table0C"">預算複價</td>";
            l_str2 += DateListString1.Text;
            l_str2 += DateListString2.Text;
            l_str2 += DateListString3.Text;
            l_str2 += @"</tr></table>";
            string html = l_str2.ToString();

            using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
            {
                var pdf = pechkin.Convert(new ObjectConfig()
                                          .SetLoadImages(true).SetZoomFactor(1.5)
                                          .SetPrintBackground(true)
                                          .SetScreenMediaType(true)
                                          .SetAffectPageCounts(true)
                                          .SetCreateExternalLinks(true), html);



                Response.Clear();

                Response.ClearContent();
                Response.ClearHeaders();

                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + ProjectName + ".pdf; size={0}", pdf.Length));
                Response.BinaryWrite(pdf);

                Response.Flush();
                Response.End();
            }
        }
Example #27
0
        public void ObjectIsHappilyGarbageCollected()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            IPechkin c = ProduceTestObject(new GlobalConfig());

            byte[] ret = c.Convert(html);

            Assert.NotNull(ret);

            c.Dispose();

            c   = ProduceTestObject(new GlobalConfig());
            ret = c.Convert(html);

            Assert.NotNull(ret);

            GC.Collect();

            TestEnd();
        }
Example #28
0
        public void HandlesConcurrentThreads()
        {
            string html          = GetResourceString("PechkinTests.Resources.page.html");
            int    numberOfTasks = 10;
            int    completed     = 0;

            var tasks = Enumerable.Range(0, numberOfTasks).Select(i => new Task(() =>
            {
                Debug.WriteLine(String.Format("#{0} started", i + 1));
                IPechkin sc = Factory.Create();
                Assert.NotNull(sc.Convert(html));
                completed++;
                Debug.WriteLine(String.Format("#{0} completed", i + 1));
            }));

            Parallel.ForEach(tasks, task => task.Start());

            while (completed < numberOfTasks)
            {
                Thread.Sleep(1000);
            }
        }
Example #29
0
        public void ResultIsPdf()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            IPechkin c = Factory.Create();

            byte[] ret = c.Convert(html);

            Assert.NotNull(ret);

            byte[] right = Encoding.UTF8.GetBytes("%PDF");

            Assert.True(right.Length <= ret.Length);

            byte[] test = new byte[right.Length];
            Array.Copy(ret, 0, test, 0, right.Length);

            for (int i = 0; i < right.Length; i++)
            {
                Assert.Equal(right[i], test[i]);
            }
        }
Example #30
0
        protected void ImgPDF_Click(object sender, ImageClickEventArgs e)
        {
            string ProjectName = WebModel.PidName(Session["DatabaseName"].ToString(), "ProjectM0", Session["ProjectCode"].ToString());
            string l_str2      = "";

            l_str2 += @"<table class=""Sht100""><tr class=""ShtRowTop"">";
            l_str2 += Literal1.Text;
            l_str2 += Literal4.Text;
            l_str2 += Literal2.Text;
            l_str2 += Literal5.Text;
            l_str2 += @"<tr class=""ShtRowSingle"">";
            l_str2 += Literal3.Text;
            l_str2 += @"</tr></table>";
            string html = l_str2.ToString();

            using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
            {
                var pdf = pechkin.Convert(new ObjectConfig()
                                          .SetLoadImages(true).SetZoomFactor(1.5)
                                          .SetPrintBackground(true)
                                          .SetScreenMediaType(true)
                                          .SetAffectPageCounts(true)
                                          .SetCreateExternalLinks(true), html);



                Response.Clear();

                Response.ClearContent();
                Response.ClearHeaders();

                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=" + ProjectName + ".pdf; size={0}", pdf.Length));
                Response.BinaryWrite(pdf);

                Response.Flush();
                Response.End();
            }
        }
Example #31
0
        public void ReturnsResultFromFile()
        {
            string html = GetResourceString("PechkinTests.Resources.page.html");

            string       fn = Path.GetTempFileName() + ".html";
            FileStream   fs = new FileStream(fn, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            sw.Write(html);

            sw.Close();

            using (IPechkin c = ProduceTestObject(new GlobalConfig()))
            {
                byte[] ret = c.Convert(new ObjectConfig().SetPageUri(fn));

                Assert.NotNull(ret);
            }

            File.Delete(fn);

            TestEnd();
        }
Example #32
0
 private void OnScPhase(IPechkin converter, int phasenumber, string phasedescription)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("New Phase " + phasenumber + ": " + phasedescription); }));
     }
     else
     {
         Text = ("New Phase " + phasenumber + ": " + phasedescription);
     }
 }
Example #33
0
 private void OnScProgress(IPechkin converter, int progress, string progressdescription)
 {
     if (this.InvokeRequired)
     {
         // simple Invoke WILL NEVER SUCCEDE, because we're in the button click handler. Invoke will simply deadlock everything
         this.BeginInvoke((Action<string>)SetText, string.Format("Progress {0}: {1}", progress, progressdescription));
     }
     else
     {
         this.Text = string.Format("Progress {0}: {1}", progress, progressdescription);
     }
 }
Example #34
0
 private void OnScBegin(IPechkin converter, int expectedphasecount)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("Begin, PhaseCount: {0}", expectedphasecount); }));
     }
     else
     {
         this.Text = string.Format("Begin, PhaseCount: {0}", expectedphasecount);
     }
 }
Example #35
0
 private void OnScProgress(IPechkin converter, int progress, string progressdescription)
 {
     if (InvokeRequired)
     {
         // simple Invoke WILL NEVER SUCCEDE, because we're in the button click handler. Invoke will simply deadlock everything
         BeginInvoke((Action<string>)SetText,"Progress " + progress + ": " + progressdescription);
     }
     else
     {
         Text = ("Progress " + progress + ": " + progressdescription);
     }
 }
Example #36
0
 private void OnScError(IPechkin converter, string errorText)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Error: " + errorText); }));
     }
     else
     {
         MessageBox.Show("Error: " + errorText);
     }
 }
Example #37
0
 private void OnScError(IPechkin converter, string errorText)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { MessageBox.Show(string.Format("Error: {0}", errorText)); }));
     }
     else
     {
         MessageBox.Show(string.Format("Error: {0}", errorText));
     }
 }
Example #38
0
 private void OnScWarning(IPechkin converter, string warningtext)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { MessageBox.Show(string.Format("Warning: {0}", warningtext)); }));
     }
     else
     {
         MessageBox.Show(string.Format("Warning: {0}", warningtext));
     }
 }
Example #39
0
 private void OnScFinished(IPechkin converter, bool success)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("Finished, Success: {0}", success); }));
     }
     else
     {
         this.Text = string.Format("Finished, Success: {0}", success);
     }
 }
Example #40
0
 private void OnScPhase(IPechkin converter, int phasenumber, string phasedescription)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke((Action)(() => { Text = string.Format("New Phase {0}: {1}", phasenumber, phasedescription); }));
     }
     else
     {
         this.Text = string.Format("New Phase {0}: {1}", phasenumber, phasedescription);
     }
 }
Example #41
0
 private void OnScFinished(IPechkin converter, bool success)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("Finished, Success: " + success); }));
     }
     else
     {
         Text = ("Finished, Success: " + success);
     }
 }
Example #42
0
 private void OnScWarning(IPechkin converter, string warningtext)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Warning: " + warningtext); }));
     }
     else
     {
         MessageBox.Show("Warning: " + warningtext);
     }
 }
Example #43
0
 private void OnScBegin(IPechkin converter, int expectedphasecount)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("Begin, PhaseCount: " + expectedphasecount); }));
     }
     else
     {
         Text = ("Begin, PhaseCount: " + expectedphasecount);
     }
 }
Example #44
0
        /// <summary>
        /// Event handler for proxies that get disposed.
        /// </summary>
        /// <param name="disposed"></param>
        private static void OnInstanceDisposed(IPechkin disposed)
        {
            Factory.proxies.Remove(disposed);

            if (Factory.proxies.Count == 0 && Factory.useDynamicLoading)
            {
                Factory.TearDownAppDomain(null, EventArgs.Empty);
            }
        }
Example #45
0
 /// <summary>
 /// Constructs HTML to PDF converter instance from <code>GlobalConfig</code>.
 /// </summary>
 /// <param name="config">global configuration object</param>
 public SynchronizedPechkin(GlobalConfig config)
 {
     _converter = (IPechkin) _synchronizer.Invoke((Func<GlobalConfig, IPechkin>)(cfg => new SimplePechkin(cfg)), new[] { config });
 }