public class HomeController : Controller{ public ActionResult Index() { VIEwBag.Message = "Welcome to ASP.NET MVC!"; Session["MySessionVariable"] = "My Session Variable Value assigned in Index"; return VIEw(); } public ActionResult About() { return VIEw(); } public string RenderVIEwAsstring(string vIEwname,object model) { // create a string writer to receive the HTML code StringWriter stringWriter = new StringWriter(); // get the vIEw to render VIEwEngineResult vIEwResult = VIEwEngines.Engines.FindVIEw(ControllerContext,vIEwname,null); // create a context to render a vIEw based on a model VIEwContext vIEwContext = new VIEwContext( ControllerContext,vIEwResult.VIEw,new VIEwDataDictionary(model),new TempDataDictionary(),stringWriter ); // render the vIEw to a HTML code vIEwResult.VIEw.Render(vIEwContext,stringWriter); // return the HTML code return stringWriter.ToString(); } [httpPost] public ActionResult ConvertThisPagetopdf() { // get the HTML code of this vIEw string HTMLToConvert = RenderVIEwAsstring("Index",null); // the base URL to resolve relative images and CSS String thisPageUrl = this.ControllerContext.httpContext.Request.Url.absoluteUri; String baseUrl = thisPageUrl.Substring(0,thisPageUrl.Length - "Home/ConvertThisPagetopdf".Length); // instantiate the HiQpdf HTML to pdf converter HTMLTopdf HTMLtopdfconverter = new HTMLTopdf(); // hIDe the button in the created pdf HTMLtopdfconverter.HIDdenHTMLElements = new string[] { "#convertThisPagebuttondiv" }; // render the HTML code as pdf in memory byte[] pdfBuffer = HTMLtopdfconverter.ConvertHTMLToMemory(HTMLToConvert,baseUrl); // send the pdf file to browser fileResult fileResult = new fileContentResult(pdfBuffer,"application/pdf"); fileResult.fileDownloadname = "ThisMvcVIEwTopdf.pdf"; return fileResult; } [httpPost] public ActionResult ConvertAboutPagetopdf() { // get the About vIEw HTML code string HTMLToConvert = RenderVIEwAsstring("About",thisPageUrl.Length - "Home/ConvertAboutPagetopdf".Length); // instantiate the HiQpdf HTML to pdf converter HTMLTopdf HTMLtopdfconverter = new HTMLTopdf(); // render the HTML code as pdf in memory byte[] pdfBuffer = HTMLtopdfconverter.ConvertHTMLToMemory(HTMLToConvert,"application/pdf"); fileResult.fileDownloadname = "AboutMvcVIEwTopdf.pdf"; return fileResult; }}
此示例代码的来源:How to convert HTML to PDF using HiQPDF
总结以上是内存溢出为你收集整理的html – 从Asp.net MVC控制器返回Pdf文档全部内容,希望文章能够帮你解决html – 从Asp.net MVC控制器返回Pdf文档所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)