如何在 ASP.NET MVC 中将视图转换为 PDF
视图是 ASP.NET 框架中的一个组件,用于在网络应用程序中生成 HTML 标记。它是模型-视图-控制器的一部分 (模型-视图-控制器 (MVC)) 模式,常用于 ASP.NET MVC 和 ASP.NET Core MVC 应用程序。视图负责通过动态呈现 HTML 内容向用户展示数据。
ASP.NET 网络应用程序 (.NET框架) MVC 是微软提供的一种网络应用程序框架。它遵循一种称为模型-视图-控制器(Model-View-Controller)的结构化架构模式。 (模型-视图-控制器 (MVC)) 来组织和简化网络应用程序的开发。
- 模型:管理数据、业务逻辑和数据完整性。
视图:呈现用户界面并渲染信息。
- 控制器:处理用户输入、处理请求并协调模型和视图之间的交互。
IronPDF 简化了从 ASP.NET MVC 项目中的视图创建 PDF 文件的过程。这使得在 ASP.NET MVC 中生成 PDF 变得简单而直接。
如何在 ASP.NET MVC 中将视图转换为 PDF
IronPDF 扩展包
IronPdf.Extensions.Mvc.Framework包是主包IronPdf**的扩展。IronPdf.Extensions.Mvc.Framework 和 IronPdf 包都是在 ASP.NET MVC 中渲染 PDF 文档视图所必需的。
PM > Install-Package IronPdf.Extensions.Mvc.Framework
安装使用 NuGet
安装包 IronPdf.Extensions.Mvc.Framework
将视图渲染为 PDF
要将视图转换为 PDF 文件,您需要一个 ASP.NET 网络应用程序 (.NET框架) MVC 项目。
添加模型类
- 导航至 "模型 "文件夹
- 创建一个名为 "Person "的新 C# 类文件。该类将作为表示个人数据的模型。使用以下代码片段:
:path=/static-assets/pdf/content-code-examples/how-to/cshtml-to-pdf-mvc-framework-model.cs
namespace ViewToPdfMVCSample.Models
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
Namespace ViewToPdfMVCSample.Models
Public Class Person
Public Property Id() As Integer
Public Property Name() As String
Public Property Title() As String
Public Property Description() As String
End Class
End Namespace
编辑控制器
导航至 "控制器 "文件夹并打开 "HomeController "文件。我们将添加 "人员 "操作。请参阅下面的代码以获得指导:
在提供的代码中,首先创建了ChromePdfRenderer类。要使用 "RenderView "方法,您需要为其提供一个HttpContext,指定 "Persons.cshtml "文件的路径,并提供一个包含必要数据的列表。在渲染视图时,用户可以选择使用 RenderingOptions 来自定义边距,添加 自定义文本和 HTML 页眉和页脚并应用 页码 到生成的 PDF 文档中。
请注意
File(pdf.BinaryData、"application/pdf"、"viewToPdfMVC.pdf")
.using IronPdf;
using System.Collections.Generic;
using System.Web.Mvc;
using ViewToPdfMVCSample.Models;
namespace ViewToPdfMVCSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
// GET: Person
public ActionResult Persons()
{
var persons = new List<Person>
{
new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
};
if (HttpContext.Request.HttpMethod == "POST")
{
// Provide the path to your View file
var viewPath = "~/Views/Home/Persons.cshtml";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render View to PDF document
PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
Response.Headers.Add("Content-Disposition", "inline");
// View the PDF
return File(pdf.BinaryData, "application/pdf");
}
return View(persons);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
using IronPdf;
using System.Collections.Generic;
using System.Web.Mvc;
using ViewToPdfMVCSample.Models;
namespace ViewToPdfMVCSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
// GET: Person
public ActionResult Persons()
{
var persons = new List<Person>
{
new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
};
if (HttpContext.Request.HttpMethod == "POST")
{
// Provide the path to your View file
var viewPath = "~/Views/Home/Persons.cshtml";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render View to PDF document
PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
Response.Headers.Add("Content-Disposition", "inline");
// View the PDF
return File(pdf.BinaryData, "application/pdf");
}
return View(persons);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Imports IronPdf
Imports System.Collections.Generic
Imports System.Web.Mvc
Imports ViewToPdfMVCSample.Models
Namespace ViewToPdfMVCSample.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
' GET: Person
Public Function Persons() As ActionResult
'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
Dim persons_Conflict = New List(Of Person) From {
New Person With {
.Name = "Alice",
.Title = "Mrs.",
.Description = "Software Engineer"
},
New Person With {
.Name = "Bob",
.Title = "Mr.",
.Description = "Software Engineer"
},
New Person With {
.Name = "Charlie",
.Title = "Mr.",
.Description = "Software Engineer"
}
}
If HttpContext.Request.HttpMethod = "POST" Then
' Provide the path to your View file
Dim viewPath = "~/Views/Home/Persons.cshtml"
Dim renderer As New ChromePdfRenderer()
' Render View to PDF document
Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict)
Response.Headers.Add("Content-Disposition", "inline")
' View the PDF
Return File(pdf.BinaryData, "application/pdf")
End If
Return View(persons_Conflict)
End Function
Public Function About() As ActionResult
ViewBag.Message = "Your application description page."
Return View()
End Function
Public Function Contact() As ActionResult
ViewBag.Message = "Your contact page."
Return View()
End Function
End Class
End Namespace
通过 "RenderView "方法获得PDFDocument对象后,您可以对其进行各种改进和调整。您可以将 PDF 转换为 PDF/A 或 PDFUA 格式、标志 数字签名 或 合并和拆分 PDF 文档。此外,该库还能让你旋转页面,插入 注释 或 书签和 打上明显的水印 到 PDF 文件中。
添加查看
- 右键单击新添加的人物操作,选择 "添加视图"。
- 为新的脚手架项目选择 "MVC 5 视图"。
- 选择 "列表 "模板和 "人员 "模型类。
这将创建一个名为 "人 "的 .cshtml 文件。
- 导航至 "Views "文件夹->"Home "文件夹->"Persons.cshtml "文件。
要添加一个调用 "Persons "操作的按钮,请使用下面的代码:
@using (Html.BeginForm("Persons", "Home", FormMethod.Post))
{
<input type="submit" value="Print Person" />
}
@using (Html.BeginForm("Persons", "Home", FormMethod.Post))
{
<input type="submit" value="Print Person" />
}
在顶部导航栏添加一个部分
- 在 "Views "文件夹中,导航至 "Shared "文件夹->"_Layout.cshtml "文件。将 "人员 "导航项放在 "主页 "之后。
确保 ActionLink 方法的值与我们的文件名(本例中为 "人员")完全匹配。
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark">
<div class="container">
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>
</ul>
</div>
</div>
</nav>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark">
<div class="container">
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" title="Toggle navigation" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li>@Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Persons", "Persons", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })</li>
</ul>
</div>
</div>
</nav>
运行项目
向您演示如何运行项目并生成 PDF 文档。
下载 ASP.NET MVC 项目
您可以下载本指南的完整代码。它是一个压缩文件,可以在 Visual Studio 中作为 ASP.NET Web 应用程序打开。 (.NET框架) MVC 项目。