前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ASP.NET指定页面转PDF、JPG(插件)

ASP.NET指定页面转PDF、JPG(插件)

作者头像
小语雀网
发布2022-05-06 13:54:46
1.8K0
发布2022-05-06 13:54:46
举报
文章被收录于专栏:小语雀网
代码语言:javascript
复制
 //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;
        }
代码语言:javascript
复制
 //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;
        }
代码语言:javascript
复制
布局页面代码:
  <a>@Html.ActionLink("当前页面导出PDF", "pdfs")</a>
  <a>@Html.ActionLink("当前页面导出JPG", "jpgs")</a>

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档