• C# wkhtmltopdf 将html转pdf(详解)


    https://www.cnblogs.com/louby/p/905198.html转自,看文章只放了代码看起来云里雾里的,在此做些解析

    使用说明:

    1.首先呢,得安装下软件,地址下面有链接,文件里有使用说明

    2.代码里安装路径得改下,改成你安装的路径,或者把wkhtmltopdf.exe 文件放在网站bin目录下

    3.设置下输出路径,转好的pdf得有地方存,代码里是放在了D盘,可以变动

    4.就可以使用啦,直接调用HtmlToPdf(string url) url为要转的html路径。

    一、转换程序代码如下:

     public string HtmlToPdf(string url)
            {
                bool success = true;
               // string dwbh = url.Split('?')[1].Split('=')[1];
                //CommonBllHelper.CreateUserDir(dwbh);
                //url = Request.Url.Host + "/html/" + url;
                string guid = DateTime.Now.ToString("yyyyMMddhhmmss");
                string pdfName =   "1.pdf";
                //string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName;
                string path = "D:\" + pdfName;
                try
                {
                    if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
                        success = false;
              //获取安装路径
                    string str = Server.MapPath("~\bin\wkhtmltopdf.exe");
                    Process p = System.Diagnostics.Process.Start(str, url+" "+path);
                    p.WaitForExit();
                    if (!System.IO.File.Exists(str))
                        success = false;
                    if (System.IO.File.Exists(path))
                    {
                        FileStream fs = new FileStream(path, FileMode.Open);
                        byte[] bytes = new byte[(int)fs.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        fs.Close();
                        if (Request.UserAgent != null)
                        {
                            string userAgent = Request.UserAgent.ToUpper();
                            if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
                            {
                                Response.AddHeader("Content-Disposition",
                                              "attachment;  filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8));
                            }
                            else
                            {
                                Response.AddHeader("Content-Disposition", "attachment;  filename=" + pdfName);
                            }
                        }
                        Response.ContentEncoding = Encoding.UTF8;
                        Response.ContentType = "application/octet-stream";
                        //通知浏览器下载文件而不是打开
                        Response.BinaryWrite(bytes);
                        Response.Flush();
                        Response.End();
                        fs.Close();
                        System.IO.File.Delete(path);
                    }
                    else
                    {
                        Response.Write("文件未找到,可能已经被删除");
                        Response.Flush();
                        Response.End();
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                }
                return "";
            }

    文件下载地址:https://download.csdn.net/download/licongzhuo/10591217

    一、转换程序代码如下: 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    <br>   public string HtmlToPdf(string url)
            {
                bool success = true;
               // string dwbh = url.Split('?')[1].Split('=')[1];
                //CommonBllHelper.CreateUserDir(dwbh);
                //url = Request.Url.Host + "/html/" + url;
                string guid = DateTime.Now.ToString("yyyyMMddhhmmss");
                string pdfName =   "1.pdf";
                //string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName;
                string path = "D:\" + pdfName;
                try
                {
                    if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
                        success = false;
              //获取安装路径
                    string str = Server.MapPath("~\bin\wkhtmltopdf.exe");
                    Process p = System.Diagnostics.Process.Start(str, url+" "+path);
                    p.WaitForExit();
                    if (!System.IO.File.Exists(str))
                        success = false;
                    if (System.IO.File.Exists(path))
                    {
                        FileStream fs = new FileStream(path, FileMode.Open);
                        byte[] bytes = new byte[(int)fs.Length];
                        fs.Read(bytes, 0, bytes.Length);
                        fs.Close();
                        if (Request.UserAgent != null)
                        {
                            string userAgent = Request.UserAgent.ToUpper();
                            if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0)
                            {
                                Response.AddHeader("Content-Disposition",
                                              "attachment;  filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8));
                            }
                            else
                            {
                                Response.AddHeader("Content-Disposition", "attachment;  filename=" + pdfName);
                            }
                        }
                        Response.ContentEncoding = Encoding.UTF8;
                        Response.ContentType = "application/octet-stream";
                        //通知浏览器下载文件而不是打开
                        Response.BinaryWrite(bytes);
                        Response.Flush();
                        Response.End();
                        fs.Close();
                        System.IO.File.Delete(path);
                    }
                    else
                    {
                        Response.Write("文件未找到,可能已经被删除");
                        Response.Flush();
                        Response.End();
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                }
                return "";
            }
  • 相关阅读:
    服务器编程入门(12) 守护进程
    Effective C++(10) 重载赋值操作符时,返回该对象的引用(retrun *this)
    C++ Primer(6) 模板和泛型编程(上)
    Effective C++(9) 构造函数调用virtual函数会发生什么
    Android开发(7)数据库和Content Provider
    Effective C++(7) 为多态基类声明virtual析构函数 or Not
    Effective C++(6) 如何拒绝编译器的自动生成函数
    Effective C++(4) 确定对象被使用前已先被初始化
    Effective C++(5) 了解C++默默地编写并调用哪些函数
    Effective C++(1-2) 编译器替换预处理器
  • 原文地址:https://www.cnblogs.com/licongzhuo/p/9443663.html
Copyright © 2020-2023  润新知