Files
QuestPDF/QuestPDF.Examples/Engine/RenderingTest.cs
T

133 lines
3.7 KiB
C#
Raw Normal View History

2021-06-09 23:34:25 +02:00
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
2021-06-09 23:34:25 +02:00
using QuestPDF.Elements;
using QuestPDF.Fluent;
2021-10-23 02:59:47 +02:00
using QuestPDF.Helpers;
2021-06-09 23:34:25 +02:00
using QuestPDF.Infrastructure;
namespace QuestPDF.Examples.Engine
{
2021-08-09 22:35:39 +02:00
public enum RenderingTestResult
{
Pdf,
Images
}
2021-06-09 23:34:25 +02:00
public class RenderingTest
{
private string FileNamePrefix = "test";
private Size Size { get; set; }
2021-10-29 01:23:46 +02:00
private int? MaxPagesThreshold { get; set; }
2021-08-26 22:25:33 +02:00
private bool ShowResult { get; set; }
2021-12-25 11:53:39 +01:00
private bool ApplyCaching { get; set; }
private bool ApplyDebugging { get; set; }
2021-08-09 22:35:39 +02:00
private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
2022-03-15 13:06:09 +01:00
private bool ShowingResultsEnabled = true;
2021-06-09 23:34:25 +02:00
private RenderingTest()
{
}
2021-10-23 02:59:47 +02:00
public static RenderingTest Create([CallerMemberName] string fileName = "test")
2021-06-09 23:34:25 +02:00
{
2021-10-23 02:59:47 +02:00
return new RenderingTest().FileName(fileName);
2021-06-09 23:34:25 +02:00
}
2021-10-23 02:59:47 +02:00
public RenderingTest FileName(string fileName)
2021-06-09 23:34:25 +02:00
{
FileNamePrefix = fileName;
return this;
}
2021-08-25 03:40:16 +02:00
public RenderingTest PageSize(Size size)
{
Size = size;
return this;
}
2021-06-09 23:34:25 +02:00
public RenderingTest PageSize(int width, int height)
{
2021-08-25 03:40:16 +02:00
return PageSize(new Size(width, height));
2021-06-09 23:34:25 +02:00
}
2021-08-09 22:35:39 +02:00
public RenderingTest ProducePdf()
{
ResultType = RenderingTestResult.Pdf;
return this;
}
public RenderingTest ProduceImages()
{
ResultType = RenderingTestResult.Images;
return this;
}
2021-08-26 22:25:33 +02:00
public RenderingTest ShowResults(bool value = true)
2021-08-26 22:25:33 +02:00
{
ShowResult = value;
2021-08-26 22:25:33 +02:00
return this;
}
2021-06-09 23:34:25 +02:00
2021-12-16 01:39:26 +01:00
public RenderingTest MaxPages(int value)
{
MaxPagesThreshold = value;
return this;
}
2021-12-25 11:53:39 +01:00
public RenderingTest EnableCaching(bool value = true)
{
ApplyCaching = value;
return this;
}
public RenderingTest EnableDebugging(bool value = true)
{
ApplyDebugging = value;
return this;
}
2021-06-09 23:34:25 +02:00
public void Render(Action<IContainer> content)
{
2021-10-23 02:59:47 +02:00
RenderDocument(container =>
{
container.Page(page =>
{
page.Size(new PageSize(Size.Width, Size.Height));
page.Content().Container().Background(Colors.White).Element(content);
});
});
}
2021-08-26 22:25:33 +02:00
2021-10-23 02:59:47 +02:00
public void RenderDocument(Action<IDocumentContainer> content)
{
2021-10-29 01:23:46 +02:00
MaxPagesThreshold ??= ResultType == RenderingTestResult.Pdf ? 1000 : 10;
2021-12-25 11:53:39 +01:00
var document = new SimpleDocument(content, MaxPagesThreshold.Value, ApplyCaching, ApplyDebugging);
2021-06-09 23:34:25 +02:00
2021-10-23 02:59:47 +02:00
Render(document);
}
private void Render(IDocument document)
{
2021-08-09 22:35:39 +02:00
if (ResultType == RenderingTestResult.Images)
{
Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
document.GenerateImages(fileNameSchema);
2021-08-26 22:25:33 +02:00
2022-03-15 13:06:09 +01:00
if (ShowResult && ShowingResultsEnabled)
2021-08-26 22:25:33 +02:00
Process.Start("explorer", fileNameSchema(0));
2021-08-09 22:35:39 +02:00
}
if (ResultType == RenderingTestResult.Pdf)
{
var fileName = $"{FileNamePrefix}.pdf";
document.GeneratePdf(fileName);
2021-08-26 22:25:33 +02:00
2022-03-15 13:06:09 +01:00
if (ShowResult && ShowingResultsEnabled)
2021-08-26 22:25:33 +02:00
Process.Start("explorer", fileName);
2021-08-09 22:35:39 +02:00
}
2021-06-09 23:34:25 +02:00
}
}
}