Skip to content

Commit

Permalink
Use text resource bindings to get footer title (#836) (#896)
Browse files Browse the repository at this point in the history
* use text resource bindings to get footer title

* update verified test file

Co-authored-by: Ivar Nesje <[email protected]>
  • Loading branch information
Magnusrm and ivarne authored Nov 8, 2024
1 parent 9c3b7d2 commit dcd9c77
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/Altinn.App.Core/Internal/Pdf/PdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ public async Task GenerateAndStorePdf(Instance instance, string taskId, Cancella

var language = GetOverriddenLanguage(queries) ?? await GetLanguage(user);

var pdfContent = await GeneratePdfContent(instance, taskId, language, ct);
TextResource? textResource = await GetTextResource(instance, language);

var appIdentifier = new AppIdentifier(instance);
var pdfContent = await GeneratePdfContent(instance, taskId, language, textResource, ct);

TextResource? textResource = await GetTextResource(appIdentifier.App, appIdentifier.Org, language);
string fileName = GetFileName(instance, textResource);
await _dataClient.InsertBinaryData(instance.Id, PdfElementType, PdfContentType, fileName, pdfContent, taskId);
}
Expand All @@ -102,13 +101,16 @@ public async Task<Stream> GeneratePdf(Instance instance, string taskId, Cancella

var language = GetOverriddenLanguage(queries) ?? await GetLanguage(user);

return await GeneratePdfContent(instance, taskId, language, ct);
TextResource? textResource = await GetTextResource(instance, language);

return await GeneratePdfContent(instance, taskId, language, textResource, ct);
}

private async Task<Stream> GeneratePdfContent(
Instance instance,
string taskId,
string language,
TextResource? textResource,
CancellationToken ct
)
{
Expand All @@ -120,7 +122,7 @@ CancellationToken ct
Uri uri = BuildUri(baseUrl, pagePath, language);

bool displayFooter = _pdfGeneratorSettings.DisplayFooter;
string? footerContent = displayFooter ? GetFooterContent(instance) : null;
string? footerContent = displayFooter ? GetFooterContent(instance, textResource) : null;

Stream pdfContent = await _pdfGeneratorClient.GeneratePdf(uri, footerContent, ct);

Expand Down Expand Up @@ -188,8 +190,11 @@ await _profileClient.GetUserProfile((int)userId)
return null;
}

private async Task<TextResource?> GetTextResource(string app, string org, string language)
private async Task<TextResource?> GetTextResource(Instance instance, string language)
{
var appIdentifier = new AppIdentifier(instance);
string org = appIdentifier.Org;
string app = appIdentifier.App;
TextResource? textResource = await _resourceService.GetTexts(org, app, language);

if (textResource == null && language != LanguageConst.Nb)
Expand All @@ -213,29 +218,44 @@ private static string GetFileName(Instance instance, TextResource? textResource)
return GetValidFileName(fileName);
}

TextResourceElement? titleText =
textResource.Resources.Find(textResourceElement =>
textResourceElement.Id.Equals("appName", StringComparison.Ordinal)
)
?? textResource.Resources.Find(textResourceElement =>
textResourceElement.Id.Equals("ServiceName", StringComparison.Ordinal)
);
string? titleText = GetTitleText(textResource);

if (titleText is not null && !string.IsNullOrEmpty(titleText.Value))
if (!string.IsNullOrEmpty(titleText))
{
fileName = titleText.Value + ".pdf";
fileName = titleText + ".pdf";
}

return GetValidFileName(fileName);
}

private static string? GetTitleText(TextResource? textResource)
{
if (textResource is not null)
{
TextResourceElement? titleText =
textResource.Resources.Find(textResourceElement =>
textResourceElement.Id.Equals("appName", StringComparison.Ordinal)
)
?? textResource.Resources.Find(textResourceElement =>
textResourceElement.Id.Equals("ServiceName", StringComparison.Ordinal)
);

if (titleText is not null)
{
return titleText.Value;
}
}

return null;
}

private static string GetValidFileName(string fileName)
{
fileName = Uri.EscapeDataString(fileName.AsFileName(false));
return fileName;
}

private string GetFooterContent(Instance instance)
private string GetFooterContent(Instance instance, TextResource? textResource)
{
TimeZoneInfo timeZone = TimeZoneInfo.Utc;
try
Expand All @@ -250,13 +270,14 @@ private string GetFooterContent(Instance instance)

DateTimeOffset now = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, timeZone);

string title = GetTitleText(textResource) ?? "Altinn";
string dateGenerated = now.ToString("G", new CultureInfo("nb-NO"));
string altinnReferenceId = instance.Id.Split("/")[1].Split("-")[4];

string footerTemplate =
$@"<div style='font-family: Inter; font-size: 12px; width: 100%; display: flex; flex-direction: row; align-items: center; gap: 12px; padding: 0 70px 0 70px;'>
<div style='display: flex; flex-direction: row; width: 100%; align-items: center'>
<span class='title'></span>
<span>{title}</span>
<div
id='header-template'
style='color: #F00; font-weight: 700; border: 1px solid #F00; padding: 6px 8px; margin-left: auto;'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
ActivityName: ApplicationMetadata.Service.GetLayoutSettingsForSet,
IdFormat: W3C
},
{
ActivityName: ApplicationMetadata.Service.GetTexts,
IdFormat: W3C
},
{
ActivityName: ApplicationMetadata.Service.GetTexts,
IdFormat: W3C
},
{
ActivityName: ApplicationMetadata.Service.GetValidationConfiguration,
IdFormat: W3C
Expand Down

0 comments on commit dcd9c77

Please sign in to comment.