refactor(BurnPdfCommand): add Report property to forward report.
- Reverse the order in which behaviors are added.
This commit is contained in:
parent
958bcdfc42
commit
e68965543e
@ -87,11 +87,13 @@ public static class DependencyInjection
|
|||||||
services.AddMediatR(cfg =>
|
services.AddMediatR(cfg =>
|
||||||
{
|
{
|
||||||
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
|
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
|
||||||
|
|
||||||
|
cfg.AddBehavior<CreateHistoryBehavior>();
|
||||||
|
cfg.AddBehavior<SavePdfBehavior>();
|
||||||
#if WINDOWS
|
#if WINDOWS
|
||||||
|
cfg.AddBehavior<PdfMergeBehavior>();
|
||||||
cfg.AddBehavior<AddReportBehavior>();
|
cfg.AddBehavior<AddReportBehavior>();
|
||||||
#endif
|
#endif
|
||||||
cfg.AddBehavior<SavePdfBehavior>();
|
|
||||||
cfg.AddBehavior<CreateHistoryBehavior>();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add memory cache
|
// Add memory cache
|
||||||
|
|||||||
@ -39,9 +39,7 @@ public class AddReportBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
|||||||
{
|
{
|
||||||
var docResult = await next(cancel);
|
var docResult = await next(cancel);
|
||||||
|
|
||||||
var report = await CreateReport(request.Envelope!, cancel);
|
request.Report = await CreateReport(request.Envelope!, cancel);
|
||||||
|
|
||||||
var reportBase64 = Convert.ToBase64String(report);
|
|
||||||
|
|
||||||
return docResult;
|
return docResult;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,6 +34,8 @@ public class CreateHistoryBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<byte[]> Handle(BurnPdfCommand request, RequestHandlerDelegate<byte[]> next, CancellationToken cancel)
|
public async Task<byte[]> Handle(BurnPdfCommand request, RequestHandlerDelegate<byte[]> next, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
|
var doc = await next(cancel);
|
||||||
|
|
||||||
if (!request.Debug)
|
if (!request.Debug)
|
||||||
await _sender.Send(new CreateHistoryCommand()
|
await _sender.Send(new CreateHistoryCommand()
|
||||||
{
|
{
|
||||||
@ -42,6 +44,6 @@ public class CreateHistoryBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
|||||||
Status = EnvelopeStatus.EnvelopeReportCreated,
|
Status = EnvelopeStatus.EnvelopeReportCreated,
|
||||||
}, cancel);
|
}, cancel);
|
||||||
|
|
||||||
return await next(cancel);
|
return doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,88 @@
|
|||||||
|
#if WINDOWS
|
||||||
|
using EnvelopeGenerator.Application.Common.Extensions;
|
||||||
|
using EnvelopeGenerator.Application.Exceptions;
|
||||||
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
|
using GdPicture14;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Application.Pdf.Behaviors;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public class PdfMergeBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
||||||
|
{
|
||||||
|
private static readonly bool ALLOW_RASTERIZATION = true;
|
||||||
|
|
||||||
|
private static readonly bool ALLOW_VECTORIZATION = true;
|
||||||
|
|
||||||
|
private static readonly PdfConversionConformance PDFAConformanceLevel = PdfConversionConformance.PDF_A_1b;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="next"></param>
|
||||||
|
/// <param name="cancel"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
|
public async Task<byte[]> Handle(BurnPdfCommand request, RequestHandlerDelegate<byte[]> next, CancellationToken cancel)
|
||||||
|
{
|
||||||
|
var doc = await next(cancel);
|
||||||
|
|
||||||
|
doc = request.Report is byte[] report
|
||||||
|
? MergeDocuments(doc, report)
|
||||||
|
: throw new InvalidOperationException("The final document report could not be merged." +
|
||||||
|
"There may be an error related to the behavior register order." +
|
||||||
|
"Request details:\n" + request.ToJson(Format.Json.ForDiagnostics));
|
||||||
|
|
||||||
|
string base64String = Convert.ToBase64String(doc);
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="pDocument"></param>
|
||||||
|
/// <param name="pReport"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="MergeDocumentException"></exception>
|
||||||
|
public static byte[] MergeDocuments(byte[] pDocument, byte[] pReport)
|
||||||
|
{
|
||||||
|
using var oDocumentStream = new MemoryStream(pDocument);
|
||||||
|
using var oReportStream = new MemoryStream(pReport);
|
||||||
|
using var oFinalStream = new MemoryStream();
|
||||||
|
using var oDocumentPDF = new GdPicturePDF();
|
||||||
|
using var oReportPDF = new GdPicturePDF();
|
||||||
|
GdPictureStatus oStatus = GdPictureStatus.OK;
|
||||||
|
|
||||||
|
// Load the source file into memory
|
||||||
|
oDocumentPDF.LoadFromStream(oDocumentStream, true);
|
||||||
|
|
||||||
|
oStatus = oDocumentPDF.GetStat();
|
||||||
|
if (oStatus != GdPictureStatus.OK)
|
||||||
|
throw new MergeDocumentException($"Document could not be loaded: {oStatus}");
|
||||||
|
|
||||||
|
// Load the report file into memory
|
||||||
|
oReportPDF.LoadFromStream(oReportStream, true);
|
||||||
|
oStatus = oReportPDF.GetStat();
|
||||||
|
if (oStatus != GdPictureStatus.OK)
|
||||||
|
throw new MergeDocumentException($"Report could not be loaded: {oStatus}");
|
||||||
|
|
||||||
|
// Merge the documents
|
||||||
|
var oMergedPDF = oDocumentPDF.Merge2Documents(oDocumentPDF, oReportPDF);
|
||||||
|
oStatus = oMergedPDF.GetStat();
|
||||||
|
if (oStatus != GdPictureStatus.OK)
|
||||||
|
throw new MergeDocumentException($"Documents could not be merged: {oStatus}");
|
||||||
|
|
||||||
|
// Convert to PDF/A
|
||||||
|
oMergedPDF.ConvertToPDFA(oFinalStream, PDFAConformanceLevel, ALLOW_VECTORIZATION, ALLOW_RASTERIZATION);
|
||||||
|
oStatus = oDocumentPDF.GetStat();
|
||||||
|
if (oStatus != GdPictureStatus.OK)
|
||||||
|
throw new MergeDocumentException($"Document could not be converted to PDF/A: {oStatus}");
|
||||||
|
|
||||||
|
return oFinalStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -24,6 +24,10 @@ public record BurnPdfCommand(int? EnvelopeId = null, string? EnvelopeUuid = null
|
|||||||
internal bool Debug { get; set; }
|
internal bool Debug { get; set; }
|
||||||
|
|
||||||
internal Envelope? Envelope { get; set; }
|
internal Envelope? Envelope { get; set; }
|
||||||
|
|
||||||
|
#if WINDOWS
|
||||||
|
internal byte[]? Report { get; set; }
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user