diff --git a/QuestPDF.Examples/ImageExamples.cs b/QuestPDF.Examples/ImageExamples.cs index 89801bf..ad24b57 100644 --- a/QuestPDF.Examples/ImageExamples.cs +++ b/QuestPDF.Examples/ImageExamples.cs @@ -21,8 +21,14 @@ namespace QuestPDF.Examples page.Padding(25).Stack(stack => { stack.Spacing(25); - stack.Item().Image(File.ReadAllBytes("logo.png")); + stack.Item().Image("logo.png"); + + var binaryData = File.ReadAllBytes("logo.png"); + stack.Item().Image(binaryData); + + using var stream = new FileStream("logo.png", FileMode.Open); + stack.Item().Image(stream); }); }); } diff --git a/QuestPDF.ReportSample/DataSource.cs b/QuestPDF.ReportSample/DataSource.cs index 062cea3..6f92802 100644 --- a/QuestPDF.ReportSample/DataSource.cs +++ b/QuestPDF.ReportSample/DataSource.cs @@ -113,8 +113,8 @@ namespace QuestPDF.ReportSample Date = DateTime.Now - TimeSpan.FromDays(Helpers.Random.NextDouble() * 100), Location = Helpers.RandomLocation(), - MapContextSource = x => Placeholders.Image(400, 300), - MapDetailsSource = x => Placeholders.Image(400, 300) + MapContextSource = Placeholders.Image, + MapDetailsSource = Placeholders.Image }; } } diff --git a/QuestPDF.ReportSample/Layouts/ImagePlaceholder.cs b/QuestPDF.ReportSample/Layouts/ImagePlaceholder.cs new file mode 100644 index 0000000..0a2a63a --- /dev/null +++ b/QuestPDF.ReportSample/Layouts/ImagePlaceholder.cs @@ -0,0 +1,20 @@ +using QuestPDF.Fluent; +using QuestPDF.Helpers; +using QuestPDF.Infrastructure; + +namespace QuestPDF.ReportSample.Layouts +{ + public class ImagePlaceholder : IComponent + { + public static bool Solid { get; set; } = false; + + public void Compose(IContainer container) + { + if (Solid) + container.Background(Placeholders.Color()); + + else + container.Image(Placeholders.Image); + } + } +} \ No newline at end of file diff --git a/QuestPDF.ReportSample/Layouts/ImageTemplate.cs b/QuestPDF.ReportSample/Layouts/ImageTemplate.cs deleted file mode 100644 index 0e3eca8..0000000 --- a/QuestPDF.ReportSample/Layouts/ImageTemplate.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using QuestPDF.Fluent; -using QuestPDF.Helpers; -using QuestPDF.Infrastructure; - -namespace QuestPDF.ReportSample.Layouts -{ - public class ImageTemplate : IComponent - { - private Func Source { get; } - private float AspectRatio { get; } - - public ImageTemplate(byte[] source, float aspectRatio = 1.333333f) : this(_ => source, aspectRatio) - { - - } - - public ImageTemplate(Func source, float aspectRatio = 1.333333f) - { - Source = source; - AspectRatio = aspectRatio; - } - - public void Compose(IContainer container) - { - container - .AspectRatio(AspectRatio) - .Background(Colors.Grey.Lighten3) - .Image(Source(Size.Zero)); - } - } -} \ No newline at end of file diff --git a/QuestPDF.ReportSample/Layouts/PhotoTemplate.cs b/QuestPDF.ReportSample/Layouts/PhotoTemplate.cs index f2a4c7d..d8fa483 100644 --- a/QuestPDF.ReportSample/Layouts/PhotoTemplate.cs +++ b/QuestPDF.ReportSample/Layouts/PhotoTemplate.cs @@ -30,14 +30,14 @@ namespace QuestPDF.ReportSample.Layouts container .Row(row => { - row.RelativeColumn(2).AspectRatio(4 / 3f).Image(Placeholders.Image); + row.RelativeColumn(2).AspectRatio(4 / 3f).Component(); row.RelativeColumn().PaddingLeft(5).Stack(stack => { stack.Spacing(7f); - stack.Item().AspectRatio(4 / 3f).Image(Placeholders.Image); - stack.Item().AspectRatio(4 / 3f).Image(Placeholders.Image); + stack.Item().AspectRatio(4 / 3f).Component(); + stack.Item().AspectRatio(4 / 3f).Component(); }); }); } diff --git a/QuestPDF.ReportSample/Layouts/SectionTemplate.cs b/QuestPDF.ReportSample/Layouts/SectionTemplate.cs index 8142da9..98514d0 100644 --- a/QuestPDF.ReportSample/Layouts/SectionTemplate.cs +++ b/QuestPDF.ReportSample/Layouts/SectionTemplate.cs @@ -59,7 +59,7 @@ namespace QuestPDF.ReportSample.Layouts { stack.Spacing(5); - stack.Item().MaxWidth(250).AspectRatio(4 / 3f).Image(Placeholders.Image); + stack.Item().MaxWidth(250).AspectRatio(4 / 3f).Component(); stack.Item().Text(model.Location.Format()); }); } @@ -77,7 +77,7 @@ namespace QuestPDF.ReportSample.Layouts grid.Spacing(5); grid.Columns(3); - model.Photos.ForEach(x => grid.Item().AspectRatio(4 / 3f).Image(Placeholders.Image)); + model.Photos.ForEach(x => grid.Item().AspectRatio(4 / 3f).Component()); }); } } diff --git a/QuestPDF.ReportSample/Tests.cs b/QuestPDF.ReportSample/Tests.cs index 089ad2e..fc5edd3 100644 --- a/QuestPDF.ReportSample/Tests.cs +++ b/QuestPDF.ReportSample/Tests.cs @@ -15,7 +15,7 @@ namespace QuestPDF.ReportSample { var reportModel = DataSource.GetReport(); var report = new StandardReport(reportModel); - + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf"); report.GeneratePdf(path); @@ -23,13 +23,16 @@ namespace QuestPDF.ReportSample } [Test] - public void PerformanceBenchmark() + public void PerformanceBenchmark() { // target document length should be around 100 pages // test size const int testSize = 10; const decimal performanceTarget = 1; // documents per second + + // generating placeholder images is slow, for benchmarking reasons, replace images with simple colorful boxes + ImagePlaceholder.Solid = true; // create report models var reports = Enumerable @@ -45,9 +48,14 @@ namespace QuestPDF.ReportSample var sw = new Stopwatch(); sw.Start(); - var totalSize = reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length); + var totalSize = BenchmarkAndGenerate(); sw.Stop(); + long BenchmarkAndGenerate() + { + return reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);; + } + // show summary Console.WriteLine($"Total size: {totalSize:N0} bytes");