Compare commits

..

4 Commits

5 changed files with 57 additions and 24 deletions
+4 -3
View File
@@ -45,13 +45,14 @@ namespace QuestPDF.Drawing
throw new ArgumentException("The library requires a Stream object with the 'seek' capability available (the CanSeek flag). Please consider using the MemoryStream class."); throw new ArgumentException("The library requires a Stream object with the 'seek' capability available (the CanSeek flag). Please consider using the MemoryStream class.");
} }
internal static ICollection<byte[]> GenerateImages(IDocument document) internal static ICollection<byte[]> GenerateImages(IDocument document, ImageGenerationSettings settings)
{ {
ValidateLicense(); ValidateLicense();
var settings = document.GetSettings(); var documentSettings = document.GetSettings();
documentSettings.ImageRasterDpi = settings.RasterDpi;
var canvas = new ImageCanvas(settings); var canvas = new ImageCanvas(settings);
RenderDocument(canvas, document, settings); RenderDocument(canvas, document, documentSettings);
return canvas.Images; return canvas.Images;
} }
+4 -4
View File
@@ -7,12 +7,12 @@ namespace QuestPDF.Drawing
{ {
internal class ImageCanvas : SkiaCanvasBase internal class ImageCanvas : SkiaCanvasBase
{ {
private DocumentSettings Settings { get; } private ImageGenerationSettings Settings { get; }
private SKSurface Surface { get; set; } private SKSurface Surface { get; set; }
internal ICollection<byte[]> Images { get; } = new List<byte[]>(); internal ICollection<byte[]> Images { get; } = new List<byte[]>();
public ImageCanvas(DocumentSettings settings) public ImageCanvas(ImageGenerationSettings settings)
{ {
Settings = settings; Settings = settings;
} }
@@ -30,7 +30,7 @@ namespace QuestPDF.Drawing
public override void BeginPage(Size size) public override void BeginPage(Size size)
{ {
var scalingFactor = Settings.ImageRasterDpi / (float) PageSizes.PointsPerInch; var scalingFactor = Settings.RasterDpi / (float) PageSizes.PointsPerInch;
var imageInfo = new SKImageInfo((int) (size.Width * scalingFactor), (int) (size.Height * scalingFactor)); var imageInfo = new SKImageInfo((int) (size.Width * scalingFactor), (int) (size.Height * scalingFactor));
Surface = SKSurface.Create(imageInfo); Surface = SKSurface.Create(imageInfo);
@@ -42,7 +42,7 @@ namespace QuestPDF.Drawing
public override void EndPage() public override void EndPage()
{ {
Canvas.Save(); Canvas.Save();
var image = Surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).ToArray(); var image = Surface.Snapshot().Encode(Settings.Format, Settings.Quality).ToArray();
Images.Add(image); Images.Add(image);
Canvas.Dispose(); Canvas.Dispose();
+20 -8
View File
@@ -75,30 +75,42 @@ namespace QuestPDF.Fluent
public static IEnumerable<byte[]> GenerateImages(this IDocument document) public static IEnumerable<byte[]> GenerateImages(this IDocument document)
{ {
return DocumentGenerator.GenerateImages(document); return document.GenerateImages(ImageGenerationSettings.Default);
} }
public static IEnumerable<byte[]> GenerateImages(this IDocument document, ImageGenerationSettings settings)
{
return DocumentGenerator.GenerateImages(document, settings);
}
/// <param name="filePath">Method should return fileName for given index</param> /// <param name="filePath">Method should return fileName for given index</param>
public static void GenerateImages(this IDocument document, Func<int, string> filePath) public static void GenerateImages(this IDocument document, Func<int, string> filePath)
{
document.GenerateImages(filePath, ImageGenerationSettings.Default);
}
/// <param name="filePath">Method should return fileName for given index</param>
public static void GenerateImages(this IDocument document, Func<int, string> filePath, ImageGenerationSettings settings)
{ {
var index = 0; var index = 0;
foreach (var imageData in document.GenerateImages()) foreach (var imageData in document.GenerateImages(settings))
{ {
var path = filePath(index); var path = filePath(index);
if (File.Exists(path)) if (File.Exists(path))
File.Delete(path); File.Delete(path);
File.WriteAllBytes(path, imageData); File.WriteAllBytes(path, imageData);
index++; index++;
} }
} }
#endregion #endregion
#region Helpers #region Helpers
private static void OpenFileUsingDefaultProgram(string filePath) private static void OpenFileUsingDefaultProgram(string filePath)
{ {
var process = new Process var process = new Process
+1 -9
View File
@@ -26,15 +26,7 @@ namespace QuestPDF.Helpers
static bool IsColorValid(string color) static bool IsColorValid(string color)
{ {
try return SKColor.TryParse(color, out _);
{
SKColor.Parse(color);
return true;
}
catch
{
return false;
}
} }
} }
} }
@@ -0,0 +1,28 @@
using SkiaSharp;
namespace QuestPDF.Infrastructure
{
public class ImageGenerationSettings
{
/// <summary>
/// The file format used to encode the image(s).
/// </summary>
public SKEncodedImageFormat Format { get; set; } = SKEncodedImageFormat.Png;
/// <summary>
/// The quality level to use for the image(s). This is in the range from 0-100.
/// </summary>
public int Quality { get; set; } = 100;
/// <summary>
/// The DPI (pixels-per-inch) at which images and features without native PDF support will be rasterized.
/// A larger DPI would create a PDF that reflects the original intent with better fidelity, but it can make for larger PDF files too, which would use more memory while rendering, and it would be slower to be processed or sent online or to printer.
/// When generating images, this parameter also controls the resolution of the generated content.
/// Default value is 144.
/// </summary>
public int RasterDpi { get; set; } = 144;
public static ImageGenerationSettings Default => new ImageGenerationSettings();
}
}