在ASP.NET CORE下生成PDF文档

本文介绍如何使用Rotativa.AspNetCore库在ASP.NET Core MVC应用中创建PDF文档。通过ViewAsPdf方法将视图转换为PDF,并提供了示例代码及HTML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


原文链接:https://www.c-sharpcorner.com/article/creating-pdf-in-asp-net-core-mvc-using-rotativa-aspnetcore/

1528265575(1)

引入类库:Rotativa.AspNetCore

修改类Startup的方法Configure(IApplicationBuilder app, IHostingEnvironment env)

添加如下代码:

RotativaConfiguration.Setup(env);

调用ViewAsPdf 方法

public IActionResult Index()
{
     return new ViewAsPdf("Index");
}

zai www文件夹中添加必要的文件:

1528265872(1)

view 内容

@{
     Layout = null;
}

<!DOCTYPE html>

<html>
<head>
     <meta name="viewport" content="width=device-width" />
     <title>Index</title>
</head>
<body>
     <header>
         <h1>Invoice</h1>
         <address contenteditable>
             <p>Jonathan Neal</p>
             <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
             <p>(800) 555-1234</p>
         </address>
     </header>
     <article>
         <h1>Recipient</h1>
         <address contenteditable>
             <p>Demo Company<br>c/o Saineshwar</p>
         </address>
         <table class="meta">
             <tr>
                 <th><span contenteditable>Invoice #</span></th>
                 <td><span contenteditable>000001</span></td>
             </tr>
             <tr>
                 <th><span contenteditable>Date</span></th>
                 <td><span contenteditable>January 1, 2018</span></td>
             </tr>
             <tr>
                 <th><span contenteditable>Amount Due</span></th>
                 <td><span id="prefix" contenteditable>र </span><span>600.00</span></td>
             </tr>
         </table>
         <table class="inventory">
             <thead>
                 <tr>
                     <th><span contenteditable>Item</span></th>
                     <th><span contenteditable>Description</span></th>
                     <th><span contenteditable>Rate</span></th>
                     <th><span contenteditable>Quantity</span></th>
                     <th><span contenteditable>Price</span></th>
                 </tr>
             </thead>
             <tbody>
                 <tr>
                     <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
                     <td><span contenteditable>Experience Review</span></td>
                     <td><span data-prefix>र </span><span contenteditable>150.00</span></td>
                     <td><span contenteditable>4</span></td>
                     <td><span data-prefix>र </span><span>600.00</span></td>
                 </tr>
             </tbody>
         </table>

        <table class="balance">
             <tr>
                 <th><span contenteditable>Total</span></th>
                 <td><span data-prefix>र </span><span>600.00</span></td>
             </tr>
             <tr>
                 <th><span contenteditable>Amount Paid</span></th>
                 <td><span data-prefix>र </span><span contenteditable>0.00</span></td>
             </tr>
             <tr>
                 <th><span contenteditable>Balance Due</span></th>
                 <td><span data-prefix>र </span><span>600.00</span></td>
             </tr>
         </table>
     </article>

    <aside>
         <h1><span contenteditable>Additional Notes</span></h1>
         <div contenteditable>
             <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
         </div>
     </aside>
</body>
</html>

最终效果图:

1528331432(1)


转载于:https://www.cnblogs.com/mikechang/p/9148784.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值