DocumentPreviewerExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Diagnostics;
  2. using System.Reflection;
  3. using Microsoft.AspNetCore.Builder;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Helpers;
  6. using QuestPDF.Infrastructure;
  7. namespace QuestPDF.Previewer;
  8. public static class DocumentPreviewerExtensions
  9. {
  10. public static void ShowInPreviewer(this IDocument document)
  11. {
  12. ArgumentNullException.ThrowIfNull(document);
  13. var builder = WebApplication.CreateBuilder();
  14. var app = builder.Build();
  15. var pdfDocumentCache = GeneratePdf(document);
  16. var refreshFlag = false;
  17. static byte[] GenerateDocumentAboutException(Exception exception)
  18. {
  19. return Document.Create(document =>
  20. {
  21. document.Page(page =>
  22. {
  23. page.Size(PageSizes.A4);
  24. page.Margin(1, Unit.Inch);
  25. page.PageColor(Colors.Red.Lighten4);
  26. page.DefaultTextStyle(x => x.FontSize(16));
  27. page.Header()
  28. .BorderBottom(2)
  29. .BorderColor(Colors.Red.Medium)
  30. .PaddingBottom(5)
  31. .Text("Ooops! Something went wrong...").FontSize(28).FontColor(Colors.Red.Medium).Bold();
  32. page.Content().PaddingVertical(20).Column(column =>
  33. {
  34. var currentException = exception;
  35. while (currentException != null)
  36. {
  37. column.Item().Text(exception.GetType().Name).FontSize(20).SemiBold();
  38. column.Item().Text(exception.Message).FontSize(14);
  39. column.Item().PaddingTop(10).Text(exception.StackTrace).FontSize(10).Light();
  40. currentException = currentException.InnerException;
  41. if (currentException != null)
  42. column.Item().PaddingVertical(15).LineHorizontal(2).LineColor(Colors.Red.Medium);
  43. }
  44. });
  45. });
  46. }).GeneratePdf();
  47. }
  48. static byte[] GeneratePdf(IDocument document)
  49. {
  50. try
  51. {
  52. return document.GeneratePdf();
  53. }
  54. catch(Exception exception)
  55. {
  56. return GenerateDocumentAboutException(exception);
  57. }
  58. }
  59. HotReloadManager.OnApplicationChanged += () =>
  60. {
  61. pdfDocumentCache = GeneratePdf(document);
  62. refreshFlag = true;
  63. };
  64. app.MapGet("/", () =>
  65. {
  66. var assembly = Assembly.GetExecutingAssembly();
  67. var resourceName = "QuestPDF.Previewer.index.html";
  68. using var stream = assembly.GetManifestResourceStream(resourceName);
  69. using var reader = new StreamReader(stream);
  70. var result = reader.ReadToEnd();
  71. return Results.Content(result, "text/html");
  72. });
  73. app.MapGet("/render", () =>
  74. {
  75. refreshFlag = false;
  76. return Results.File(pdfDocumentCache, "application/pdf");
  77. });
  78. app.MapGet("/listen", async () =>
  79. {
  80. foreach (var i in Enumerable.Range(0, 1000))
  81. {
  82. await Task.Delay(TimeSpan.FromMilliseconds(100));
  83. if (!refreshFlag)
  84. continue;
  85. return Results.Text("true");
  86. }
  87. return Results.Text("false");
  88. });
  89. app.Lifetime.ApplicationStarted.Register(() =>
  90. {
  91. var openBrowserProcess = new Process()
  92. {
  93. StartInfo = new()
  94. {
  95. UseShellExecute = true,
  96. FileName = app.Urls.First()
  97. }
  98. };
  99. openBrowserProcess.Start();
  100. });
  101. app.Run();
  102. }
  103. }