Refactor PDFBurner to use dependency injection

Replaced internal construction of dependencies with injected EGDbContext, ILogger, LicenseManager, AnnotationManager, and WorkerOptions. Removed BaseClass inheritance and internal fields. Updated annotation and PDF burning methods to use injected instances. Switched configuration from PDFBurnerParams to WorkerOptions.PDFBurnerOptions. Improves testability and aligns with DI best practices.
This commit is contained in:
2026-02-26 18:55:50 +01:00
parent 6fac1cd96a
commit 15a18b1bfd

View File

@@ -1,8 +1,5 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Drawing; using System.Drawing;
using System.IO;
using DigitalData.Modules.Base;
using DigitalData.Modules.Logging;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Infrastructure; using EnvelopeGenerator.Infrastructure;
using EnvelopeGenerator.PdfEditor; using EnvelopeGenerator.PdfEditor;
@@ -14,33 +11,21 @@ using Newtonsoft.Json;
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument; namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
public class PDFBurner : BaseClass //TODO: check if licence manager is needed as a dependency to
/// <summary>
///
/// </summary>
/// <param name="workerOptions"></param>
/// <param name="context2"></param>
/// <param name="logger2"></param>
/// <param name="licenseManager"></param>
/// <param name="annotationManager2"></param>
public class PDFBurner(IOptions<WorkerOptions> workerOptions, EGDbContext context, ILogger<PDFBurner> logger, LicenseManager licenseManager, AnnotationManager manager)
{ {
private readonly AnnotationManager _manager; private readonly WorkerOptions.PDFBurnerOptions _options = workerOptions.Value.PdfBurner;
private readonly LicenseManager _licenseManager;
private readonly DbContextOptions<EGDbContext> _dbContextOptions;
private readonly PDFBurnerParams _pdfBurnerParams;
public PDFBurner(LogConfig logConfig, string gdPictureLicenseKey, PDFBurnerParams pdfBurnerParams, string connectionString) : base(logConfig)
{
_licenseManager = new LicenseManager();
_licenseManager.RegisterKEY(gdPictureLicenseKey);
_manager = new AnnotationManager();
_pdfBurnerParams = pdfBurnerParams;
_dbContextOptions = new DbContextOptionsBuilder<EGDbContext>()
.UseSqlServer(connectionString)
.EnableDetailedErrors()
.EnableSensitiveDataLogging()
.Options;
}
public byte[] BurnAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList, int envelopeId) public byte[] BurnAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList, int envelopeId)
{ {
using var context = new EGDbContext(_dbContextOptions, Options.Create(new DbTriggerParams()));
var envelope = context.Envelopes.FirstOrDefault(env => env.Id == envelopeId); var envelope = context.Envelopes.FirstOrDefault(env => env.Id == envelopeId);
if (envelope is null) if (envelope is null)
{ {
@@ -69,7 +54,7 @@ public class PDFBurner : BaseClass
sourceBuffer = doc.Background(elements, 1.9500000000000002 * 0.93, 2.52 * 0.67).ExportStream().ToArray(); sourceBuffer = doc.Background(elements, 1.9500000000000002 * 0.93, 2.52 * 0.67).ExportStream().ToArray();
using var sourceStream = new MemoryStream(sourceBuffer); using var sourceStream = new MemoryStream(sourceBuffer);
var result = _manager.InitFromStream(sourceStream); var result = manager.InitFromStream(sourceStream);
if (result != GdPictureStatus.OK) if (result != GdPictureStatus.OK)
{ {
throw new BurnAnnotationException($"Could not open document for burning: [{result}]"); throw new BurnAnnotationException($"Could not open document for burning: [{result}]");
@@ -120,13 +105,13 @@ public class PDFBurner : BaseClass
} }
using var newStream = new MemoryStream(); using var newStream = new MemoryStream();
result = _manager.SaveDocumentToPDF(newStream); result = manager.SaveDocumentToPDF(newStream);
if (result != GdPictureStatus.OK) if (result != GdPictureStatus.OK)
{ {
throw new BurnAnnotationException($"Could not save document to stream: [{result}]"); throw new BurnAnnotationException($"Could not save document to stream: [{result}]");
} }
_manager.Close(); manager.Close();
return newStream.ToArray(); return newStream.ToArray();
} }
@@ -135,7 +120,7 @@ public class PDFBurner : BaseClass
public byte[] BurnInstantJSONAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList) public byte[] BurnInstantJSONAnnotsToPDF(byte[] sourceBuffer, List<string> instantJsonList)
{ {
using var sourceStream = new MemoryStream(sourceBuffer); using var sourceStream = new MemoryStream(sourceBuffer);
var result = _manager.InitFromStream(sourceStream); var result = manager.InitFromStream(sourceStream);
if (result != GdPictureStatus.OK) if (result != GdPictureStatus.OK)
{ {
throw new BurnAnnotationException($"Could not open document for burning: [{result}]"); throw new BurnAnnotationException($"Could not open document for burning: [{result}]");
@@ -149,26 +134,26 @@ public class PDFBurner : BaseClass
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogWarning("Error in AddInstantJSONAnnotationToPDF - oJson: "); logger.LogWarning("Error in AddInstantJSONAnnotationToPDF - oJson: ");
Logger.LogWarning(json); logger.LogWarning(json);
throw new BurnAnnotationException("Adding Annotation failed", ex); throw new BurnAnnotationException("Adding Annotation failed", ex);
} }
} }
result = _manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true); result = manager.BurnAnnotationsToPage(RemoveInitialAnnots: true, VectorMode: true);
if (result != GdPictureStatus.OK) if (result != GdPictureStatus.OK)
{ {
throw new BurnAnnotationException($"Could not burn annotations to file: [{result}]"); throw new BurnAnnotationException($"Could not burn annotations to file: [{result}]");
} }
using var newStream = new MemoryStream(); using var newStream = new MemoryStream();
result = _manager.SaveDocumentToPDF(newStream); result = manager.SaveDocumentToPDF(newStream);
if (result != GdPictureStatus.OK) if (result != GdPictureStatus.OK)
{ {
throw new BurnAnnotationException($"Could not save document to stream: [{result}]"); throw new BurnAnnotationException($"Could not save document to stream: [{result}]");
} }
_manager.Close(); manager.Close();
return newStream.ToArray(); return newStream.ToArray();
} }
@@ -185,7 +170,7 @@ public class PDFBurner : BaseClass
foreach (var annotation in annotationData.annotations) foreach (var annotation in annotationData.annotations)
{ {
Logger.LogDebug("Adding AnnotationID: " + annotation.id); logger.LogDebug("Adding AnnotationID: " + annotation.id);
switch (annotation.type) switch (annotation.type)
{ {
@@ -197,7 +182,7 @@ public class PDFBurner : BaseClass
break; break;
case AnnotationType.Widget: case AnnotationType.Widget:
var formFieldValue = annotationData.formFieldValues.FirstOrDefault(fv => fv.name == annotation.id); var formFieldValue = annotationData.formFieldValues.FirstOrDefault(fv => fv.name == annotation.id);
if (formFieldValue is not null && !_pdfBurnerParams.IgnoredLabels.Contains(formFieldValue.value)) if (formFieldValue is not null && !_options.IgnoredLabels.Contains(formFieldValue.value))
{ {
AddFormFieldValue(annotation, formFieldValue); AddFormFieldValue(annotation, formFieldValue);
} }
@@ -208,8 +193,8 @@ public class PDFBurner : BaseClass
private void AddImageAnnotation(double x, double y, double width, double height, int page, string base64) private void AddImageAnnotation(double x, double y, double width, double height, int page, string base64)
{ {
_manager.SelectPage(page); manager.SelectPage(page);
_manager.AddEmbeddedImageAnnotFromBase64(base64, (float) x, (float) y, (float) width, (float) height); manager.AddEmbeddedImageAnnotFromBase64(base64, (float) x, (float) y, (float) width, (float) height);
} }
private void AddImageAnnotation(Annotation annotation, Dictionary<string, Attachment> attachments) private void AddImageAnnotation(Annotation annotation, Dictionary<string, Attachment> attachments)
@@ -223,8 +208,8 @@ public class PDFBurner : BaseClass
var width = bounds[2]; var width = bounds[2];
var height = bounds[3]; var height = bounds[3];
_manager.SelectPage(annotation.pageIndex + 1); manager.SelectPage(annotation.pageIndex + 1);
_manager.AddEmbeddedImageAnnotFromBase64(attachment.Value.binary, (float) x, (float) y, (float) width, (float) height); manager.AddEmbeddedImageAnnotFromBase64(attachment.Value.binary, (float) x, (float) y, (float) width, (float) height);
} }
private void AddInkAnnotation(int page, string value) private void AddInkAnnotation(int page, string value)
@@ -237,12 +222,12 @@ public class PDFBurner : BaseClass
var segments = ink.lines.points; var segments = ink.lines.points;
var color = ColorTranslator.FromHtml(ink.strokeColor); var color = ColorTranslator.FromHtml(ink.strokeColor);
_manager.SelectPage(page); manager.SelectPage(page);
foreach (var segment in segments) foreach (var segment in segments)
{ {
var points = segment.Select(ToPointF).ToArray(); var points = segment.Select(ToPointF).ToArray();
_manager.AddFreeHandAnnot(color, points); manager.AddFreeHandAnnot(color, points);
} }
} }
@@ -250,24 +235,24 @@ public class PDFBurner : BaseClass
{ {
var segments = annotation.lines.points; var segments = annotation.lines.points;
var color = ColorTranslator.FromHtml(annotation.strokeColor); var color = ColorTranslator.FromHtml(annotation.strokeColor);
_manager.SelectPage(annotation.pageIndex + 1); manager.SelectPage(annotation.pageIndex + 1);
foreach (var segment in segments) foreach (var segment in segments)
{ {
var points = segment.Select(ToPointF).ToArray(); var points = segment.Select(ToPointF).ToArray();
_manager.AddFreeHandAnnot(color, points); manager.AddFreeHandAnnot(color, points);
} }
} }
private void AddFormFieldValue(double x, double y, double width, double height, int page, string value) private void AddFormFieldValue(double x, double y, double width, double height, int page, string value)
{ {
_manager.SelectPage(page); manager.SelectPage(page);
var annot = _manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, value); var annot = manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, value);
annot.FontName = _pdfBurnerParams.FontName; annot.FontName = _options.FontName;
annot.FontSize = _pdfBurnerParams.FontSize; annot.FontSize = _options.FontSize;
annot.FontStyle = _pdfBurnerParams.FontStyle; annot.FontStyle = _options.FontStyle;
_manager.SaveAnnotationsToPage(); manager.SaveAnnotationsToPage();
} }
private void AddFormFieldValue(Annotation annotation, FormFieldValue formFieldValue) private void AddFormFieldValue(Annotation annotation, FormFieldValue formFieldValue)
@@ -277,16 +262,16 @@ public class PDFBurner : BaseClass
var bounds = annotation.bbox.Select(ToInches).ToList(); var bounds = annotation.bbox.Select(ToInches).ToList();
var x = bounds[0]; var x = bounds[0];
var y = bounds[1] + _pdfBurnerParams.YOffset * ffIndex + _pdfBurnerParams.TopMargin; var y = bounds[1] + _options.YOffset * ffIndex + _options.TopMargin;
var width = bounds[2]; var width = bounds[2];
var height = bounds[3]; var height = bounds[3];
_manager.SelectPage(annotation.pageIndex + 1); manager.SelectPage(annotation.pageIndex + 1);
var annot = _manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, formFieldValue.value); var annot = manager.AddTextAnnot((float) x, (float) y, (float) width, (float) height, formFieldValue.value);
annot.FontName = _pdfBurnerParams.FontName; annot.FontName = _options.FontName;
annot.FontSize = _pdfBurnerParams.FontSize; annot.FontSize = _options.FontSize;
annot.FontStyle = _pdfBurnerParams.FontStyle; annot.FontStyle = _options.FontStyle;
_manager.SaveAnnotationsToPage(); manager.SaveAnnotationsToPage();
} }
private static PointF ToPointF(List<float> points) private static PointF ToPointF(List<float> points)