[chore:] logging for PdfBuilder.cs
This commit is contained in:
+94
-1
@@ -17,6 +17,8 @@ public class PdfBuilder
|
||||
private readonly XFont _smallFont;
|
||||
|
||||
public PdfBuilder(PdfExportSettings? settings = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
EnsureFontResolverRegistered();
|
||||
_settings = settings ?? new PdfExportSettings();
|
||||
@@ -32,8 +34,9 @@ public class PdfBuilder
|
||||
chosenFamily = StripStyleSuffix(Path.GetFileNameWithoutExtension(first)) ?? chosenFamily;
|
||||
}
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while searching for fonts: {ex.Message}",Logger.LogType.Error);
|
||||
chosenFamily = "Arial";
|
||||
}
|
||||
|
||||
@@ -41,15 +44,32 @@ public class PdfBuilder
|
||||
_regularFont = new XFont(chosenFamily, _settings.fontSize, XFontStyleEx.Regular);
|
||||
_smallFont = new XFont(chosenFamily, _settings.smallFontSize, XFontStyleEx.Regular);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while font resolving: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void EnsureFontResolverRegistered()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (GlobalFontSettings.FontResolver != null) return;
|
||||
//var fontsDir = Path.Combine(AppContext.BaseDirectory, "fonts");
|
||||
GlobalFontSettings.FontResolver = new StableFontResolver(Global._instance.font_path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while ensuring font resolver register state: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static string StripStyleSuffix(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(name)) return name;
|
||||
var idx = name.IndexOf('-');
|
||||
@@ -58,6 +78,14 @@ public class PdfBuilder
|
||||
return name.Substring(0, idx);
|
||||
return name;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while stripping style suffix: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -68,6 +96,8 @@ public class PdfBuilder
|
||||
/// <param name="outputPath">Path where the PDF should be saved</param>
|
||||
public void CreateAddressLabelPdfFromAddressSetWithPlaceholder(int addressSetId, string placeholderText,
|
||||
string outputPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Find the AddressSet by ID
|
||||
var addressSet = Settings._instance.addressSets.GetAddressSetByID(addressSetId);
|
||||
@@ -106,6 +136,12 @@ public class PdfBuilder
|
||||
|
||||
CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while creating address label pdf from address set with placeholder: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a PDF document with a single placeholder cell for other information.
|
||||
@@ -114,6 +150,8 @@ public class PdfBuilder
|
||||
/// <param name="placeholderText">Text for the first cell (top-left)</param>
|
||||
/// <param name="outputPath">Path where the PDF should be saved</param>
|
||||
public void CreateAddressLabelPdfWithPlaceholder(List<string> addresses, string placeholderText, string outputPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (addresses == null || addresses.Count == 0)
|
||||
throw new ArgumentException("Addresses array cannot be null or empty");
|
||||
@@ -136,6 +174,12 @@ public class PdfBuilder
|
||||
|
||||
document.Save(outputPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while creating address label pdf with placeholder: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void DrawPage(XGraphics gfx, List<string> addresses, ref int addressIndex)
|
||||
{
|
||||
@@ -158,6 +202,8 @@ public class PdfBuilder
|
||||
|
||||
private void DrawPageWithPlaceholder(XGraphics gfx, List<string> addresses, ref int addressIndex,
|
||||
ref bool isFirstCell, string placeholderText)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (var row = 0; row < _settings.rowsPerPage; row++)
|
||||
for (var col = 0; col < _settings.columnsPerPage; col++)
|
||||
@@ -182,8 +228,16 @@ public class PdfBuilder
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while drawing page with placholder: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void DrawCell(XGraphics gfx, double x, double y, string? address)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cellWidthPoints = MmToPoints(GetCellWidthMm());
|
||||
var cellHeightPoints = MmToPoints(GetCellHeightMm());
|
||||
@@ -195,8 +249,16 @@ public class PdfBuilder
|
||||
// Draw address content if available
|
||||
if (!string.IsNullOrEmpty(address)) DrawMarkdownText(gfx, address, x, y, cellWidthPoints, cellHeightPoints);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while drawing cell: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void DrawEmptyCell(XGraphics gfx, double x, double y)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cellWidthPoints = MmToPoints(GetCellWidthMm());
|
||||
var cellHeightPoints = MmToPoints(GetCellHeightMm());
|
||||
@@ -204,8 +266,16 @@ public class PdfBuilder
|
||||
var rect = new XRect(x, y, cellWidthPoints, cellHeightPoints);
|
||||
gfx.DrawRectangle(XPens.Black, rect);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while drawing empty cell: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void DrawMarkdownText(XGraphics gfx, string text, double x, double y, double cellWidth, double cellHeight)
|
||||
{
|
||||
try
|
||||
{
|
||||
var paddingLeftPoints = MmToPoints(_settings.cellPaddingLeftMm);
|
||||
var paddingRightPoints = MmToPoints(_settings.cellPaddingRightMm);
|
||||
@@ -240,8 +310,16 @@ public class PdfBuilder
|
||||
currentY += lineHeight;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while drawing markdown text: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void DrawLineWithMarkdown(XGraphics gfx, string line, double x, double y, double maxWidth)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) return;
|
||||
var currentX = x;
|
||||
@@ -331,8 +409,16 @@ public class PdfBuilder
|
||||
i = textEnd;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while drawing markdown line: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private string TruncateTextToWidth(XGraphics gfx, string text, XFont font, double maxWidth)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return text;
|
||||
@@ -347,6 +433,13 @@ public class PdfBuilder
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log($"Error while truncating text to width: {ex.Message}",Logger.LogType.Error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts millimeters to points (1 mm = 2.834645669 points)
|
||||
|
||||
Reference in New Issue
Block a user