public class htmlToPDFHelper
    {
        #region 使用wkhtmltopdf.exe组建转换htmlToPDFHelper

        /// <summary>
        /// html文件转换成pdf
        /// </summary>
        /// <param name="htmlPath">html文件绝对路径或者url地址</param>
        /// <param name="pdfSavePath">pdf文件保存路径</param>
        /// <remarks>pdfSavePath说明:不能有非Ansi字符,如中文、日文等,且转换gb2312、韩文charset、日文charset等非utf-8\ansi等网页时,会出现乱码</remarks>
        /// <returns>是否转换成功(true=成功,false=失败)</returns>
        public static bool HtmlToPDF(string htmlPath,string pdfSavePath)
        {
            try
            {
                if (string.IsNullOrEmpty(htmlPath) || string.IsNullOrEmpty(pdfSavePath))
                {
                    return false;
                }
                using (Process p = new Process())
                {
                    string str = Path.Combine(HttpContext.Current.Server.MapPath(ProjectTransfrom.HtmlToPDFConvertPath), //wkhtmltopdf.exe所在绝对路径

                    if (!File.Exists(str))
                    {
                        return false;
                    }
                    KillWKHtmltoPDF();
                    p.StartInfo.FileName = str;
                    p.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", htmlPath, pdfSavePath);
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    return ConfirmConvertSuccess(pdfSavePath);
                }
            }
            catch (Exception)
            {
            }
            return false;
        }

        private static bool ConfirmConvertSuccess(string path)
        {
            int count = 0;
            bool isSuccessful = true;

            while (true)
            {
                if (File.Exists(path))
                {
                    WaitWKHtmltoPDFClose();
                    break;
                }
                Thread.Sleep(1000);
                count++;
                if (count >= 300)
                {
                    isSuccessful = false;
                    break;
                }
            }
            return isSuccessful;
        }

        private static void WaitWKHtmltoPDFClose()
        {
            while (true)
            {
                Process[] procs = Process.GetProcessesByName("wkhtmltopdf");
                if (procs.Length > 0)
                {
                    Thread.Sleep(5000);
                }
                else
                {
                    break;
                }
            }
        }

        /// <summary>
        /// Kill WKHTMLTOPDF exe
        /// </summary>
        private static void KillWKHtmltoPDF()
        {
            try
            {
                Process[] procs = Process.GetProcessesByName("wkhtmltopdf");
                Array.ForEach(procs,
                delegate(Process proc)
                {
                    proc.Kill();
                });
            }
            catch (Exception)
            {
            }
        }
        #endregion
    }

更多推荐