RenderingTest.cs 3.7 KB

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