Example #1
0
        static void Main(string[] args)
        {
            try
            {
                //  Read names of input and output files.
                HtmlToPdfConverter htmlToPdfConverter = ParseArguments(args);
                try
                {
                    // Configure the logging.
                    htmlToPdfConverter.ConfigureLogging();

                    // Read HTML, convert and write PDF.
                    htmlToPdfConverter.ConvertFileToPdf();
                }
                finally
                {
                    htmlToPdfConverter.CleanUp();
                }

                Environment.ExitCode = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                Environment.ExitCode = 1;
            }
        }
        public HtmlToPdfResult Get(string url)
        {
            try
            {
                if (url != null && url != "")
                {
                    string pdfFileName = FileNameController.UrlToFileName(url, ".pdf");
                    string pdfFilePath = Path.Combine(Directory.GetCurrentDirectory(), pdfFileName);

                    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(url, pdfFilePath);

                    try
                    {
                        // Configures the logging.
                        htmlToPdfConverter.ConfigureLogging();

                        // Reads HTML, converts and writes PDF.
                        htmlToPdfConverter.ConvertFileToPdf();
                    }
                    finally
                    {
                        // Removes temporary files.
                        //htmlToPdfConverter.CleanUp();
                    }

                    return(new HtmlToPdfResult {
                        UrlToPdf = GetStaticUrl(pdfFileName), FileName = pdfFileName, Message = "Converted successfully."
                    });
                }
                else
                {
                    return(new HtmlToPdfResult {
                        Message = "Provide URL for conversion via \"url\" parameter: https://<host>/pdf/?url=<url>."
                    });
                }
            }
            catch (Exception e)
            {
                return(new HtmlToPdfResult {
                    Message = e.Message
                });
            }
        }