学习无止境!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

第一步:生成PDF文件

 

protected void LinkButtonPrint_Click(object sender, EventArgs e)
        {
            try
            {
                                 string contractContentUrl = GetWebVirtualPath(HttpContext.Current) + "PrintPDF?kind=" + this.Kind.Replace("+", "%2B") + "&NO=" + this.No.ToString().Replace("+", "%2B");

                string pdfFilePath = FormHelp.GetFormConfig(this.FormKind, "PDF_FILE");
                string pdfFileName = pdfFilePath + Guid.NewGuid().ToString() + ".pdf";

                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(this.FormKind, output);
                }
                printProcess.WaitForExit();

                System.Threading.Thread.Sleep(500);

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

 

第二步:下载PDF文件

 

 

/// <summary>
        /// download pdf
        /// </summary>
        /// <param name="fileName"></param>
        private void DownLoadPDF(string fileName)
        {
            //string PDFFilePath = Server.MapPath("../PDFFile/") + Request.QueryString["FileName"].Trim() + ".PDF";
            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=" + Server.UrlEncode(ContractName + ".pdf"));
            //Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(BynFile);

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

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

通过下面的组件进行HTML导出PDF格式文件:

/Files/huanghai223/wkhtmltopdf.rar

 

posted on 2012-05-18 11:03  钻石眼泪  阅读(5673)  评论(1编辑  收藏  举报