public string GetUrlToPdf(string url)
        {
            HtmlCleanerInjector injector = new HtmlCleanerInjector(new BaseInjectorConfig(), new WebCleanerConfigSerializer(_pdfController.Server));
            //  Creating cleaner instance based on URL.
            IHtmlCleaner processChain = injector.CreateHtmlCleaner(url);

            //  Performs request.
            string s = HtmlCleanerApp.MakeRequest(url);

            _ = processChain.Process(s);

            ITagFormatter formatter = processChain.GetFormatter();

            //  Finishes processing.
            formatter.CloseDocument();
            using (MemoryStream dataStream = formatter.GetOutputStream())
            {
                string pdfFileName = _pdfController.UrlToFileName(url);
                string pdfFilePath = _pdfController.GetContentPath(pdfFileName);

                if (dataStream != null)
                {
                    using (FileStream fileStream = System.IO.File.Create(pdfFilePath))
                    {
                        dataStream.Seek(0, SeekOrigin.Begin);
                        dataStream.CopyTo(fileStream);
                    }
                }

                return(_pdfController.GetContentUri(pdfFileName));
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            if (args.Count() != 0)
            {
                var url = args[0];

                var injector = new HtmlCleanerInjector(new BaseInjectorConfig(), new ConsoleCleanerConfigSerializer());
                //  Creating cleaner instance based on URL.
                var processChain = injector.CreateHtmlCleaner(url);

                //  Performs request.
                var s = HtmlCleanerApp.MakeRequest(url);

                var output = processChain.Process(s);

                //  Creates directories for storing page content.
                var path = HtmlCleanerApp.CreateDirectories(url);

                var formatter = processChain.GetFormatter();

                //  Forms content file name.
                var fileName = path + "\\" + "content." + formatter.GetResultingFileExtension();

                //  Finishes processing.
                formatter.CloseDocument();
                var dataStream = formatter.GetOutputStream();

                if (dataStream != null)
                {
                    using (var fileStream = File.Create(fileName))
                    {
                        dataStream.Seek(0, SeekOrigin.Begin);
                        dataStream.CopyTo(fileStream);
                    }
                }
                else
                {
                    //  Saves text to file.
                    HtmlCleanerApp.WriteTextToFile(fileName, output);
                }
            }
            else
            {
                //  Default HTML cleaner for writing configutaion.
                var processChain = new WordPressHtmlCleaner(new ConsoleCleanerConfigSerializer());
                //  Writes template of configuration file.
                processChain.WriteConfiguration();
            }
        }