Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for displaying a text if the user is previewing the pdf #904

Merged
merged 11 commits into from
Nov 15, 2024
7 changes: 1 addition & 6 deletions src/Altinn.App.Api/Controllers/PdfController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,14 @@ public async Task<ActionResult> GetPdfPreview(
[FromRoute] Guid instanceGuid
)
{
if (_env.IsProduction())
{
return NotFound();
}

var instance = await _instanceClient.GetInstance(app, org, instanceOwnerPartyId, instanceGuid);
string? taskId = instance.Process?.CurrentTask?.ElementId;
if (instance == null || taskId == null)
{
return NotFound("Did not find instance or task");
}

Stream pdfContent = await _pdfService.GeneratePdf(instance, taskId, CancellationToken.None);
Stream pdfContent = await _pdfService.GeneratePdf(instance, taskId, CancellationToken.None, true);
return new FileStreamResult(pdfContent, "application/pdf");
}

Expand Down
3 changes: 2 additions & 1 deletion src/Altinn.App.Core/Internal/Pdf/IPdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public interface IPdfService
/// <param name="instance">The instance details.</param>
/// <param name="taskId">The task id for witch the pdf is generated</param>
/// <param name="ct">Cancellation Token for when a request should be stopped before it's completed.</param>
Task<Stream> GeneratePdf(Instance instance, string taskId, CancellationToken ct);
/// <param name="isPreview">If set to true, this will show a text saying that this is a preview PDF.</param>
Task<Stream> GeneratePdf(Instance instance, string taskId, CancellationToken ct, Boolean? isPreview = false);
adamhaeger marked this conversation as resolved.
Show resolved Hide resolved
}
56 changes: 49 additions & 7 deletions src/Altinn.App.Core/Internal/Pdf/PdfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,19 @@ public async Task GenerateAndStorePdf(Instance instance, string taskId, Cancella

TextResource? textResource = await GetTextResource(instance, language);

var pdfContent = await GeneratePdfContent(instance, taskId, language, textResource, ct);
var pdfContent = await GeneratePdfContent(instance, language, ct, textResource, null);

string fileName = GetFileName(instance, textResource);
await _dataClient.InsertBinaryData(instance.Id, PdfElementType, PdfContentType, fileName, pdfContent, taskId);
}

/// <inheritdoc/>
public async Task<Stream> GeneratePdf(Instance instance, string taskId, CancellationToken ct)
public async Task<Stream> GeneratePdf(
Instance instance,
string taskId,
CancellationToken ct,
Boolean? isPreview = false
)
{
using var activity = _telemetry?.StartGeneratePdfActivity(instance, taskId);

Expand All @@ -103,15 +108,15 @@ public async Task<Stream> GeneratePdf(Instance instance, string taskId, Cancella

TextResource? textResource = await GetTextResource(instance, language);

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

private async Task<Stream> GeneratePdfContent(
Instance instance,
string taskId,
string language,
TextResource? textResource,
CancellationToken ct
CancellationToken ct,
TextResource? textResource = null,
Boolean? isPreview = false
adamhaeger marked this conversation as resolved.
Show resolved Hide resolved
)
{
var baseUrl = _generalSettings.FormattedExternalAppBaseUrl(new AppIdentifier(instance));
Expand All @@ -122,7 +127,17 @@ CancellationToken ct
Uri uri = BuildUri(baseUrl, pagePath, language);

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

string? footerContent = null;

if (isPreview == true)

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == true' can be simplified to 'A'.
adamhaeger marked this conversation as resolved.
Show resolved Hide resolved
{
footerContent = GetPreviewFooter(textResource);
}
else if (displayFooter)
{
footerContent = GetFooterContent(instance, textResource);
}

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

Expand Down Expand Up @@ -249,12 +264,39 @@ private static string GetFileName(Instance instance, TextResource? textResource)
return null;
}

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

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

return null;
adamhaeger marked this conversation as resolved.
Show resolved Hide resolved
}

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

private static string GetPreviewFooter(TextResource? textResource)
{
string previewText = GetPdfPreviewText(textResource) ?? "Dette er en forhåndsvisning";
return $@"<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>{previewText}</span>
</div>
</div>";
}

private string GetFooterContent(Instance instance, TextResource? textResource)
{
TimeZoneInfo timeZone = TimeZoneInfo.Utc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
{
"id": "Side1.Button-Lj0cYG.title",
"value": "Avvis"
},
{
"id": "PdfPreviewText",
"value": "Dette er en forhåndsvisning"
adamhaeger marked this conversation as resolved.
Show resolved Hide resolved
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task End_PaymentCompleted_ShouldGeneratePdfReceipt()
await _paymentProcessTask.End(taskId, instance);

// Assert
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None));
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None, false));
_dataClientMock.Verify(x =>
x.InsertBinaryData(
instance.Id,
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task End_PaymentSkipped_ShouldNotGeneratePdfReceipt()
await _paymentProcessTask.End(taskId, instance);

// Assert
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None), Times.Never);
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None, false), Times.Never);
_dataClientMock.Verify(
x =>
x.InsertBinaryData(
Expand Down Expand Up @@ -142,7 +142,7 @@ public async Task End_PaymentNotCompleted_ShouldThrowException()
.ReturnsAsync(PaymentStatus.Created);

// Act and assert
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None), Times.Never);
_pdfServiceMock.Verify(x => x.GeneratePdf(instance, taskId, CancellationToken.None, false), Times.Never);
_dataClientMock.Verify(
x =>
x.InsertBinaryData(
Expand Down Expand Up @@ -220,7 +220,9 @@ public async Task End_ValidConfiguration_ShouldNotThrow()

using var memoryStream = new MemoryStream();
_pdfServiceMock
.Setup(ps => ps.GeneratePdf(It.IsAny<Instance>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Setup(ps =>
ps.GeneratePdf(It.IsAny<Instance>(), It.IsAny<string>(), It.IsAny<CancellationToken>(), false)
)
.ReturnsAsync(memoryStream);

Func<Task> act = async () => await _paymentProcessTask.End("taskId", new Instance());
Expand Down
Loading