JESAI.HtmlTemplate.Pdf
介绍JESAI.HtmlTemplate.Pdf是一个把Html模版转换PDF 或者把Razor渲染PDF,也可以把Razor生成动态html的插件。 使用说明直接nuget 安装JESAI.HtmlTemplate.Pdf.net45或者JESAI.HtmlTemplate.Pdf.Core包 1.新建一个数据Model。 namespace JESAI.Demo.Models{ [PdfExport(PaperKind = PaperKind.A4)] public class Student { public string Name { get; set; } public string Class { get; set; } public int Age { get; set; } public string Address { get; set; } public string Tel { get; set; } public string Sex { get; set; } public string Des { get; set; } }} 2.集成BaseController,并修改ActionResult返回 public class HomeController : BaseComtroller { private readonly ILogger<HomeController> _logger; private readonly ICacheService _cache; public HomeController(ILogger<HomeController> logger, ICacheService cache) { _logger = logger; _cache = cache; } public IActionResult GetPDF() { var m = new Student() { Name = "111111", Address = "3333333", Age = 22, Sex = "男", Tel = "19927352816", Des = "2222222222222222222" }; return PDFResult<Student>(m); }} 3.Strup启用插件 public void ConfigureServices(IServiceCollection services) { services.AddHtmlTemplateExportPdf(); services.AddControllersWithViews(); } 4.PDF的一些属性配置,包括页面大小,横竖排版等 public class PdfExportAttribute:Attribute {#if !NET461 &&!NET45 /// <summary> /// 方向 /// </summary> public Orientation Orientation { get; set; } = Orientation.Landscape;#else /// <summary> /// 方向 /// </summary> public PaperOrientation Orientation { get; set; } = PaperOrientation.Portrait;#endif /// <summary> /// 纸张类型(默认A4,必须) /// </summary> public PaperKind PaperKind { get; set; } = PaperKind.A4; /// <summary> /// 是否启用分页数 /// </summary> public bool IsEnablePagesCount { get; set; } /// <summary> /// 头部设置 /// </summary> public HeaderSettings HeaderSettings { get; set; } /// <summary> /// 底部设置 /// </summary> public FooterSettings FooterSettings { get; set; } /// <summary> /// 名称 /// </summary> public string Name { get; set; } /// <summary> /// 服务器是否保存一份 /// </summary> public bool IsEnableSaveFile { get; set; } = false; /// <summary> /// 保存路径 /// </summary> public string SaveFileRootPath { get; set; } = "D:\\PdfFile"; /// <summary> /// 是否缓存 /// </summary> public bool IsEnableCache { get; set; } = false; /// <summary> /// 缓存有效时间 /// </summary> public TimeSpan CacheTimeSpan { get; set; } = TimeSpan.FromMinutes(30); } 5.Razor 模版 <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head> <meta charset="utf-8" /> <title></title></head><body> <table border="1" style="background-color:red;width:800px;height:500px;"> <tr> <td>姓名</td> <td>@Model.Name</td> <td>性别</td> <td>@Model.Sex</td> </tr> <tr> <td>年龄</td> <td>@Model.Age</td> <td>班级</td> <td>@Model.Class</td> </tr> <tr> <td>住址</td> <td>@Model.Address</td> <td>电话</td> <td>@Model.Tel</td> </tr> <tr> <td clospan="2">住址</td> <td>@Model.Des</td> </tr> </table></body></html> 效果更新- 2020.6.11
- netcore3.X模版生成html报错问题解决
- RazorEngine.NetCore=>RazorEngineCore
|
请发表评论