【问题标题】:MVC Net Core 3.1: Tool to create and download PDF file [closed]MVC Net Core 3.1:创建和下载 PDF 文件的工具 [关闭]
【发布时间】:2020-11-12 13:19:00
【问题描述】:

从过去 2 天开始,我尝试了多种工具来创建 PDF 文件。以前我使用过 iTextSharp,但在 .Net 4 的控制台应用程序中。现在我想在 Asp Net Core 中创建 PDF,当我尝试使用 iTextSharp 时,我发现还有其他可以使用的 nuget 包。我安装了它们并进行了编码,但我有很多参考错误。还尝试了其他几个工具,但我在运行时或设计时遇到问题,有一次我的程序在运行时崩溃了,我不得不撤消我的更改。

有人可以指导我学习任何适用于 Asp .Net Core 3.1 的教程吗?它可以是从 html 或从对象列表创建 PDF 的示例。

【问题讨论】:

    标签: asp.net-core pdf


    【解决方案1】:

    也推荐Rotativa,感觉用起来很方便。我做了一个demo,大家可以参考一下。

    1.从nuget下载Rotativa.AspNetCore

    2.我们需要在 wwwroot 中添加一个名为“Rotativa”的新文件夹,并在该文件夹内,然后添加 wkhtmltopdf.exe, wkhtmltoimage.exe files.

    您可以从 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.在您的控制器操作中,只需将returnType更改为ViewAsPdf,然后它将以pdf格式显示视图。

    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);
    }
    

    索引视图:

    @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>
    

    结果:

    【讨论】:

    • 您好 mj1313,我使用了 Rotativa,但当前版本在 asp.net 3.1 上不起作用。搜索后我不得不使用测试版,然后我能够成功地创建 pdf 文件。尽管如此,我还是遇到了一个问题,即我无法在视图中访问 viewbag。在这里查看帖子stackoverflow.com/questions/59995626/…
    • 嗨@Faisal,如果你想在pdf中启用ViewBag或ViewData,你也需要返回ViewData。查看我的更新。
    • 非常感谢。 mj1313,它很有帮助,我能够得到 View.Bag。就像我在 bsod 中提到和提到的那样,为了让我做对,我必须获得 1.2.0 的 beta 版本。
    • 您好 mj1313,感谢您展示如何传递 ViewBag。您知道如何将页面方向更改为纵向吗?由于列数很多,在垂直视图中不适合页面。
    【解决方案2】:

    看看 Rotativa(个人可以说效果很好)-

    https://www.nuget.org/packages/Rotativa.AspNetCore/1.2.0-beta

    GitHub 页面有一个如何开始的教程 -

    https://github.com/webgio/Rotativa

    【讨论】:

      【解决方案3】:

      对于 .net 核心,我转而使用 PuppeteerSharp 从 HTML 生成 PDF 文件:

      https://www.puppeteersharp.com/

      我相信现在 iTextSharp 需要许可证,除非使用的是旧版本。

      我也使用过 PdfSharp(另一个免费库,但不相信它支持 .net 核心。将来可能需要注意。这比 iTextSharp 效果很好,而且更容易使用。

      【讨论】:

      • 嗨,马克,我没有尝试 PuppeteerSharp,因为我使用的是 asp.net 3.1。我检查了它们最后一次更新的文件是在 2017 年,而 asp.net 3.1 是在 2019 年底发布的。由于我已经尝试过很少的其他第三个工具来生成 PDF 并且发现了问题,所以我使用了 Rotativa,但我会尝试当我有空的时候。谢谢
      【解决方案4】:
      猜你喜欢
      • 2013-11-27
      • 2021-04-29
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2020-07-11
      • 2017-07-27
      • 2021-01-04
      • 1970-01-01
      相关资源
      最近更新 更多