Test2Service.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Admin.NET.Application.Const;
  2. using Magicodes.ExporterAndImporter.Core;
  3. using Magicodes.ExporterAndImporter.Pdf;
  4. using Microsoft.AspNetCore.Authorization;
  5. using System.IO;
  6. namespace Admin.NET.Application.Service;
  7. /// <summary>
  8. /// 自己业务服务
  9. /// </summary>
  10. [ApiDescriptionSettings(TestConst.GroupName, Order = 200)]
  11. [AllowAnonymous]
  12. public class Test2Service : IDynamicApiController, ITransient
  13. {
  14. public Test2Service()
  15. {
  16. }
  17. /// <summary>
  18. /// 生成PDF文件
  19. /// </summary>
  20. /// <returns></returns>
  21. public async Task<dynamic> CreatePDFReport()
  22. {
  23. var tpl = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "temp/CovidReport.html"));
  24. var exporter = new PdfExporter();
  25. var pdfAtt = new PdfExporterAttribute();
  26. pdfAtt.Orientation = WkHtmlToPdfDotNet.Orientation.Portrait;
  27. pdfAtt.PaperKind = WkHtmlToPdfDotNet.PaperKind.A4;
  28. var result = await exporter.ExportBytesByTemplate(new ReportData
  29. {
  30. Name = "张三",
  31. IdNo = "130430xxxxxxxxxxxx",
  32. Sex = "男",
  33. Age = 35,
  34. TudeNo = "12345678901",
  35. CollectTime = "2022-08-01 12:33:33",
  36. ReceiveTime = "2022-08-01 18:33:33",
  37. CheckTime = "2022-08-02 18:33:33"
  38. }, pdfAtt, tpl);
  39. return new FileStreamResult(new MemoryStream(result), "application/octet-stream") { FileDownloadName = "核酸.PDF" };
  40. }
  41. }
  42. [Exporter(Name = "核酸报告")]
  43. public class ReportData
  44. {
  45. /// <summary>
  46. /// 姓名
  47. /// </summary>
  48. public string Name { get; set; }
  49. /// <summary>
  50. /// 身份证
  51. /// </summary>
  52. public string IdNo { get; set; }
  53. /// <summary>
  54. /// 性别
  55. /// </summary>
  56. public string Sex { get; set; }
  57. /// <summary>
  58. /// 年龄
  59. /// </summary>
  60. public int Age { get; set; }
  61. /// <summary>
  62. /// 试管编号
  63. /// </summary>
  64. public string TudeNo { get; set; }
  65. /// <summary>
  66. /// 采样时间
  67. /// </summary>
  68. public string CollectTime { get; set; }
  69. /// <summary>
  70. /// 收样时间
  71. /// </summary>
  72. public string ReceiveTime { get; set; }
  73. /// <summary>
  74. /// 检测时间
  75. /// </summary>
  76. public string CheckTime { get; set; }
  77. }