在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF。如果您使用ASP.NET MVC,那么Rot​​ativa工具已经可用,我们可以使用它来生成pdf。

创建一个MVC项目,无论您是core或不core,然后nuget下包.命令如下:

  1. Install-Package Rotativa
  2. #或者
  3. Install-Package Rotativa.AspNetCore

这个工具由意大利人Giorgio Bozio创建。他需要在ASP.NET MVC中生成pdf,并且重复的任务是设置一种方法来创建PDF文档,用于业务流程或报告,下面废话不多说,我们开始吧。

在startup.cs类中配置Rotativa.AspNetCore设置

我们在Configure方法内的startup.cs类中添加此设置,以设置要访问的wkhtmltopdf.exe文件的相对路径。

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  2. {
  3. RotativaConfiguration.Setup(env);
  4. }

 我们需要在wwwroot中添加Rotativa文件夹,然后放入这两个exe,我把这两个文件已经放到了百度云盘。

然后我们添加一个Demo控制器,定义一个Get方法,其定义如下,通过ViewAsPdf方法,就可以通过pdf的形式去套住cshtml,也就达到了pdf的效果。

  1. public class DemoController : Controller
  2. {
  3. [HttpGet]
  4. public IActionResult DemoViewAsPdf()
  5. {
  6. return new ViewAsPdf("DemoViewAsPdf");
  7. }
  8. }

 就现在,我们需要通过控制器去创建一个视图,然后在视图中有如下定义:

  1. @{
  2. ViewData["Title"] = "DemoViewAsPdf";
  3. }
  4. <html>
  5. <head>
  6. <meta charset="utf-8">
  7. <title>Demo</title>
  8. </head>
  9. <body>
  10. <p>Hello AspNetCore!!</p>
  11. </body>
  12. </html>

现在,我们把页面重定与

  1. http://localhost:55999/Demo/DemoViewAsPdf

除了普通的展示pdf,我们还可以进行操作,例如下载,打印。当然如果宽和高不太满意,你可以对视图进行设置,其中有一个类是对视图进行配置的,其定义如下,有四大配置值。

  1. public class Margins
  2. {
  3. [OptionFlag("-B")]
  4. public int? Bottom;
  5. [OptionFlag("-L")]
  6. public int? Left;
  7. [OptionFlag("-R")]
  8. public int? Right;
  9. [OptionFlag("-T")]
  10. public int? Top;
  11. public Margins();
  12. public Margins(int top, int right, int bottom, int left);
  13. public override string ToString();
  14. }

在控制器中直接new出它,然后直接return,和上面类似,现在你可以将html中的p标签添加一些内容,然后看一下效果。

  1. [HttpGet]
  2. public IActionResult DemoViewAsPdf()
  3. {
  4. return new ViewAsPdf("DemoPageMarginsPDF")
  5. {
  6. PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
  7. };
  8. }

 就这样,我们再次启动,可见已经有了外边距!

它还给我们提供了横向还是竖向的pdf效果,如以下定义:

  1. [HttpGet]
  2. public IActionResult DemoViewAsPdf(string Orientation)
  3. {
  4. if (Orientation == "Portrait")
  5. {
  6. var demoViewPortrait = new ViewAsPdf("DemoViewAsPDF")
  7. {
  8. FileName = "Invoice.pdf",
  9. PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
  10. };
  11. return demoViewPortrait;
  12. }
  13. else
  14. {
  15. var demoViewLandscape = new ViewAsPdf("DemoViewAsPDF")
  16. {
  17. FileName = "Invoice.pdf",
  18. PageOrientation = Rotativa.AspNetCore.Options.Orientation.Landscape,
  19. };
  20. return demoViewLandscape;
  21. }
  22. }

通过 http//localhost:60042/demo/DemoOrientationPDF?Orientation=Portrait 或者其它路由进行访问,你对比以下就可以看到效果。

 基本上都是A4,枚举里很多值,自己看~

  1. [HttpGet]
  2. public IActionResult DemoViewAsPdf(string Orientation)
  3. {
  4. return new ViewAsPdf("DemoPageSizePDF")
  5. {
  6. PageSize = Rotativa.AspNetCore.Options.Size.A4
  7. };
  8. }

 创建一个模型,这是一个非常简单的模型,定义如下:

  1. public class Customer
  2. {
  3. public int CustomerID { get; set; }
  4. public string Name { get; set; }
  5. public string Address { get; set; }
  6. public string Country { get; set; }
  7. public string City { get; set; }
  8. public string Phoneno { get; set; }
  9. }

在控制器中new几个对象,然后返回pdf。

  1. [HttpGet]
  2. public IActionResult DemoViewAsPdf()
  3. {
  4. List<Customer> customerList = new List<Customer>() {
  5. new Customer { CustomerID = 1, Address = "Taj Lands Ends 1", City = "Mumbai" , Country ="India", Name ="Sai", Phoneno ="9000000000"},
  6. new Customer { CustomerID = 2, Address = "Taj Lands Ends 2", City = "Mumbai" , Country ="India", Name ="Ram", Phoneno ="9000000000"},
  7. new Customer { CustomerID = 3, Address = "Taj Lands Ends 3", City = "Mumbai" , Country ="India", Name ="Sainesh", Phoneno ="9000000000"},
  8. new Customer { CustomerID = 4, Address = "Taj Lands Ends 4", City = "Mumbai" , Country ="India", Name ="Saineshwar", Phoneno ="9000000000"},
  9. new Customer { CustomerID = 5, Address = "Taj Lands Ends 5", City = "Mumbai" , Country ="India", Name ="Saibags", Phoneno ="9000000000"}
  10. };
  11. return new ViewAsPdf("DemoModelPDF", customerList);
  12. }

在视图中,我们只是迭代集合,渲染页面。

 

  1. @model List<MvcHtmlToPdf.Models.Customer>
  2. @{
  3. Layout = null;
  4. }
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <title>Bootstrap Example</title>
  9. <meta charset="utf-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. </head>
  12. <body>
  13. <div class="container">
  14. <h2>Customer</h2>
  15. <p>Customer Details</p>
  16. <table class="table table-bordered">
  17. <thead>
  18. <tr>
  19. <th>CustomerID</th>
  20. <th>Name</th>
  21. <th>Address</th>
  22. <th>Country</th>
  23. <th>City</th>
  24. <th>Phoneno</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. @foreach (var item in Model)
  29. {
  30. <tr>
  31. <td>@item.CustomerID</td>
  32. <td>@item.Name</td>
  33. <td>@item.Address</td>
  34. <td>@item.Country</td>
  35. <td>@item.City</td>
  36. <td>@item.Phoneno</td>
  37. </tr>
  38. }
  39. </tbody>
  40. </table>
  41. </div>
  42. </body>
  43. </html>

 

版权声明:本文为ZaraNet原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ZaraNet/p/10417056.html