【问题标题】:Return Pdf document from Asp.net MVC controller从 Asp.net MVC 控制器返回 Pdf 文档
【发布时间】:2012-07-18 14:44:22
【问题描述】:

从视图中的数据生成 html 页面的最佳方法是什么?我有一个包含所有表格等的 html 模板。不想使用任何模板,如 JqueryTemplate。

【问题讨论】:

  • 你为什么不想在 mvc synrax razor 中使用 build?还是你需要在客户端做?
  • 为什么你需要做客户端?为什么不生成pdf服务器端并将准备好的pdf作为字节返回?

标签: html asp.net-mvc pdf razor


【解决方案1】:

使用商业产品 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", 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/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, baseUrl);

        // send the PDF file to browser
        FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
        fileResult.FileDownloadName = "AboutMvcViewToPdf.pdf";

        return fileResult;
    }
}

本示例代码来源:How to convert HTML to PDF using HiQPDF

【讨论】:

  • 我正在搜索显示 pdf 文件 3 天。但是这个解决方案更好。谢谢
【解决方案2】:

只需创建 pdf 服务器端并返回 File 而不是 html 视图。 我不知道您使用哪种 pdf 提供程序,但这是 iTextSharp 的解决方案:

How to return PDF to browser in MVC?

【讨论】:

  • 好吧,你让我很满意。让我们从头开始。你的真实情况是什么?你想让我做什么?有许多不同的从控制器发送任何类型的数据到 html 页面有或没有控制器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 2023-02-11
  • 2013-02-01
  • 1970-01-01
相关资源
最近更新 更多