zoukankan      html  css  js  c++  java
  • .net导出HTML为PDF格式文件

    第一步:生成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

     

  • 相关阅读:
    VMware安装最新版CentOS7图文教程
    git 本地给远程仓库创建分支 三步法
    git如何利用分支进行多人开发
    题解 洛谷P6478 [NOI Online #2 提高组] 游戏
    题解 CF1146D Frog Jumping
    题解 洛谷P6477 [NOI Online #2 提高组] 子序列问题
    题解 LOJ2472 「九省联考 2018」IIIDX
    题解 CF1340 A,B,C Codeforces Round #637 (Div. 1)
    题解 LOJ3284 「USACO 2020 US Open Platinum」Exercise
    windows上的路由表
  • 原文地址:https://www.cnblogs.com/huanghai223/p/2507434.html
Copyright © 2011-2022 走看看