RenderingTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using QuestPDF.Elements;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. namespace QuestPDF.Examples.Engine
  9. {
  10. public enum RenderingTestResult
  11. {
  12. Pdf,
  13. Images
  14. }
  15. public class RenderingTest
  16. {
  17. private string FileNamePrefix = "test";
  18. private Size Size { get; set; }
  19. private int? MaxPagesThreshold { get; set; }
  20. private bool ShowResult { get; set; }
  21. private bool ApplyCaching { get; set; }
  22. private bool ApplyDebugging { get; set; }
  23. private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
  24. private RenderingTest()
  25. {
  26. }
  27. public static RenderingTest Create([CallerMemberName] string fileName = "test")
  28. {
  29. return new RenderingTest().FileName(fileName);
  30. }
  31. public RenderingTest FileName(string fileName)
  32. {
  33. FileNamePrefix = fileName;
  34. return this;
  35. }
  36. public RenderingTest PageSize(Size size)
  37. {
  38. Size = size;
  39. return this;
  40. }
  41. public RenderingTest PageSize(int width, int height)
  42. {
  43. return PageSize(new Size(width, height));
  44. }
  45. public RenderingTest ProducePdf()
  46. {
  47. ResultType = RenderingTestResult.Pdf;
  48. return this;
  49. }
  50. public RenderingTest ProduceImages()
  51. {
  52. ResultType = RenderingTestResult.Images;
  53. return this;
  54. }
  55. public RenderingTest ShowResults()
  56. {
  57. ShowResult = true;
  58. return this;
  59. }
  60. public RenderingTest MaxPages(int value)
  61. {
  62. MaxPagesThreshold = value;
  63. return this;
  64. }
  65. public RenderingTest EnableCaching(bool value = true)
  66. {
  67. ApplyCaching = value;
  68. return this;
  69. }
  70. public RenderingTest EnableDebugging(bool value = true)
  71. {
  72. ApplyDebugging = value;
  73. return this;
  74. }
  75. public void Render(Action<IContainer> content)
  76. {
  77. RenderDocument(container =>
  78. {
  79. container.Page(page =>
  80. {
  81. page.Size(new PageSize(Size.Width, Size.Height));
  82. page.Content().Container().Background(Colors.White).Element(content);
  83. });
  84. });
  85. }
  86. public void RenderDocument(Action<IDocumentContainer> content)
  87. {
  88. MaxPagesThreshold ??= ResultType == RenderingTestResult.Pdf ? 1000 : 10;
  89. var document = new SimpleDocument(content, MaxPagesThreshold.Value, ApplyCaching, ApplyDebugging);
  90. Render(document);
  91. }
  92. private void Render(IDocument document)
  93. {
  94. if (ResultType == RenderingTestResult.Images)
  95. {
  96. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  97. document.GenerateImages(fileNameSchema);
  98. if (ShowResult)
  99. Process.Start("explorer", fileNameSchema(0));
  100. }
  101. if (ResultType == RenderingTestResult.Pdf)
  102. {
  103. var fileName = $"{FileNamePrefix}.pdf";
  104. document.GeneratePdf(fileName);
  105. if (ShowResult)
  106. Process.Start("explorer", fileName);
  107. }
  108. }
  109. }
  110. }