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)

关键词:jpg pdf response fs gt 页面 file string filename lt

相关推荐:

html2pdf 网页转PDF

How To Create And Download Zipped Files In .NET

在服务端实现下载的各种方式

ASP.NET C#根据HTML页面导出PDF

.net 下载文档

在Asp.Net中操作PDF – iTextSharp - 操作图片

Html导出Pdf

ASP.NET MVC文档下载

ItextSharp将多张pdf内容合并至一个pdf中

ASP.NET MVC中常用的ActionResult类型