html – 从Asp.net MVC控制器返回Pdf文档

html – 从Asp.net MVC控制器返回Pdf文档,第1张

概述从视图中的数据生成html页面的最佳方法是什么?我有一个包含所有表格等的html模板.不想使用像 JqueryTemplate这样的任何模板. 使用商业产品hiqpdf html to pdf converter尝试这种方法: public class HomeController : Controller{ public ActionResult Index() { 从视图中的数据生成HTML页面的最佳方法是什么?我有一个包含所有表格等的HTML模板.不想使用像 JqueryTemplate这样的任何模板.解决方法 使用商业产品hiqpdf html to pdf converter尝试这种方法:

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文档所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1081273.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存