示例#1
0
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/Reports/Utility/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("Can't find wkhtmltopdf.exe which is required for PDF generation at location: '{0}'!", exePath));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("An error occured while starting PDF generator!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 seconds
                {
                    throw new InvalidOperationException("Timeout while PDF generation!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF generator returned error code {0}!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("Can't find generatored PDF file!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Generated PDF file is empty!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }
示例#2
0
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/App_Data/Reporting/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("Can't find wkhtmltopdf.exe which is required for PDF generation.\n" +
                                                                  "Please download a stable version from http://wkhtmltopdf.org/downloads.html\n and place it under directory '{0}'.",
                                                                  Path.GetDirectoryName(exePath)));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            args.Add(!SmartShrinking ? "--disable-smart-shrinking" : "--enable-smart-shrinking");

            if (!string.IsNullOrEmpty(PageSize))
            {
                args.Add("--page-size");
                args.Add(PageSize);
            }

            if (!string.IsNullOrEmpty(PageWidth))
            {
                args.Add("--page-width");
                args.Add(PageWidth);
            }

            if (!string.IsNullOrEmpty(PageHeight))
            {
                args.Add("--page-height");
                args.Add(PageHeight);
            }

            if (MarginLeft != null)
            {
                args.Add("--margin-left");
                args.Add(MarginLeft);
            }

            if (MarginTop != null)
            {
                args.Add("--margin-top");
                args.Add(MarginTop);
            }

            if (MarginRight != null)
            {
                args.Add("--margin-right");
                args.Add(MarginRight);
            }

            if (MarginBottom != null)
            {
                args.Add("--margin-bottom");
                args.Add(MarginBottom);
            }

            if (Dpi != null)
            {
                args.Add("--dpi");
                args.Add(Dpi.Value.ToString(CultureInfo.InvariantCulture));
            }

            if (Zoom != null)
            {
                args.Add("--zoom");
                args.Add(Zoom);
            }

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            if (PrintBackground)
            {
                args.Add("--background");
            }
            else
            {
                args.Add("--no-background");
            }

            if (FooterHtmlUrl != null)
            {
                args.Add("--footer-html");
                args.Add(FooterHtmlUrl);
            }

            if (Landscape)
            {
                args.Add("--orientation");
                args.Add("Landscape");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            foreach (var replace in FooterHeaderReplace)
            {
                args.Add("--replace");
                args.Add(replace.Key);
                args.Add(replace.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            foreach (var arg in CustomArgs)
            {
                args.Add(arg);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("An error occured while starting PDF generator!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 seconds
                {
                    throw new InvalidOperationException("Timeout while PDF generation!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF generator returned error code {0}!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("Can't find generatored PDF file!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Generated PDF file is empty!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }
        public byte[] Execute()
        {
            var exePath = UtilityExePath ?? HostingEnvironment.MapPath("~/Reports/Utility/wkhtmltopdf.exe");

            if (!File.Exists(exePath))
            {
                throw new InvalidOperationException(String.Format("PDF üretimi için gereken wkhtmltopdf.exe dosyası '{0}' konumunda bulunamadı'", exePath));
            }

            if (String.IsNullOrEmpty(this.Url))
            {
                throw new ArgumentNullException("url");
            }

            var args = new List <string>();

            if (UsePrintMediaType)
            {
                args.Add("--print-media-type");
            }
            else
            {
                args.Add("--no-print-media-type");
            }

            foreach (var cookie in Cookies)
            {
                args.Add("--cookie");
                args.Add(cookie.Key);
                args.Add(cookie.Value);
            }

            args.Add(this.Url);
            foreach (var additional in AdditionalUrls)
            {
                args.Add(additional);
            }

            var tempFile = Path.GetTempFileName();

            try
            {
                args.Add(tempFile);

                var process = new Process
                {
                    StartInfo = new ProcessStartInfo(exePath, CommandLineTools.EscapeArguments(args.ToArray()))
                    {
                        UseShellExecute = false,
                        CreateNoWindow  = true
                    }
                };

                if (!process.Start())
                {
                    throw new InvalidOperationException("PDF üretim programı başlatılırken hata oluştu!");
                }

                if (!process.WaitForExit(TimeoutSeconds * 1000)) // max 300 sn de üretmeli
                {
                    throw new InvalidOperationException("PDF üretilirken zaman aşımı oluştu!");
                }

                if (process.ExitCode != 0 && process.ExitCode != 1)
                {
                    throw new InvalidOperationException(String.Format("PDF üretim programı {0} hata kodu döndürdü!", process.ExitCode));
                }

                if (!File.Exists(tempFile))
                {
                    throw new InvalidOperationException("PDF üretimi sonrasında geçici dosya bulunamadı!");
                }

                var bytes = File.ReadAllBytes(tempFile);
                if (bytes.Length == 0)
                {
                    throw new InvalidOperationException("Üretilen PDF dosyası boş!");
                }

                return(bytes);
            }
            finally
            {
                TemporaryFileHelper.TryDelete(tempFile);
            }
        }