.net导出HTML为PDF格式文件

本文介绍了一种使用.NET将HTML转换为PDF的方法。首先通过wkhtmltopdf工具将指定URL的HTML内容生成PDF文件,然后提供下载功能。此过程涉及设置wkhtmltopdf路径、构建进程参数、启动进程并读取输出等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:生成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 钻石眼泪 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/huanghai223/archive/2012/05/18/2507434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值