Compare commits
4 Commits
main
..
feature/wip
| Author | SHA1 | Date | |
|---|---|---|---|
| 82bc1e72c6 | |||
| f20d7af0c5 | |||
| 3a73886d70 | |||
| 045b2a43b8 |
@@ -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.");
|
||||
}
|
||||
|
||||
internal static ICollection<byte[]> GenerateImages(IDocument document)
|
||||
internal static ICollection<byte[]> GenerateImages(IDocument document, ImageGenerationSettings settings)
|
||||
{
|
||||
ValidateLicense();
|
||||
|
||||
var settings = document.GetSettings();
|
||||
var documentSettings = document.GetSettings();
|
||||
documentSettings.ImageRasterDpi = settings.RasterDpi;
|
||||
var canvas = new ImageCanvas(settings);
|
||||
RenderDocument(canvas, document, settings);
|
||||
RenderDocument(canvas, document, documentSettings);
|
||||
|
||||
return canvas.Images;
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ namespace QuestPDF.Drawing
|
||||
{
|
||||
internal class ImageCanvas : SkiaCanvasBase
|
||||
{
|
||||
private DocumentSettings Settings { get; }
|
||||
private ImageGenerationSettings Settings { get; }
|
||||
private SKSurface Surface { get; set; }
|
||||
|
||||
internal ICollection<byte[]> Images { get; } = new List<byte[]>();
|
||||
|
||||
public ImageCanvas(DocumentSettings settings)
|
||||
public ImageCanvas(ImageGenerationSettings settings)
|
||||
{
|
||||
Settings = settings;
|
||||
}
|
||||
@@ -30,7 +30,7 @@ namespace QuestPDF.Drawing
|
||||
|
||||
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));
|
||||
|
||||
Surface = SKSurface.Create(imageInfo);
|
||||
@@ -42,7 +42,7 @@ namespace QuestPDF.Drawing
|
||||
public override void EndPage()
|
||||
{
|
||||
Canvas.Save();
|
||||
var image = Surface.Snapshot().Encode(SKEncodedImageFormat.Png, 100).ToArray();
|
||||
var image = Surface.Snapshot().Encode(Settings.Format, Settings.Quality).ToArray();
|
||||
Images.Add(image);
|
||||
|
||||
Canvas.Dispose();
|
||||
|
||||
@@ -75,30 +75,42 @@ namespace QuestPDF.Fluent
|
||||
|
||||
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>
|
||||
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;
|
||||
|
||||
foreach (var imageData in document.GenerateImages())
|
||||
|
||||
foreach (var imageData in document.GenerateImages(settings))
|
||||
{
|
||||
var path = filePath(index);
|
||||
|
||||
|
||||
if (File.Exists(path))
|
||||
File.Delete(path);
|
||||
|
||||
|
||||
File.WriteAllBytes(path, imageData);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Helpers
|
||||
|
||||
|
||||
private static void OpenFileUsingDefaultProgram(string filePath)
|
||||
{
|
||||
var process = new Process
|
||||
|
||||
@@ -26,15 +26,7 @@ namespace QuestPDF.Helpers
|
||||
|
||||
static bool IsColorValid(string color)
|
||||
{
|
||||
try
|
||||
{
|
||||
SKColor.Parse(color);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return SKColor.TryParse(color, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user