RenderingTest.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 bool ShowingResultsEnabled = true;
  25. private RenderingTest()
  26. {
  27. }
  28. public static RenderingTest Create([CallerMemberName] string fileName = "test")
  29. {
  30. return new RenderingTest().FileName(fileName);
  31. }
  32. public RenderingTest FileName(string fileName)
  33. {
  34. FileNamePrefix = fileName;
  35. return this;
  36. }
  37. public RenderingTest PageSize(Size size)
  38. {
  39. Size = size;
  40. return this;
  41. }
  42. public RenderingTest PageSize(int width, int height)
  43. {
  44. return PageSize(new Size(width, height));
  45. }
  46. public RenderingTest ProducePdf()
  47. {
  48. ResultType = RenderingTestResult.Pdf;
  49. return this;
  50. }
  51. public RenderingTest ProduceImages()
  52. {
  53. ResultType = RenderingTestResult.Images;
  54. return this;
  55. }
  56. public RenderingTest ShowResults()
  57. {
  58. ShowResult = true;
  59. return this;
  60. }
  61. public RenderingTest MaxPages(int value)
  62. {
  63. MaxPagesThreshold = value;
  64. return this;
  65. }
  66. public RenderingTest EnableCaching(bool value = true)
  67. {
  68. ApplyCaching = value;
  69. return this;
  70. }
  71. public RenderingTest EnableDebugging(bool value = true)
  72. {
  73. ApplyDebugging = value;
  74. return this;
  75. }
  76. public void Render(Action<IContainer> content)
  77. {
  78. RenderDocument(container =>
  79. {
  80. container.Page(page =>
  81. {
  82. page.Size(new PageSize(Size.Width, Size.Height));
  83. page.Content().Container().Background(Colors.White).Element(content);
  84. });
  85. });
  86. }
  87. public void RenderDocument(Action<IDocumentContainer> content)
  88. {
  89. var document = new SimpleDocument(content, ApplyCaching, ApplyDebugging);
  90. Render(document);
  91. }
  92. public void Render(IDocument document)
  93. {
  94. if (ResultType == RenderingTestResult.Images)
  95. {
  96. var generationSettings = new ImageGenerationSettings { RasterDpi = 144 };
  97. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  98. document.GenerateImages(index => fileNameSchema(index), generationSettings);
  99. if (ShowResult && ShowingResultsEnabled)
  100. {
  101. var firstImagePath = fileNameSchema(0);
  102. Helpers.Helpers.OpenFileUsingDefaultProgram(firstImagePath);
  103. }
  104. }
  105. if (ResultType == RenderingTestResult.Pdf)
  106. {
  107. document.GeneratePdfAndShow();
  108. }
  109. }
  110. }
  111. }