ShiftScheduler/Services/PdfExportService.cs
Claudio Schaad 1a00709447
fix: resolve PDF export icons path after moving icons to config/icons/ (#62)
Icons were moved from wwwroot/icons/ to config/icons/ in the configurable-icons
feature, but PdfExportService still referenced the old AppContext.BaseDirectory path.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 17:43:34 +02:00

286 lines
No EOL
11 KiB
C#

using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using System.Globalization;
using ShiftScheduler.Shared;
namespace ShiftScheduler.Services
{
public class PdfExportService
{
private static readonly Color borderColor = Colors.Grey.Darken1;
private static readonly float boarderWidth = 1;
static PdfExportService()
{
QuestPDF.Settings.License = LicenseType.Community;
}
public byte[] GenerateMonthlySchedulePdf(List<ShiftWithTransport> shiftsWithTransport)
{
if (!shiftsWithTransport.Any())
return new byte[0];
var year = shiftsWithTransport.First().Date.Year;
var month = shiftsWithTransport.First().Date.Month;
var document = Document.Create(container =>
{
container.Page(page =>
{
page.Margin(15);
page.Size(PageSizes.A4.Landscape());
page.DefaultTextStyle(x => x.FontSize(10));
page.Header()
.Text($"Shift Schedule for {CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month)} {year}")
.FontSize(16).Bold().AlignCenter();
page.Content().Element(e => ComposeTable(e, shiftsWithTransport));
});
});
return document.GeneratePdf();
}
void ComposeTable(IContainer container, List<ShiftWithTransport> shiftsWithTransport)
{
container.Table(table =>
{
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
});
var scheduleDictionary = shiftsWithTransport.ToDictionary(s => s.Date, s => s);
var year = shiftsWithTransport.First().Date.Year;
var month = shiftsWithTransport.First().Date.Month;
var daysInMonth = DateTime.DaysInMonth(year, month);
var monthDates = Enumerable.Range(1, daysInMonth)
.Select(day => new DateTime(year, month, day))
.ToList();
var weeks = monthDates
.GroupBy(d => CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(
d, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
.ToList();
foreach (var week in weeks)
{
RenderShiftForWeek(table, week, scheduleDictionary);
RenderEmptyRow(table);
}
});
}
private static void RenderShiftForWeek(TableDescriptor table, IGrouping<int, DateTime> week,
IReadOnlyDictionary<DateTime, ShiftWithTransport> shiftsWithTransport)
{
var weekDates = week.ToList();
var dayInWeek = weekDates.Count;
var preRender = weekDates.First().DayOfWeek != DayOfWeek.Monday ? 7 - dayInWeek : 0;
var postRender = weekDates.First().DayOfWeek == DayOfWeek.Monday ? 7 - dayInWeek : 0;
// Date
RenderEmptyCells(table, preRender);
foreach (var day in Enumerable.Range(0, dayInWeek))
{
var dddd = weekDates.ElementAtOrDefault(day).ToString("dd.MM.yyyy (ddd)");
table.Cell().Element(CellStyleFirst).Text(dddd);
}
RenderEmptyCells(table, postRender);
// Icon
RenderEmptyCells(table, preRender);
foreach (var day in Enumerable.Range(0, dayInWeek))
{
var shiftWithTransport = shiftsWithTransport.GetValueOrDefault(weekDates.ElementAtOrDefault(day));
if (shiftWithTransport != null)
{
var shift = shiftWithTransport.Shift;
if (shift.IsPngIcon)
{
var iconPath = Path.Combine(Directory.GetCurrentDirectory(), "config", "icons", shift.Icon);
if (File.Exists(iconPath))
{
table.Cell().Height(24).Element(CellStyleMiddle).Image(iconPath).FitArea();
continue;
}
}
table.Cell().Element(CellStyleMiddle).Text(shift.Name);
}
}
RenderEmptyCells(table, postRender);
// Morning times
RenderEmptyCells(table, preRender);
foreach (var day in Enumerable.Range(0, dayInWeek))
{
var shiftWithTransport = shiftsWithTransport.GetValueOrDefault(weekDates.ElementAtOrDefault(day));
if (!string.IsNullOrWhiteSpace(shiftWithTransport?.Shift.MorningTime))
{
table.Cell().Element(CellStyleTimes).Text($" {shiftWithTransport.Shift.MorningTime}");
}
else
{
table.Cell().Element(CellStyleMiddle).Text("");
}
}
RenderEmptyCells(table, postRender);
// Afternoon times
RenderEmptyCells(table, preRender);
foreach (var day in Enumerable.Range(0, dayInWeek))
{
var shiftWithTransport = shiftsWithTransport.GetValueOrDefault(weekDates.ElementAtOrDefault(day));
if (!string.IsNullOrWhiteSpace(shiftWithTransport?.Shift.AfternoonTime))
{
table.Cell().Element(CellStyleTimes).Text($" {shiftWithTransport.Shift.AfternoonTime}");
}
else
{
table.Cell().Element(CellStyleMiddle).Text("");
}
}
RenderEmptyCells(table, postRender);
// Transport
RenderEmptyCells(table, preRender);
foreach (var day in Enumerable.Range(0, dayInWeek))
{
var shiftWithTransport = shiftsWithTransport.GetValueOrDefault(weekDates.ElementAtOrDefault(day));
var transportSummary = GetTransportSummary(shiftWithTransport);
if (!string.IsNullOrWhiteSpace(transportSummary))
{
table.Cell().Element(CellStyleTransport).Text(transportSummary);
}
else {
table.Cell().Element(CellStyleLast).Text("");
}
}
RenderEmptyCells(table, postRender);
}
private static string GetTransportSummary(ShiftWithTransport? shiftWithTransport)
{
if (shiftWithTransport == null) return "";
var transportLines = new List<string>();
if (shiftWithTransport.MorningTransport != null)
{
var morningInfo = FormatTransportConnection(shiftWithTransport.MorningTransport);
transportLines.Add(morningInfo);
}
if (shiftWithTransport.AfternoonTransport != null)
{
var afternoonInfo = FormatTransportConnection(shiftWithTransport.AfternoonTransport);
transportLines.Add(afternoonInfo);
}
return transportLines.Count > 0 ? string.Join("\n", transportLines) : "";
}
private static string FormatTransportConnection(TransportConnection transport)
{
var departure = transport.DepartureTime.ToString("HH:mm");
var arrival = transport.ArrivalTime.ToString("HH:mm");
return $" {transport.Platform}: {departure} → {arrival}";
}
private static void RenderEmptyRow(TableDescriptor table)
{
RenderEmptyCells(table, 7);
}
private static void RenderEmptyCells(TableDescriptor table, int cellCount)
{
foreach (var _ in Enumerable.Range(0, cellCount))
{
RenderEmptyCell(table);
}
}
private static void RenderEmptyCell(TableDescriptor table)
{
table.Cell().Element(EmptyCellStyle).Text("");
}
private static IContainer CellStyleFirst(IContainer container)
{
return container
.Padding(0)
.BorderLeft(boarderWidth)
.BorderTop(boarderWidth)
.BorderRight(boarderWidth)
.BorderColor(borderColor)
.AlignCenter()
.AlignMiddle();
}
private static IContainer CellStyleMiddle(IContainer container)
{
return container
.Padding(0)
.BorderLeft(boarderWidth)
.BorderRight(boarderWidth)
.BorderColor(borderColor)
.AlignCenter()
.AlignMiddle();
}
private static IContainer CellStyleTimes(IContainer container)
{
return container
.Padding(0)
.BorderLeft(boarderWidth)
.BorderRight(boarderWidth)
.BorderColor(borderColor)
.AlignCenter()
.AlignMiddle()
.Width(37, Unit.Millimetre)
.Background(Color.FromHex("#c7ffd6"));
}
private static IContainer CellStyleTransport(IContainer container)
{
return container
.Padding(0)
.BorderLeft(boarderWidth)
.BorderBottom(boarderWidth)
.BorderRight(boarderWidth)
.BorderColor(borderColor)
.AlignCenter()
.AlignMiddle().Width(37, Unit.Millimetre)
.Background(Color.FromHex("#bdccff"));
}
private static IContainer CellStyleLast(IContainer container)
{
return container
.Padding(0)
.BorderLeft(boarderWidth)
.BorderBottom(boarderWidth)
.BorderRight(boarderWidth)
.BorderColor(borderColor)
.AlignCenter()
.AlignMiddle();
}
private static IContainer EmptyCellStyle(IContainer container)
{
return container
.Padding(0)
.BorderLeft(0)
.BorderBottom(0)
.BorderRight(0)
.BorderTop(0);
}
}
}