- Added detailed request context to all MergeDocumentException messages - Clarified conversion error message from "converted to PDF/A" to "converted to byte" - Removed unused base64 conversion and redundant variable assignment - Improved code readability and string concatenation formatting
71 lines
2.9 KiB
C#
71 lines
2.9 KiB
C#
#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[]>
|
|
{
|
|
/// <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);
|
|
|
|
if (request.Report is null)
|
|
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));
|
|
|
|
using var oDocumentStream = new MemoryStream(doc);
|
|
using var oReportStream = new MemoryStream(request.Report);
|
|
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}."
|
|
+ "Request details:\n" + request.ToJson(Format.Json.ForDiagnostics));
|
|
|
|
// 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}."
|
|
+ "Request details:\n" + request.ToJson(Format.Json.ForDiagnostics));
|
|
|
|
// 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}."
|
|
+ "Request details:\n" + request.ToJson(Format.Json.ForDiagnostics));
|
|
|
|
// Convert to byte
|
|
oMergedPDF.SaveToStream(oFinalStream);
|
|
oStatus = oDocumentPDF.GetStat();
|
|
if (oStatus != GdPictureStatus.OK)
|
|
throw new MergeDocumentException($"Document could not be converted to byte: {oStatus}."
|
|
+ "Request details:\n" + request.ToJson(Format.Json.ForDiagnostics));
|
|
|
|
return oFinalStream.ToArray();
|
|
}
|
|
}
|
|
#endif |