• .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

     

  • 相关阅读:
    常见问题|一起工作 高端互联网人才兼职平台
    一拍即合
    食茶_尼尼龙_美愿作品展示平台
    Cop-Out
    员工宝
    java~使用自己的maven本地仓库
    java~接口的共享实体使用Map后更灵活
    知其所以然~tcp和udp的区别
    知其所以然~mongodb副本集
    java--map容器的hashcode和equals
  • 原文地址:https://www.cnblogs.com/huanghai223/p/2507434.html
Copyright © 2020-2023  润新知