Пример #1
0
        public void ConvertUrlToPdfWithSuccess()
        {
            byte[] resultBuffer = null;
            string url          = "http://writelinez.com";

            UriSourceConverterSettings settings = new UriSourceConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;
            settings.WebSettings            = new PdfWebSettings();
            settings.WebSettings.LoadImages = true;
            settings.WebSettings.Background = true;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            using (UriSourcePdfConverter converter = pdf.CreateUriSourcePdfConverter())
            {
                resultBuffer = converter.Convert(settings, url);
            }

            using (FileStream fs = new FileStream(@"c:\temp\writelinez.pdf", FileMode.Create))
            {
                fs.Write(resultBuffer, 0, resultBuffer.Length);
            }
        }
Пример #2
0
        public void ConvertStringWithMultipleThreads()
        {
            BlockingCollection <byte[]> dta = new BlockingCollection <byte[]>();
            int threadIterations            = 50;

            byte[] resultBuffer = null;
            string htmlString   = @"<html><head><title>Test Page</title></head><body><b>COUNTER: {0}</b></body></html>";

            StandardPdfConverterSettings settings = new StandardPdfConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            Parallel.For(0, threadIterations, index =>
            {
                string ns = string.Format(htmlString, index);

                using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
                {
                    resultBuffer = pdfConverter.Convert(settings, htmlString);
                    dta.Add(resultBuffer);
                }
            });

            Assert.AreEqual(threadIterations, dta.Count);
            Assert.IsTrue(dta.All(t => t.Length > 0));
        }
        public void TestForCorrectVersion()
        {
            string version;

            using (DotNetCorePdf converter = DotNetCorePdf.Create())
            {
                version = converter.WkHtmlToPdfVersion();
            }

            Assert.AreEqual("0.12.4", version);
        }
Пример #4
0
        public void ConvertBytesWithMultipleThreads()
        {
            BlockingCollection <byte[]> dta = new BlockingCollection <byte[]>();
            int threadIterations            = 2;

            byte[] resultBuffer = null;
            string htmlString   = @"<html><head><title>Test Page</title></head><body><b>COUNTER: {0}</b></body></html>";

            StandardPdfConverterSettings settings = new StandardPdfConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            //for (int index = 0; index < threadIterations; index++)
            //{
            //    string ns = string.Format(htmlString, index);
            //    byte[] htmlBuffer = Encoding.UTF8.GetBytes(ns);

            //    using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
            //    {
            //        resultBuffer = pdfConverter.Convert(settings, htmlBuffer);
            //        dta.Add(resultBuffer);
            //    }
            //    using (FileStream fs = new FileStream($@"c:\temp\test_pdf_{index}.pdf", FileMode.Create))
            //    {
            //        fs.Write(resultBuffer, 0, resultBuffer.Length);
            //    }
            //}
            Parallel.For(0, threadIterations, index =>
            {
                System.Threading.Thread.Sleep(2000 * index);
                string ns         = string.Format(htmlString, index);
                byte[] htmlBuffer = Encoding.UTF8.GetBytes(ns);

                using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
                {
                    resultBuffer = pdfConverter.Convert(settings, htmlBuffer);
                    dta.Add(resultBuffer);
                }
                using (FileStream fs = new FileStream($@"c:\temp\test_pdf_{index}.pdf", FileMode.Create))
                {
                    fs.Write(resultBuffer, 0, resultBuffer.Length);
                }
            });

            Assert.AreEqual(threadIterations, dta.Count);
            Assert.IsTrue(dta.All(t => t.Length > 0));
        }
Пример #5
0
        public void ConvertStringSuccess()
        {
            byte[] resultBuffer = null;
            string htmlString   = @"<html><head><title>Test Page</title></head><body><b>I am the one and only PDF.</b><p>You know you like it!!!</p></body></html>";

            StandardPdfConverterSettings settings = new StandardPdfConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            using (StandardPdfConverter pdfConverter = pdf.CreateStandardPdfConverter())
            {
                resultBuffer = pdfConverter.Convert(settings, htmlString);
            }

            Assert.IsTrue(resultBuffer != null && resultBuffer.Length > 0);
        }
Пример #6
0
        public void ConvertFilePathToPdfWithSuccess()
        {
            byte[] resultBuffer = null;
            string path         = @"c:\temp\testFile.html";

            if (!File.Exists(path))
            {
                string html      = "<html><head><title>Test Page</title></head><body><b>I am the one and only PDF.</b><p>You know you like it!!!</p></body></html>";
                byte[] htmlBytes = Encoding.UTF8.GetBytes(html);
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    fs.Write(htmlBytes, 0, htmlBytes.Length);
                }
            }

            UriSourceConverterSettings settings = new UriSourceConverterSettings();

            settings.GlobalSettings               = new PdfConverterGlobalSettings();
            settings.GlobalSettings.Copies        = 1;
            settings.GlobalSettings.DocumentTitle = "YaYa!!!";
            settings.GlobalSettings.Orientation   = Orientation.Portrait;
            settings.WebSettings            = new PdfWebSettings();
            settings.WebSettings.LoadImages = true;
            settings.WebSettings.Background = true;

            DotNetCorePdf pdf = DotNetCorePdf.Create();

            using (UriSourcePdfConverter converter = pdf.CreateUriSourcePdfConverter())
            {
                resultBuffer = converter.Convert(settings, path);
            }

            using (FileStream fs = new FileStream(@"c:\temp\testFile.pdf", FileMode.Create))
            {
                fs.Write(resultBuffer, 0, resultBuffer.Length);
            }
        }