zoukankan      html  css  js  c++  java
  • ASP.NET指定页面转PDF、JPG(插件)

     //PDF文件导出
            public ActionResult pdfs() {
                
                //导出页面的路径(死路径)
                string url = "http://localhost:1213/";
                //插件的路径(转换为pdfNE)
                string pdf = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe";
    
                //随机生成一个文件名称
                string filename = Guid.NewGuid().ToString();
                //pdf格式
                string pdfpath = filename + ".pdf";
                Process p = System.Diagnostics.Process.Start(pdf, url + " "" + Server.MapPath(pdfpath) + """);
                p.WaitForExit();
                
                //下载
                FileStream fs = new FileStream(Server.MapPath(pdfpath), FileMode.Open);
                byte[] file = new byte[fs.Length];
                fs.Read(file, 0, file.Length);
                fs.Close();
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".pdf");//以二进制流模式,强制下载 
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(file);
                Response.Write("<script>window.location='Index.cshtml'</script>");
                return null;
            }
     //JPG文件导出
            public ActionResult jpgs()
            {
                //导出页面的路径
                string url = "http://localhost:1213/";
                //插件的路径(转换为jpg)
                string jpg = "C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe";
    
                //随机生成一个文件名称
                string filename = Guid.NewGuid().ToString();
                //jpg格式
                string pdfpath = filename + ".jpg";
                Process p = System.Diagnostics.Process.Start(jpg, url + " "" + Server.MapPath(pdfpath) + """);
                p.WaitForExit();
    
                //下载
                FileStream fs = new FileStream(Server.MapPath(pdfpath), FileMode.Open);
                byte[] file = new byte[fs.Length];
                fs.Read(file, 0, file.Length);
                fs.Close();
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".jpg");//以二进制流模式,强制下载 
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(file);
                Response.Write("<script>window.location='Index.cshtml'</script>");
                return null;
            }
    布局页面代码:
      <a>@Html.ActionLink("当前页面导出PDF", "pdfs")</a>
      <a>@Html.ActionLink("当前页面导出JPG", "jpgs")</a>

    转PDF、JPG插件(wkhtmltox-0.12.4_msvc2015-win64.exe)

    注:强推一波个人小站:小语雀网 | 欢迎大佬们访问哈~
  • 相关阅读:
    属性选择器(通常用在input)
    函数调用的文档注释
    List集合操作
    数组排序三种方法
    字符串反序输出字符串
    js中完美运动框架
    查找100-200之间是否存在水仙花数
    提示用户输入一个正整数,如果错误,则重新输入,可以使用以下的代码来保证用户输入正确:
    Ubuntu 16.10下的 jdk 1.8.0_111
    方法内部类
  • 原文地址:https://www.cnblogs.com/zpblogs/p/8360068.html
Copyright © 2011-2022 走看看