Пример #1
0
        public static void ConvertHtmlToPdf(PdfDocument document, PdfConvertEnvironment environment, PdfOutput woutput)
        {
            if (environment == null)
            {
                environment = Environment;
            }

            String outputPdfFilePath;
            bool   delete;

            if (woutput.OutputFilePath != null)
            {
                outputPdfFilePath = woutput.OutputFilePath;
                delete            = false;
            }
            else
            {
                outputPdfFilePath = Path.Combine(environment.TempFolderPath, String.Format("{0}.pdf", Guid.NewGuid()));
                delete            = true;
            }

            if (!File.Exists(environment.WkHtmlToPdfPath))
            {
                throw new Exception(String.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", environment.WkHtmlToPdfPath));
            }

            ProcessStartInfo si;

            StringBuilder paramsBuilder = new StringBuilder();

            paramsBuilder.Append("--page-size A4 ");
            //paramsBuilder.Append("--redirect-delay 0 "); not available in latest version
            if (!string.IsNullOrEmpty(document.HeaderUrl))
            {
                paramsBuilder.AppendFormat("--header-html {0} ", document.HeaderUrl);
                paramsBuilder.Append("--margin-top 25 ");
                paramsBuilder.Append("--header-spacing 5 ");
            }
            if (!string.IsNullOrEmpty(document.FooterUrl))
            {
                paramsBuilder.AppendFormat("--footer-html {0} ", document.FooterUrl);
                paramsBuilder.Append("--margin-SOSAppttom 25 ");
                paramsBuilder.Append("--footer-spacing 5 ");
            }

            paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", document.Url, outputPdfFilePath);


            si = new ProcessStartInfo();
            si.CreateNoWindow        = true; //!environment.Debug;
            si.FileName              = environment.WkHtmlToPdfPath;
            si.Arguments             = paramsBuilder.ToString();
            si.UseShellExecute       = false;
            si.RedirectStandardError = true; //!environment.Debug;

            try
            {
                using (var process = new Process())
                {
                    process.StartInfo = si;
                    process.Start();

                    try
                    {
                        var output = process.StandardError.ReadToEnd();

                        if (!output.Trim().EndsWith("Done", StringComparison.InvariantCultureIgnoreCase))
                        {
                            throw new Exception(output);
                        }

                        process.WaitForExit();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        process.Close();
                    }
                }
            }
            finally
            {
                if (delete && File.Exists(outputPdfFilePath))
                {
                    File.Delete(outputPdfFilePath);
                }
            }
        }
Пример #2
0
 public static void ConvertHtmlToPdf(PdfDocument document, PdfOutput output)
 {
     ConvertHtmlToPdf(document, null, output);
 }