Html to PDF 简单示例


//url:need to convert page's url
//path:save converted pdf file to the server's path
public bool HtmlPageConvertToPDF(string url, string path)
        {
            bool flag = false;
            try
            {
                if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) return false;
                Process process = new Process();
                var wkhtmlExePath = System.Configuration.ConfigurationManager.AppSettings["ToolPath"];
                wkhtmlExePath = currentContext.Server.MapPath(wkhtmlExePath);
                if (File.Exists(wkhtmlExePath))
                {
                    //string str = currentContext.Server.MapPath(wkhtml);
                    //if (!System.IO.File.Exists(str))
                    //    return false;
                    process.StartInfo.FileName = wkhtmlExePath;
                    process.StartInfo.Arguments = " \"" + url + "\" " + path;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    process.WaitForExit(10000);
                    process.Close();
                    if (ConfirmConvertSuccess(path))
                    {
                        flag = true;
                    }
                }
            }
            catch (Exception ex)
            {
                flag = false;
                LogUtility.LogUtilityIntance.LogMessage(string.Format("Method: {0},  Exception Message:{1}", "HtmlPageConvertToPDF", ex.Message));
            }

            return flag;
        }

        
        private void DownLoadPDF(string filePath, string fileName)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                byte[] BynFile = new byte[br.BaseStream.Length];
                br.BaseStream.Seek(0, SeekOrigin.Begin);
                br.Read(BynFile, 0, (int)br.BaseStream.Length);
                fs.Close();

                currentContext.Response.Buffer = true;
                currentContext.Response.Clear();
                currentContext.Response.Charset = "UTF-8";
                currentContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                currentContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));//强制下载
                currentContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                currentContext.Response.ContentType = "application/pdf";
                currentContext.Response.BinaryWrite(BynFile);
                System.IO.FileInfo file = new System.IO.FileInfo(filePath);
                if (file.Exists)
                {
                    file.Delete();
                }

                currentContext.Response.Flush();
                currentContext.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogMessage(string.Format("Method: {0},  Exception Message:{1}", "DownLoadPDF", ex.Message));
            }
        }

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;
        }

 

 

使用wkhtmltopdf tools将一个链接中的页面内容转换成PDF格式的文件,并下来保存到本地。

示例代码粗糙,请多见谅。

posted @ 2013-06-07 13:12  一切都是幸福叻  阅读(416)  评论(0编辑  收藏  举报