【问题标题】:How to export HTML to PDF with ASP.net mvc4?如何使用 ASP.net mvc4 将 HTML 导出为 PDF?
【发布时间】:2016-09-30 15:14:47
【问题描述】:

我有一份使用 HTML 使用 asp.net mvc4 开发的报告,我想使用 asp.net MVC4 将此报告导出为 PDF 文件,有人帮帮我吗?谢谢!

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    使用 Razor PDF 生成 PDF 报告。这是一个简单的过程。

    第 1 步 - 创建 MVC 应用程序

    第 2 步 - 安装 Rotativa PDF Nuget。

    第 3 步 - 放置一个像这样的简单模型

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    

    第 4 步 - 为我们在上面创建的模型创建一个简单视图,将视图命名为 Index。

    @model IEnumerable<WebApplication1.Controllers.Person>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Age)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
        </tr>
    }
    
    </table>
    

    第 5 步 - 创建一个简单的控制器动作。

        public ActionResult IndexNew()
        {
            return new ActionAsPdf("GeneratePDF");
        }
    
        public ActionResult GeneratePDF()
        {
            List<Person> persons = new List<Person>();
            persons.Add(new Person() { Age = "29", Name = "Rami1" });
            persons.Add(new Person() { Age = "28", Name = "Rami2" });
            return View("Index", persons);
        }
    

    运行应用程序并导航到 IndexNew Controller Action,一个 PDF 将从 GeneratePDF() Action 中生成并由浏览器下载,如下所示 -

    【讨论】:

    • 在顶部,您说您正在使用 RazorPDF,稍后又说您正在使用 Rotativa,您真的在这个示例中同时使用了吗?
    【解决方案2】:

    看看这个应用程序:

    http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC

    如果您想直接下载文件,您可以使用 FileContentResult:

    protected FileContentResult ViewPdf(string pageTitle, string viewName, object model)
    {
    string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
    byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
    return File(buffer, "application/pdf","file.pdf");
    }
    

    【讨论】:

    • 感谢您的回答!
    【解决方案3】:

    如果报告是同一应用程序中的另一个视图,您可以使用以下 C# 获取该视图的 HTML 字符串,然后将 HTML 字符串转换为 PDF 以在缓冲区中生成 PDF,该缓冲区可以保存在服务器上或发送到浏览器下载。该代码使用evo html to pdf converter for .net 将HTML 字符串转换为PDF。这种方法的优点是会话数据在转换期间可以在报告视图中使用:

    [HttpPost]
    public ActionResult ConvertPageInSameSessionToPdf(FormCollection collection)
    {
        object model = null;
        ViewDataDictionary viewData = new ViewDataDictionary(model);
    
        // The string writer where to render the HTML code of the view
        StringWriter stringWriter = new StringWriter();
    
        // Render the Index view in a HTML string
        ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Report_View", null);
        ViewContext viewContext = new ViewContext(
                ControllerContext,
                viewResult.View,
                viewData,
                new TempDataDictionary(),
                stringWriter
                );
        viewResult.View.Render(viewContext, stringWriter);
    
        // Get the view HTML string
        string htmlToConvert = stringWriter.ToString();
    
        // Get the base URL
        String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
        String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Report_View".Length);
    
        // Convert the HTML string to a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);
    
        // Send the PDF file to browser
        FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
        fileResult.FileDownloadName = "Report.pdf";
    
        return fileResult;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2013-11-21
      • 2017-01-14
      • 2015-01-31
      • 1970-01-01
      • 2015-09-25
      • 2013-08-25
      • 2021-03-26
      • 1970-01-01
      相关资源
      最近更新 更多