asp.net-core - MVC网络核心3.1 : Tool to create and download PDF file

标签 asp.net-core pdf

过去两天我尝试了多种工具来创建 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.exewkhtmltoimage.exe 文件

enter image description here

您可以从 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>

结果:

enter image description here

如果你想下载它,你只需给它一个文件名

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>

结果:

enter image description here

关于asp.net-core - MVC网络核心3.1 : Tool to create and download PDF file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64804670/

相关文章:

iis - IIS 上的 ASP.NET Core 2.0 错误 502.5

css - Magstrip 字体无法从 PDF 正确打印

python - 将 PDF 转换为图像的省时方法

image - PDF 内嵌图像不起作用

javascript - 有没有一个好的网络应用程序来操作pdf文件?

c# - 添加具有不同程序集的迁移

.net - 无法为.NET Core应用程序创建Docker镜像

c# - .Net Core 1.1 中不再发现 XUnit 测试

c# - .Net 核心中 Json() 的小写属性名称

php - 创建html表单并稍后以pdf格式打印并填充数据?