Skip to content

Commit

Permalink
Feat svg output format (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinZiabek committed May 15, 2024
1 parent 61f5310 commit 95637a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Source/QuestPDF/Drawing/DocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ internal static ICollection<byte[]> GenerateImages(IDocument document, ImageGene

return canvas.Images;
}

internal static ICollection<string> GenerateSvg(IDocument document)
{
ValidateLicense();

var canvas = new SvgCanvas();
RenderDocument(canvas, document, document.GetSettings());

return canvas.Images;
}

private static void ValidateLicense()
{
Expand Down
5 changes: 5 additions & 0 deletions Source/QuestPDF/Drawing/ImageCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public ImageCanvas(ImageGenerationSettings settings)
Settings = settings;
}

~ImageCanvas()
{
Bitmap?.Dispose();
}

public override void BeginDocument()
{

Expand Down
51 changes: 51 additions & 0 deletions Source/QuestPDF/Drawing/SvgCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using QuestPDF.Drawing.Exceptions;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Skia;

namespace QuestPDF.Drawing
{
internal sealed class SvgCanvas : SkiaCanvasBase
{
internal SkWriteStream WriteStream { get; set; }
internal ICollection<string> Images { get; } = new List<string>();

~SvgCanvas()
{
WriteStream?.Dispose();
}

public override void BeginDocument()
{

}

public override void EndDocument()
{

}

public override void BeginPage(Size size)
{
WriteStream?.Dispose();
WriteStream = new SkWriteStream();
Canvas = SkSvgCanvas.CreateSvg(size.Width, size.Height, WriteStream);
}

public override void EndPage()
{
Canvas.Save();
Canvas.Dispose();

using var data = WriteStream.DetachData();
var svgImage = Encoding.UTF8.GetString(data.ToBytes());
Images.Add(svgImage);

WriteStream.Dispose();
}
}
}
12 changes: 12 additions & 0 deletions Source/QuestPDF/Fluent/GenerateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,17 @@ public static void GenerateImages(this IDocument document, GenerateDocumentImage
}

#endregion

#region SVG

/// <summary>
/// Generates the document as a series of SVG images and returns them as a collection of strings.
/// </summary>
public static ICollection<string> GenerateSvg(this IDocument document)
{
return DocumentGenerator.GenerateSvg(document);
}

#endregion
}
}

0 comments on commit 95637a0

Please sign in to comment.