详细内容
打印方法
发布日期:2011-09-20     点击:2634     字体:[ ]

这里的打印方法是弹出PDF文件,里面用到了一个exe文件:wkhtmltopdf.rar

引用的命名空间:

using System.Text;
using System.Diagnostics;
using System.IO;

以及两个重要方法

        /// <summary>
        /// 打印pdf文件
        /// </summary>
        public void PrintPDF(string currentPageUrl, string printPageUrl, string headId)
        {
            try
            {
                string currentUrl = Request.Url.ToString().ToLower();
                string requestPath = currentUrl.Substring(0, currentUrl.IndexOf(currentPageUrl.ToLower()));
                string contractContentUrl = requestPath + printPageUrl + "?";

                string urlPara = "{0}={1}&LOGIN_LANGUAGE={2}&IS_PRINT=1";
                urlPara = string.Format(urlPara, ID, headId,
                    SecurityHelper.EncryptString(this.Passport.LanguageCode));

                if (Request.QueryString["FORM_NO"] != null)
                {
                    urlPara = "{0}={1}&LOGIN_LANGUAGE={2}&FORM_NO={3}&IS_PRINT=1";
                    urlPara = string.Format(urlPara, ID, headId,
                       this.Passport.LanguageCode, Request.QueryString["FORM_NO"].ToString());
                }

                contractContentUrl += urlPara;
                string pdfFilePath = Server.MapPath(Request.ApplicationPath);
                string pdfFileName = pdfFilePath + @"\" + Guid.NewGuid();

                string path = Server.MapPath(Request.ApplicationPath);
                string pdfConverter = path + @"\bin\wkhtmltopdf.exe";
                if (!System.IO.File.Exists(pdfConverter))
                    return;

                Process printProcess = new Process();
                printProcess.StartInfo.FileName = pdfConverter;
                string printArguments = "\"{0}\" \"{1}\"";

                contractContentUrl = contractContentUrl.Replace("https:", "http:");
                printArguments = string.Format(printArguments, contractContentUrl, pdfFileName);
                printProcess.StartInfo.Arguments = printArguments;
                printProcess.StartInfo.UseShellExecute = false;
                printProcess.StartInfo.RedirectStandardInput = true;
                printProcess.StartInfo.RedirectStandardOutput = true;
                printProcess.StartInfo.RedirectStandardError = true;
                printProcess.StartInfo.CreateNoWindow = false;
                printProcess.Start();
                string output = printProcess.StandardOutput.ReadToEnd();
                if (!string.IsNullOrEmpty(output))
                {
                    //LogHelp.WriteInfoLog(FormKind, output);
                }
                printProcess.WaitForExit();

                System.Threading.Thread.Sleep(500);

                DownLoadPDF(pdfFileName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// download pdf
        /// </summary>
        /// <param name="fileName"></param>
        private void DownLoadPDF(string fileName)
        {
            FileStream fs = new FileStream(fileName, 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();

            Response.Buffer = true;
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(this.TPFormTitle)) + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
            Response.ContentType = "application/cdf";
            Response.BinaryWrite(BynFile);

            System.IO.FileInfo file = new System.IO.FileInfo(fileName);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            Response.Flush();
            Response.End();
        }

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计