过去两天我尝试了多种工具来创建 PDF 文件。以前我使用过 iTextSharp,但在 .Net 4 的控制台应用程序中。现在我想在 Asp Net Core 中创建 PDF,当我尝试使用 iTextSharp 时,我发现还有其他可以使用的 nuget 包。我安装了它们并进行了编码,但有很多引用错误。还尝试了其他几个工具,但要么我在运行时或设计时遇到问题,有一次我的程序崩溃了,并且在运行时我不得不撤消我的更改。
有人可以指导我任何适合 Asp .Net Core 3.1 的教程吗?它可以是从 html 或从对象列表创建 PDF 的示例。
最佳答案
也推荐Rotativa
,感觉用起来很方便。我做了一个demo,大家可以引用一下。
1.从nuget下载Rotativa.AspNetCore
。
2.我们需要在wwwroot中添加一个名为“Rotativa”的新文件夹并在此文件夹内,然后添加
wkhtmltopdf.exe
、wkhtmltoimage.exe 文件
。
您可以从 GitHub 示例演示中获取这两个文件
https://github.com/webgio/Rotativa.AspNetCore/tree/master/Rotativa.AspNetCore.Demo/wwwroot/Rotativa
3.在 Startup.cs 中进行配置
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
var hostingEnvironment = app.ApplicationServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>();
RotativaConfiguration.Setup(hostingEnvironment);
4.在您的 Controller 操作中,只需将 returnType 更改为 ViewAsPdf
,然后它将以 pdf 形式显示 View 。
public IActionResult Index()
{
var users = new List<User>()
{
new User{ Id = 1, Name = "AA", Address = "Address1"},
new User{ Id = 2, Name = "BB", Address = "Address2"},
new User{ Id = 3, Name = "CC", Address = "Address3"},
new User{ Id = 4, Name = "DD", Address = "Address4"},
};
return new ViewAsPdf(users);
}
索引 View :
@model IEnumerable<User>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
结果:
如果你想下载它,你只需给它一个文件名
return new ViewAsPdf(users)
{
FileName="MyPdf.pdf"
};
更新:
行动:
public IActionResult DemoViewAsPDF()
{
var users = new List<User>()
{
new User{ Id = 1, Name = "AA", Address = "Address1"},
new User{ Id = 2, Name = "BB", Address = "Address2"},
new User{ Id = 3, Name = "CC", Address = "Address3"},
new User{ Id = 4, Name = "DD", Address = "Address4"},
};
ViewData["Data"] = "ViewDataValue";
ViewBag.Data2 = "ViewBagValue";
return new ViewAsPdf(users, ViewData);
}
查看:
@model IEnumerable<User>
@{
ViewData["Title"] = "Index";
}
<h1>@ViewData["Data"]</h1>
<h1>@ViewBag.Data2</h1>
<h1>@ViewData["Title"]</h1>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
结果:
关于asp.net-core - MVC网络核心3.1 : Tool to create and download PDF file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64804670/