Improve error handling in PDF processing workflow
Refactored the PDF processing workflow to enhance error handling and provide more descriptive exception messages. Added `try-catch` blocks around the PDF/A-3b conversion and document saving steps to handle potential failures. Labeled the workflow steps for clarity: - Step 1: Convert to PDF/A-3b - Step 2: Buffer converted PDF - Step 3: Embed attachment - Step 4: Save document Removed redundant `MemoryStream` initialization for the original PDF and updated comments to improve code readability and maintainability.
This commit is contained in:
@@ -35,20 +35,28 @@ public class PdfResultPackageService(
|
||||
// 3. Original auf PDF/A-3b hochstufen + Bericht anhängen
|
||||
await Task.Run(() =>
|
||||
{
|
||||
// Original in MemoryStream laden
|
||||
using var inputStream = new MemoryStream(originalPdfBytes);
|
||||
using var outputStream = new MemoryStream();
|
||||
|
||||
// PDF/A-3b Konvertierung
|
||||
var converter = new PdfDocumentConverter(inputStream);
|
||||
// Schritt 1: PDF/A-3b Konvertierung
|
||||
PdfDocumentConverter converter;
|
||||
try
|
||||
{
|
||||
converter = new PdfDocumentConverter(inputStream);
|
||||
converter.Convert(PdfCompatibility.PdfA3b);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Konvertierung nach PDF/A-3b fehlgeschlagen. " +
|
||||
"Die Originaldatei ist möglicherweise beschädigt oder nicht konvertierbar.", ex);
|
||||
}
|
||||
|
||||
// Konvertiertes PDF in MemoryStream speichern
|
||||
// Schritt 2: Konvertiertes PDF puffern
|
||||
using var convertedStream = new MemoryStream();
|
||||
converter.SaveDocument(convertedStream);
|
||||
convertedStream.Position = 0;
|
||||
|
||||
// Bericht als Anhang einbetten
|
||||
// Schritt 3: Anhang einbetten
|
||||
using var processor = new PdfDocumentProcessor();
|
||||
processor.LoadDocument(convertedStream);
|
||||
|
||||
@@ -62,8 +70,17 @@ public class PdfResultPackageService(
|
||||
Data = File.ReadAllBytes(reportPath)
|
||||
});
|
||||
|
||||
// Speichern
|
||||
// Schritt 4: Speichern
|
||||
try
|
||||
{
|
||||
processor.SaveDocument(outputPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Result-PDF konnte nicht gespeichert werden unter '{outputPath}'. " +
|
||||
"Prüfe ob das Verzeichnis existiert und beschreibbar ist.", ex);
|
||||
}
|
||||
});
|
||||
|
||||
logger.LogInformation(
|
||||
|
||||
Reference in New Issue
Block a user