How to generate a pdf documet in ASP.NET Core 3.0 like the below image with parameters?

荒凉一梦 提交于 2020-08-11 00:14:49

问题


How to create a PDF like this by supplying the relevant parameters and generating a PDF document based on those parameters


回答1:


Easily Create a PDF Document in ASP.NET Core Web API

Reference Link




回答2:


You could try to use Rotativa.AspNetCore.Here are the steps:

1.Download the NuGet package – Rotativa.AspNetCore

2.You need download two additional files that doesn’t come with NuGet package – wkhtmltoimage.exe and wkhtmltopdf.exe.

The two files you could download from github:https://github.com/webgio/Rotativa.AspNetCore. It located in Rotativa.AspNetCore/Rotativa.AspNetCore.Demo/wwwroot/Rotativa/

3.Put them to Rotativa folder under wwwroot folder like shown below:

Before we can start with PFD we have to tell in configuration where those additional files are located.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{        
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    RotativaConfiguration.Setup(env.WebRootPath, "Rotativa");

    app.UseRouting();

    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {              
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    });      
}

Model:

public class Invoice
{
    public Invoice()
    {
        Lines = new List<InvoiceLine>();
    }

    public string Number { get; set; }       
    public string  Company { get; set; }
    public DateTime Date { get; set; }
    public IList<InvoiceLine> Lines { get; set; }

    public decimal Total
    {
        get
        {
            return Lines.Sum(l => l.Price);
        }
    }

    public static Invoice GetOne()
    {
        var invoice = new Invoice();
        invoice.Number = "10203";
        invoice.Date = DateTime.Parse("2019-7-8");
        invoice.Company = "ACME Corp";

        var line = new InvoiceLine();
        line.Hour = 12;
        line.Date = DateTime.Parse("2019-7-8");
        line.Unit = 800;
        line.Price = 20;
        invoice.Lines.Add(line);

        line = new InvoiceLine();
        line.Hour = 5;
        line.Date = DateTime.Parse("2019-9-8");
        line.Unit = 200;
        line.Price = 20;
        invoice.Lines.Add(line);

        line = new InvoiceLine();
        line.Hour = 30;
        line.Date = DateTime.Parse("2019-7-1");
        line.Unit = 50;
        line.Price = 20;
        invoice.Lines.Add(line);
        return invoice;
    }
}
public class InvoiceLine
{
    public int Hour { get; set; }
    public DateTime Date { get; set; }
    public decimal Unit { get; set; }
    public decimal Price { get; set; }
}

View:

@model Invoice
@{
    Layout = null;
}
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Invoice</title>
    <link rel="stylesheet" href="@Url.Content("~/css/invoice-report.css")" />
</head>
<body>
    <table>
        <tr>
            <td>Number:</td>
            <td class="numeric-cell">@Model.Number</td>
        </tr>
        <tr>
            <td>Date:</td>
            <td class="numeric-cell">@Model.Date</td>
        </tr>
        <tr>
            <td>Company:</td>
            <td class="numeric-cell">@Model.Company</td>
        </tr>
    </table>

    <table>
        <tr>
            <th>Date</th>
            <th>Hour</th>
            <th>Unit</th>
            <th>Price</th>
        </tr>
        @foreach (var line in Model.Lines)
        {
            <tr>
                <td>@line.Date</td>
                <td class="numeric-cell">@line.Hour</td>
                <td class="numeric-cell">@line.Unit.ToString("0.00")</td>
                <td class="numeric-cell">@line.Price.ToString("0.00")</td>
            </tr>
        }
        <tr>
            <td colspan="2"></td>
            <td><strong>Total:</strong></td>
            <td><strong>@Model.Total.ToString("0.00")</strong></td>
        </tr>
    </table>
</body>
</html>

<style>
    body {
        margin: 0px;
        font-family: 'Segoe UI', Arial;
    }
    table {
        clear: both;
        border: 1px solid darkgray;
        border-collapse: collapse;
        margin: auto;
    }

    td, th {
        border: 1px solid darkgray;
        padding: 5px;
    }   
</style>

Controller:

public IActionResult Index()
{
    return new ViewAsPdf("Index", Invoice.GetOne());
}

Result:



来源:https://stackoverflow.com/questions/61839229/how-to-generate-a-pdf-documet-in-asp-net-core-3-0-like-the-below-image-with-para

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!