分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

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

发布时间:2023-09-06 01:39责任编辑:蔡小小关键词:.NET
 //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)

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

原文地址:https://www.cnblogs.com/zhoupengbk/p/8360068.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved