Add support for generating result PDFs

Introduced the `ResultFilePath` property in the `ZugferdInvoice` model to store the path of generated result PDFs. Added a new service, `PdfResultPackageService`, to create result PDFs by converting the original PDF to PDF/A-3b format and attaching a report file. Updated `Upload.cshtml` and `Upload.cshtml.cs` to handle and display the `ResultFilePath`.

Created a migration to add the `ResultFilePath` column to the database. Updated `Program.cs` to register the new service and added configuration sections in `appsettings.json` for input and output directories. Enhanced error handling and logging for better traceability.
This commit is contained in:
OlgunR
2026-05-27 13:43:14 +02:00
parent 6a46bf4f4b
commit 87f27682ce
9 changed files with 242 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
using DXApp.TemplateKitProject.Models;
using DXApp.TemplateKitProject.Data;
using DXApp.TemplateKitProject.Models;
using DXApp.TemplateKitProject.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
@@ -8,6 +9,8 @@ namespace DXApp.TemplateKitProject.Pages.Invoices;
public class UploadModel(
PdfAttachmentExtractorService extractor,
ZugferdImportService zugferdImportService,
PdfResultPackageService resultPackageService,
AppDbContext db,
ILogger<UploadModel> logger) : PageModel
{
[BindProperty]
@@ -17,6 +20,7 @@ public class UploadModel(
public bool ExtractionDone { get; private set; }
public string? ErrorMessage { get; private set; }
public ZugferdInvoice? ImportedInvoice { get; private set; }
public string? ResultFilePath { get; private set; }
public void OnGet()
{ }
@@ -42,8 +46,9 @@ public class UploadModel(
// Stream in MemoryStream puffern → kann zweimal gelesen werden
using var memStream = new MemoryStream();
await PdfFile.CopyToAsync(memStream);
var originalBytes = memStream.ToArray(); // ← neu: als byte[] merken
// 1. Anhänge extrahieren und auf Disk speichern
// 1. Anhänge extrahieren
memStream.Position = 0;
Result = extractor.ExtractAttachments(memStream, PdfFile.FileName);
@@ -52,6 +57,20 @@ public class UploadModel(
{
memStream.Position = 0;
ImportedInvoice = await zugferdImportService.ImportAsync(memStream, "Upload");
// 3. Result-Package erstellen (nur wenn Import erfolgreich)
if (ImportedInvoice is not null)
{
ResultFilePath = await resultPackageService.CreateResultPackageAsync(
originalBytes, PdfFile.FileName, ImportedInvoice);
// ResultFilePath in DB aktualisieren
if (ResultFilePath is not null)
{
ImportedInvoice.ResultFilePath = ResultFilePath;
await db.SaveChangesAsync();
}
}
}
}
catch (Exception ex)