边距
除了普通的展示pdf,我们还可以进行操作,例如下载,打印。当然如果宽和高不太满意,你可以对视图进行设置,其中有一个类是对视图进行配置的,其定义如下,有四大配置值。
public class Margins { [OptionFlag("-B")] public int? Bottom; [OptionFlag("-L")] public int? Left; [OptionFlag("-R")] public int? Right; [OptionFlag("-T")] public int? Top; public Margins(); public Margins(int top, int right, int bottom, int left); public override string ToString(); }
在控制器中直接new出它,然后直接return,和上面类似,现在你可以将html中的p标签添加一些内容,然后看一下效果。
[HttpGet] public IActionResult DemoViewAsPdf() { return new ViewAsPdf("DemoPageMarginsPDF") { PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 }, }; }
就这样,我们再次启动,可见已经有了外边距!
横向与纵向
它还给我们提供了横向还是竖向的pdf效果,如以下定义:
[HttpGet] public IActionResult DemoViewAsPdf(string Orientation) { if (Orientation == "Portrait") { var demoViewPortrait = new ViewAsPdf("DemoViewAsPDF") { FileName = "Invoice.pdf", PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait, }; return demoViewPortrait; } else { var demoViewLandscape = new ViewAsPdf("DemoViewAsPDF") { FileName = "Invoice.pdf", PageOrientation = Rotativa.AspNetCore.Options.Orientation.Landscape, }; return demoViewLandscape; } }
通过 http//localhost:60042/demo/DemoOrientationPDF?Orientation=Portrait 或者其它路由进行访问,你对比以下就可以看到效果。
设置PDF大小
基本上都是A4,枚举里很多值,自己看~
[HttpGet] public IActionResult DemoViewAsPdf(string Orientation) { return new ViewAsPdf("DemoPageSizePDF") { PageSize = Rotativa.AspNetCore.Options.Size.A4 }; }
小案例
创建一个模型,这是一个非常简单的模型,定义如下:
public class Customer { public int CustomerID { get; set; } public string Name { get; set; } public string Address { get; set; } public string Country { get; set; } public string City { get; set; } public string Phoneno { get; set; } }
在控制器中new几个对象,然后返回pdf。
[HttpGet] public IActionResult DemoViewAsPdf() { List<Customer> customerList = new List<Customer>() { new Customer { CustomerID = 1, Address = "Taj Lands Ends 1", City = "Mumbai" , Country ="India", Name ="Sai", Phoneno ="9000000000"}, new Customer { CustomerID = 2, Address = "Taj Lands Ends 2", City = "Mumbai" , Country ="India", Name ="Ram", Phoneno ="9000000000"}, new Customer { CustomerID = 3, Address = "Taj Lands Ends 3", City = "Mumbai" , Country ="India", Name ="Sainesh", Phoneno ="9000000000"}, new Customer { CustomerID = 4, Address = "Taj Lands Ends 4", City = "Mumbai" , Country ="India", Name ="Saineshwar", Phoneno ="9000000000"}, new Customer { CustomerID = 5, Address = "Taj Lands Ends 5", City = "Mumbai" , Country ="India", Name ="Saibags", Phoneno ="9000000000"} }; return new ViewAsPdf("DemoModelPDF", customerList); }
在视图中,我们只是迭代集合,渲染页面。
@model List<MvcHtmlToPdf.Models.Customer> @{ Layout = null; } <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div class="container"> <h2>Customer</h2> <p>Customer Details</p> <table class="table table-bordered"> <thead> <tr> <th>CustomerID</th> <th>Name</th> <th>Address</th> <th>Country</th> <th>City</th> <th>Phoneno</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.CustomerID</td> <td>@item.Name</td> <td>@item.Address</td> <td>@item.Country</td> <td>@item.City</td> <td>@item.Phoneno</td> </tr> } </tbody> </table> </div> </body> </html>
总结